// PART 05
Migrations
Schema changes travel through Diesel migrations that run at boot. This page is the working contract for writing one.
Where migrations live
botserver/migrations/ — currently 211 migration directories, named
{version}-{feature_name}/ with up.sql (and down.sql where reversible).
Examples: 6.8.9-browser-audit, 6.5.61-drive-public-links,
6.9.9-sales-document-deal-link.
The schema reference is generated from botschema/src/** (table!,
joinable!) and these migration files (primary keys, nullability, defaults,
indexes, REFERENCES). Regenerate with:
python3 scripts/gen_schema_reference.py
The workflow (mandatory order)
- Write the migration — new directory under
botserver/migrations/, following the existing numbering:id UUID PRIMARY KEY DEFAULT gen_random_uuid(),created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(). - Apply locally —
cd botserver && diesel migration run. - Read the real schema —
diesel print-schema. - Update
botschema— adjust the matching module (newtable!, type changes, newjoinable!/group entries) so it matches the database. - Commit together — the migration and the
botschemachange land in the same commit.
There is no automated guard for “table! outside botschema” yet; the rule is
enforced in review. If you find one, move it and re-export
(pub use botschema::<table>; keeps crate::schema::<table>::dsl call sites
working).
Feature-gated tables
A table that belongs to a feature crate sits behind that crate’s feature in
botschema (#[cfg(feature = "billing")], and so on). Consumers enable the
feature in their Cargo.toml:
botschema = { workspace = true, features = ["people", "billing"] }
What runs when
- Boot:
main_module::migrations.rsruns pending migrations before the server accepts traffic — a fresh database migrates itself on first start. - Desktop edition: the bundled PostgreSQL migrates the same way; the stack path differs, the mechanism does not.
- Down migrations: used for local rollbacks; boot only goes forward.
Rules that avoid pain
| Rule | Why |
|---|---|
| Never edit an applied migration | boot already ran it; make a new one |
Never declare table! outside botschema |
single source of truth (#1497) |
| Keep the Rust type and the SQL type in agreement | a mismatching Queryable struct is a runtime bug, not a compile error |
| Back up before risky migrations | pg_dump -U postgres -F c -f /tmp/backup.dump <db> |
| Test on a fresh boot | rm -rf the dev data dir (or use a scratch database) and start the server once |
Verifying after a migration
diesel migration run # applies
diesel print-schema | grep <new_table> # schema visible
cargo test -p <affected crate> # types still line up
# restart botserver: boot must be clean, no migration errors in botserver.log