Contributor · How-to
Add a database migration#
Change the schema with a step that upgrades every existing library on its next start. The rules behind each step are in Database schema.
- In
backend/db/schema_ddl.zig, add the next step after the last one, asddl_vN: the SQL that takes a version-(N−1) database to version N. Additive where possible: a new table, a nullable column, an index. Never edit an applied step,ddl_v1above all: a database that already ran it never runs it again, so the change would reach only new installs. - In
backend/db/schema.zig, name it (const ddl_vN = schema_ddl.ddl_vN;) and append it tosteps. The list’s length is the schema version, checked at compile time. - A column that stores a verdict (“not looked at yet” versus an answer) is
nullable,
NULLmeaning not looked at: the background worker’s jobs select exactly those rows (Indexing and derived data). - Test it in
backend/db/schema_test.zig: a fresh database and one migrated from the previous version must end up identical, and a step that moves data needs a test with data in it. - Run
make test. On the next start, every library runs the new step in one transaction with its version stamp, so a failed step leaves the database as it was.
A step that changes what an already-shipped step produced gets a new version of its own: folded into an applied one, it would never run.