Test an additive SQLite migration on existing rows
The AI adds a priority field. The demo starts against a new database and everything looks perfect. Your users already have rows. Build a version-one fixture first, upgrade that same file, then try an old insert against the new schema. Now the review has something worth looking at.
- Before you start
- Python 3 with sqlite3 and a fresh directory. All commands work on the local jobs.db fixture.
- What to verify
- A pre-existing job survives, both writer versions can insert, and a second migration run exits cleanly.
Build yesterday’s database
Save the seed script. Job 41 is a witness from the old application. Keep its ID and name distinct so you can prove that the upgrade preserved an existing row. The user_version pragma stores the schema number chosen by this application. SQLite does not advance it automatically.
import sqlite3
with sqlite3.connect("jobs.db") as db:
db.execute("CREATE TABLE jobs (id INTEGER PRIMARY KEY, name TEXT NOT NULL)")
db.execute("INSERT INTO jobs(id, name) VALUES (?, ?)", (41, "Ship the button"))
db.execute("PRAGMA user_version = 1")Run the commands in the last step after saving all three files. The seed script is intentionally for a fresh fixture.
Upgrade only the version you recognise
Save migrate.py. Start the write transaction before checking the version. That keeps the decision and the change together. Add a non-null column with a concrete default and update the version within the same transaction. A repeat run sees version two and returns.
import sqlite3
def migrate(db):
with db:
db.execute("BEGIN IMMEDIATE")
version = db.execute("PRAGMA user_version").fetchone()[0]
if version == 2:
return
if version != 1:
raise ValueError(f"unsupported version {version}")
db.execute("ALTER TABLE jobs ADD COLUMN priority INTEGER NOT NULL DEFAULT 0")
db.execute("PRAGMA user_version = 2")
if __name__ == "__main__":
db = sqlite3.connect("jobs.db")
try:
migrate(db)
finally:
db.close()Here zero means ordinary priority. Pick a default that makes sense for the rows you already have. The transaction can preserve the schema while a poor default still changes the meaning of the data.
Exercise both generations of the writer
Save the verifier. First it looks for the original job by ID. Then it simulates an old client that inserts only a name, followed by a new client that supplies priority seven. Both use explicit column lists. This matters when a new column joins a table.
import sqlite3
with sqlite3.connect("jobs.db") as db:
assert db.execute("PRAGMA user_version").fetchone() == (2,)
assert db.execute("SELECT name, priority FROM jobs WHERE id=41").fetchone() == (
"Ship the button", 0
)
db.execute("INSERT INTO jobs(name) VALUES (?)", ("Old writer",))
db.execute("INSERT INTO jobs(name, priority) VALUES (?, ?)", ("New writer", 7))
print(db.execute("SELECT name, priority FROM jobs ORDER BY id").fetchall())An old client using INSERT INTO jobs VALUES with positional values for every column has a different contract. Include its exact statement in a compatibility test if that resembles your system.
Replay the migration before verification
Run these commands once in order. The migration gets invoked twice before the verifier inserts its sample rows. Look for the original name with zero, Old writer with zero, then New writer with seven. The second migration must never try to add the column again.
python3 seed_v1.py
python3 migrate.py
python3 migrate.py
python3 verify.pyFor another clean rehearsal, copy the scripts into a new empty directory. If the fixture already exists, keep it for inspection instead of reseeding over it.
Try this prompt with your agent
Review this change from the perspective of an old writer and an existing row. Show the exact statements that exercise each contract. Identify where the schema number is committed and what a retry does.A trap worth catching
Creating a fresh database from the new schema skips the upgrade path completely. A test that starts from version one and checks the old row can detect an accidental table rebuild that loses data.