// PART 08
Migrations
Migrations are Diesel migrations under botserver/migrations/ — 211 of them
at the time of writing, named {version}-{feature} (for example
6.8.9-browser-audit). The applied set is tracked in
__diesel_schema_migrations; the reference generator reads both the migrations
and botschema/src/**:
python3 scripts/gen_schema_reference.py # regenerate the schema reference
The workflow for changing the schema
-
Write the migration:
botserver/migrations/{version}-{feature_name}/up.sqlfollowing the existing numbering. Conventions in the tree:id UUID PRIMARY KEY DEFAULT gen_random_uuid(),created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(). -
Apply and regenerate the schema declarations:
cd botserver && diesel migration run diesel print-schema -
Update the matching
botschemamodule to match the real DB. -
Commit the migration and the
botschemachange together. There is no guard script for this today; the rule is enforced in review.
How migrations run in production
The server applies pending migrations during bootstrap, before serving. What the operator needs to know:
# Which migrations ran, and when?
psql -h localhost -U postgres -d botserver -c "SELECT version, run_on FROM __diesel_schema_migrations ORDER BY run_on DESC LIMIT 5;"
# A failed migration leaves the process retrying at boot — read, never re-run blindly:
sudo incus exec bot -- tail -200 /opt/gbo/logs/stdout.log | grep -iE "migration|diesel"
The rules that keep migrations safe
- A binary rollback never rolls back a migration. If the release included one, the old binary must be able to boot against the new schema, or you restore the database (see Backup and restore).
- Dump before touching the schema.
pg_dump -U postgres -F c -f /tmp/pre-migration.dump botserveris the runbook’s first step for a failed migration. - No 2 a.m. schema migrations unless the migration is the fix for the incident and the backup restore path has been verified.
- Feature-gated tables are declared in
botschemabehind the matching Cargo feature (e.g.#[cfg(feature = "billing")]), so a minimal build does not compile tables it cannot use. - The restore drill (
scripts/restore_drill.sh) asserts__diesel_schema_migrationsis non-empty on any restored copy — a restore that lost the migration history fails the drill.
Migration hygiene checklist
| Check | Command |
|---|---|
| Migration list matches the reference | python3 scripts/gen_schema_reference.py --check (CI owns this) |
| Schema declarations match the DB | diesel print-schema diff after every run |
| Applied set sane before upgrade | the SELECT … FROM __diesel_schema_migrations above |
| Backup exists before upgrade | pg_dump -F c output present with a manifest |