Database
Zavod keeps runtime state in a SQL database: sqlite by default, Postgres in production. The database is shared between zavod and the nomenklatura components it embeds:
| Tables | Schema defined in | Created by |
|---|---|---|
cache, resolver (nomenklatura) |
the component constructors | the components, on first use |
statement (nomenklatura) |
nomenklatura.db |
load_dataset_to_db |
position, program, review, review_entity (zavod) |
zavod/stateful/model.py |
Alembic migrations |
The nomenklatura tables are not managed by zavod's Alembic setup: they are created when missing by whoever uses them.
The zavod schema is versioned with Alembic: only the
migrations in zavod/alembic/ create or change these tables. Production runs
from repository checkouts, so the migrations live in the repository rather
than the installed package.
Running jobs affected by a migration will crash, restart, pull the new image, and block until the migration is complete. It's impractical to wait until all jobs finish - they start throughout the day and some run frequently.
We don't need to go to extreme lengths to make migrations backward compatible.
Using Alembic
From the zavod/ directory:
Migrations connect to the same database as zavod. For Postgres, extend statement
timeout by setting NOMENKLATURA_DB_STMT_TIMEOUT (0 to disable it).
Changing the schema
- Edit the table definitions in
zavod/stateful/model.py. - Autogenerate a revision and review the draft:
- Apply it with
alembic upgrade head.