// CONTENTS Part 08 · Data

// PART 08

Migrations

Chapter 34 / 60 2 min read English Part 08 of 15

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

  1. Write the migration: botserver/migrations/{version}-{feature_name}/up.sql following the existing numbering. Conventions in the tree: id UUID PRIMARY KEY DEFAULT gen_random_uuid(), created_at TIMESTAMPTZ NOT NULL DEFAULT NOW().

  2. Apply and regenerate the schema declarations:

    cd botserver && diesel migration run
    diesel print-schema
    
  3. Update the matching botschema module to match the real DB.

  4. Commit the migration and the botschema change 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 botserver is 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 botschema behind 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_migrations is 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

Found a mistake? Open an issue · All books

// SEARCH ESC
Pagefind · 236 pages · 4 books Enter to open ↑ ↓ to move