Product case study · November 30, 2026
Ask Claude Code to fix a SQLite backup that loses recent rows
Reproduce a SQLite backup that opens but misses committed rows, ask Claude Code for a scoped repair, and verify the restored data with Python.

A backup file that opens is a weak success test. Query the records you expect to recover before trusting the backup job.
The backup file exists. SQLite opens it. The two tasks you saved are gone.
That is a particularly convincing kind of broken backup: everything around the data looks healthy. In SQLite’s write-ahead logging mode, committed changes can still be in the -wal file beside the main database. Copying just the main file can leave those changes behind. SQLite explains this split in its WAL documentation.
This lab gives Claude Code a better target than “make a backup”: recover the expected records while the source connection remains open. I would put that requirement ahead of file size, timestamps or a cheerful success message.
Use Python 3.10 or newer with sqlite3, Bash on Linux, macOS or WSL, and an installed, signed-in Claude Code. Work on a local disk in the disposable folder below. All records are synthetic. The reproduction and reference repair were run with Python 3.14.4 and SQLite 3.46.1; the Claude prompt is an authored exercise, not a recorded agent session. Official sources were checked November 30, 2026.
Build a backup that looks finished
Create a practice folder and a deliberately incomplete implementation:
backup_lab=$(mktemp -d /tmp/claude-backup.XXXXXX)
cd "$backup_lab"
cat > backup_job.py <<'PY'
from shutil import copyfile
def save_backup(source_path, destination_path):
copyfile(source_path, destination_path)
PY
Save the following as verify_backup.py. Each run creates fresh database files in its own temporary directory, so a previous run cannot supply the missing rows.
from contextlib import closing
from pathlib import Path
import sqlite3
from tempfile import TemporaryDirectory
from backup_job import save_backup
with TemporaryDirectory() as folder:
source_path = Path(folder) / "tasks.db"
destination_path = Path(folder) / "backup.db"
with closing(sqlite3.connect(source_path)) as source:
source.execute("CREATE TABLE tasks (id INTEGER PRIMARY KEY, title TEXT)")
source.commit()
mode = source.execute("PRAGMA journal_mode=WAL").fetchone()[0]
if mode != "wal":
raise RuntimeError(f"This lab requires WAL mode; got {mode}")
source.execute("PRAGMA wal_autocheckpoint=0")
expected = [(1, "Draft briefing"), (2, "Review export")]
source.executemany("INSERT INTO tasks VALUES (?, ?)", expected)
source.commit()
save_backup(source_path, destination_path)
with closing(sqlite3.connect(destination_path)) as restored:
actual = restored.execute(
"SELECT id, title FROM tasks ORDER BY id"
).fetchall()
print("Recovered:", actual)
if actual != expected:
raise RuntimeError(f"Expected {expected!r}, recovered {actual!r}")
unchanged = source.execute(
"SELECT id, title FROM tasks ORDER BY id"
).fetchall()
if unchanged != expected:
raise RuntimeError("The backup job changed the source records")
print("PASS: both records recovered; source records unchanged")
Run it:
python3 verify_backup.py
The original function prints Recovered: [] and raises RuntimeError. The table was created before switching to WAL mode, so this copy can open and query the table while missing both inserted rows. That is the failure we want the agent to preserve as evidence.
The fixture disables automatic checkpointing and leaves the source connection open to keep the demonstration reproducible. A checkpoint transfers WAL changes into the main database file; closing the last connection can also trigger cleanup. Those details explain why a casual copy test after shutting down an app can miss this bug. They are documented in SQLite’s WAL guide.
Give Claude Code the recovery requirement
Start claude in the practice folder. If you need installation or session guidance, use the Claude Code project lesson.
Paste this:
Read backup_job.py and verify_backup.py. Run the verification script
before editing and explain why the destination opens but has no rows.
Change only backup_job.py. Keep save_backup(source_path, destination_path).
Use Python's sqlite3 backup API to copy the logical database into the
fresh destination file. Close every connection your function opens.
Do not change source records or journal settings, delete sidecar files,
or edit the verification script. Do not install packages, access real
app databases, commit files or delegate.
The lab supplies an existing source and a fresh, distinct destination
on a local disk. There are no concurrent writers during this exercise.
Rerun the unchanged verification and show the output and final function.
This follows Anthropic’s debugging workflow: establish the failure, make a focused repair, and verify it. The edit restriction is a task instruction; review the session’s actual tool permissions separately.
Be suspicious of a patch that closes the fixture’s source early or adds a checkpoint to the test. That makes the easy case pass by removing the condition the backup needs to handle.
Replace the file copy with a database copy
A reference repair is:
from contextlib import closing
import sqlite3
def save_backup(source_path, destination_path):
with closing(sqlite3.connect(source_path)) as source:
with closing(sqlite3.connect(destination_path)) as destination:
source.backup(destination)
Python’s Connection.backup() documentation describes copying into a destination connection, including while other clients access the source. SQLite’s Online Backup API provides a database snapshot instead of relying on an external copy of one file.
closing() matters here: the Python SQLite connection context manager manages transactions but does not itself close a connection. The connection context-manager documentation makes that distinction explicit.
Exit Claude and run python3 verify_backup.py yourself. With the reference repair, the local output was:
Recovered: [(1, 'Draft briefing'), (2, 'Review export')]
PASS: both records recovered; source records unchanged
Open both files and confirm the verification still checks both task IDs and titles. This test proves recovery for the fixture; it does not test a busy production service, interrupted writes or storage failure.
When the result still looks wrong
If you get an empty list, check whether save_backup still calls a file-copy utility. If the baseline unexpectedly passes, confirm that the fixture still disables automatic checkpointing and calls the backup before closing the source. If you see “no such table,” check the source path: sqlite3.connect() can create a new empty database when the named file does not exist.
For a real service, use its supported backup procedure first. This small function assumes an existing source and a fresh destination; it has no overwrite policy, rotation, missing-source guard or failure cleanup. Add those requirements before adapting it into a scheduled job. Never “repair” the live database by deleting its WAL file, which SQLite treats as part of its persistent state.
The useful habit is simple: reopen the backup and ask it a question whose answer you already know. A file appearing in the backup folder cannot answer that question for you.
Sources
Official documentation checked November 30, 2026:
- SQLite: Write-Ahead Logging — committed changes, checkpoints and WAL-file persistence.
- SQLite: Online Backup API — copying a database through SQLite.
- Python: sqlite3 —
backup(), connections and context-manager behavior. - Anthropic: Common workflows — focused debugging and verification prompts.
Put this to work
Why a live SQLite database can contain committed data outside its main file.
Try
Run a disposable Python backup lab with two synthetic task records.
Prove it worked
Keep the verification script unchanged and recover both records from the backup.
Keep in view
- Reproduce a file copy that misses committed SQLite records in WAL mode.
- Ask Claude Code to replace the copy with SQLite's backup API.
- Reopen the destination and check its rows while the source stays open.