Open lesson

Mission: the midnight restart

Command palette

This mission workspace keeps code and notes in this browser tab. Export them before closing it. Running code is practice, not an automatic grade for the mission.

Brief

Decide when to confirm a batch and record its checkpoint in agreement with rows actually stored.

In the story, a vendor service stopped just after writing B1. Imani has E7 and two forms of TRANSFER-7. Mara gives you a simulation: B1 has E7 and TRANSFER-7, B2 has E9, and confirmed count starts at 0. The practice database can hold rows and checkpoint in one transaction. Voss claims “processed” means “shown once on screen”; explain a more precise rule.

Your mission

  1. Give the confirmed count after B1 and after B2 once each is truly checked.
  2. Describe the correct order of actions inside B1’s transaction.
  3. Give expected state if the program stops before or after B1’s COMMIT.
  4. Explain what can happen if the database keeps B1 while an external notebook still says 0.

How to check your work

  • After B1, this exercise’s offset is 2; after B2, 3.
  • Insert, check both rows, update checkpoint and COMMIT belong to one group.
  • Before commit: 0 rows and 0 confirmed; afterward: 2 rows and 2 confirmed.
  • A notebook at 0 may replay B1; existing events must be recognized before republishing.

Course example

import sqlite3

db = sqlite3.connect(':memory:')
db.execute('CREATE TABLE events (event_id TEXT)')
db.execute('CREATE TABLE checkpoints (name TEXT PRIMARY KEY, confirmed_count INTEGER NOT NULL)')
db.execute("INSERT INTO checkpoints VALUES ('vendor', 0)")
db.commit()

batch = ['E7', 'TRANSFER-7']
try:
    db.execute('BEGIN')
    db.executemany('INSERT INTO events VALUES (?)', [(event_id,) for event_id in batch])
    stored = db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
    if stored != len(batch):
        raise ValueError('batch count mismatch')
    db.execute("UPDATE checkpoints SET confirmed_count = ? WHERE name = 'vendor'", (2,))
    db.commit()
except Exception:
    db.rollback()
    raise

print(db.execute('SELECT COUNT(*) FROM events').fetchone()[0])
print(db.execute("SELECT confirmed_count FROM checkpoints WHERE name = 'vendor'").fetchone()[0])
db.close()

Code

Use Tab to leave the editor. Ctrl+Enter runs the source. The lines beginning with # are task reminders. Python ignores them. Write your code beneath them.

Python console

Loading laboratory…

Explore Python or SQL here; this is not an operating-system shell.

This is a disposable browser experiment. The code run here is not recorded as a completed mission.

Each command starts a fresh disposable worker: it runs a captured copy of your current source, then your command with the lesson fixtures. Changes are discarded afterward. Commands never save source or award XP. Ctrl+Enter executes the command.

Virtual workspace files

Source and declarative assets are readable here without JavaScript. Python input files are created at runtime; SQL setup initializes the disposable database.

source.py

Current editor source. Inspection shows your current edits.

# Give the confirmed count after B1 and after B2 once each is truly checked.
# Describe the correct order of actions inside B1’s transaction.
# Give expected state if the program stops before or after B1’s `COMMIT`.
# Explain what can happen if the database keeps B1 while an external notebook still says 0.

exemple.py

Declarative lesson asset; this is not a file created in the Python filesystem.

import sqlite3

db = sqlite3.connect(':memory:')
db.execute('CREATE TABLE events (event_id TEXT)')
db.execute('CREATE TABLE checkpoints (name TEXT PRIMARY KEY, confirmed_count INTEGER NOT NULL)')
db.execute("INSERT INTO checkpoints VALUES ('vendor', 0)")
db.commit()

batch = ['E7', 'TRANSFER-7']
try:
    db.execute('BEGIN')
    db.executemany('INSERT INTO events VALUES (?)', [(event_id,) for event_id in batch])
    stored = db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
    if stored != len(batch):
        raise ValueError('batch count mismatch')
    db.execute("UPDATE checkpoints SET confirmed_count = ? WHERE name = 'vendor'", (2,))
    db.commit()
except Exception:
    db.rollback()
    raise

print(db.execute('SELECT COUNT(*) FROM events').fetchone()[0])
print(db.execute("SELECT confirmed_count FROM checkpoints WHERE name = 'vendor'").fetchone()[0])
db.close()