Migration tests
Overview
A migration must preserve required data and constraints as well as change schema. Test from the actual previous schema and representative data, including an empty database and old records. Version ledgers are useful but not proof of successful migration.
First example
BEGIN;ALTER TABLE pt_account ADD COLUMN active INTEGER NOT NULL DEFAULT 1;SELECT id,active FROM pt_account ORDER BY id;ROLLBACK;SELECT COUNT(*) FROM pragma_table_info('pt_account') WHERE name='active';Observed SQLite result (rows in JSON):
[ [ 0 ]]How it works
Check defaults, backfill results, indexes and compatibility with old application versions. In SQLite the illustrated ALTER TABLE is transactional; not every DDL operation in every engine is. Run on disposable copies and test restore or forward repair if rollback is not supported. Never assume a migration is safe under concurrent production traffic.
Practical example
SELECT version FROM pt_migration ORDER BY version;Observed SQLite result (rows in JSON):
[ [ 1 ]]Practice and mistakes
Practice: add a non-null status with a default, verify old rows, then roll back. Hint 1: BEGIN before ALTER TABLE. Hint 2: inspect metadata after ROLLBACK. Mistake: testing only a fresh installation.