Database migration strategy: versioned migrations replacing the AutoMigrate evolution
Status
Proposed (direction decided, landing in phases). Do not let production databases run AutoMigrate at startup ever again.
Decision
- Schema lifecycle is separated from ORM runtime responsibility: GORM does CRUD only, never schema evolution.
- Versioned migrations are the single source of truth for schema changes; migration files land in the repository under review.
- Because croupier has game databases created dynamically at runtime, we use a hybrid model:
- A brand-new game database created for the first time → the runtime bootstraps the database and applies the baseline version (once only);
- An existing database → catches up on pending migrations by version number;
- A version upgrade → an operator-triggered batch rolling tool covers N existing game databases.
- Startup only performs a compatibility check (database version ≥ the minimum version the program requires); on failure it refuses to start outright — it never silently ALTERs.
Background and current state (evidence)
The current implementation is the "GORM AutoMigrate + hand-written compensation hooks + scattered SQL" shape, and the compensation cost has already materialized:
internal/model/migration.go: before AutoMigrate,migrateEnumColumnsmust run first (the comment's own words: AutoMigrate would alter their types which would destroy values — i.e. skipping it destroys data), plusrenameLegacyTablesanddropLegacyPageUniqueIndexes; postgres also needs a constraint self-healing retry loop; afterwards there isCleanupAllLegacy. All of these are hand-written compensation layers that exist because there is no versioned migration, and they will persist forever and keep growing.internal/db/router/router.go: lazily creates physical game databases (game_<id>_<env>) at runtime by(game_id, env);MigrateGameruns on first open. singleflight only deduplicates within one process — multiple processes starting at the same time still issue concurrent ALTERs against the same table.migrations/001_openapi_schema.sql,002_function_openapi_backfill.sql: stray SQL files have already started to appear, with no version table and no unified executor.
Options compared
| Option | Assessment | Conclusion |
|---|---|---|
| GORM AutoMigrate | Convenient for bootstrapping new databases; but running it repeatedly against existing databases is dangerous: no audit, no rollback, unreviewable DDL, MDL locks on large tables, multi-instance races, schema/code drift | Reserved only for the brand-new database baseline |
| Atlas (versioned) | Diff-based migration generation, lint, test, and a complete CI ecosystem; supports MySQL/MariaDB/Postgres/SQLite | Preferred toolchain (usable for generating baselines and linting) |
| Goose | Mature, dual SQL + Go mode, well suited to complex data backfills | An acceptable alternative executor |
| golang-migrate | The classic pure versioned executor | Same as above |
The tool is not the point — the governance model is: migrations must be "versioned, committed to git, reviewable, and idempotently catch-up". Under that premise, choosing Atlas or Goose as the executor is equally fine.
What makes croupier special: dynamic game databases
The generic tutorial assumes the set of schema targets is known and finite, so CI can run against the target databases directly. Croupier's game databases are created only on first access; future game databases do not exist today, and CI cannot run migrations against them. Hence the hybrid model:
Brand-new database (first EnsureDatabase)
└─ Runtime bootstrap: CREATE DATABASE + apply the baseline version once (CREATE semantics only)
└─ Write the schema_migrations version record
Existing database (server startup / first access)
└─ Catch up unapplied versions per schema_migrations
└─ Cross-process lock: MySQL GET_LOCK / Postgres advisory lock (single-file sqlite has no such problem)
New version release
└─ Operator triggers the fan-out tool, batch-rolling every existing game database per the game_envs registryRules:
- AutoMigrate is only allowed inside the "detected a brand-new empty database" branch as the baseline generation mechanism; any database with existing data always goes through versioned migrations.
- Migration files are named
NNN_<description>.sql(paired where necessary with goose Go migrations for data backfill / dialect probing, e.g. 0002–0004), embedded into the binary so the fan-out tool works offline. - Every migration must be idempotent, or commit atomically in-transaction together with its version write; MySQL DDL is not transactional, so probe-style idempotency (HasTable/HasColumn probe, then execute) is the required shape.
- After modifying
internal/db/migrate/migrations/*.sql, runmake migrate-hashto updateatlas.sum; CI validates withatlas migrate validate.
Data change discipline (Expand [Contract]
When changing a field or its meaning, a one-step DROP is forbidden:
Expand Extend: ADD COLUMN a new column (with a default value)
Migrate Backfill: UPDATE to migrate old values (in batches for large tables, off-peak)
Switch Switch over: the code reads and writes both during the transition
Contract Contract: DROP the old column only after confirming all data has migratedmigrateEnumColumns is the existing hand-rolled example of this pattern; it should later be rewritten as a standard versioned migration entry instead of a startup hook.
Startup validation
At startup the Server/Agent reads the current version from schema_migrations:
- Database version < the program's minimum required version → print a clear error (
database schema version 181, required >= 182) and refuse to start; - No component may implicitly trigger DDL on the request path.
The boundary against "data regeneration"
Schema migration and data regeneration are two different problems; do not mix the tools.
- Column type / structure changes → the versioned migrations in this document.
- Business/spec content regeneration (e.g. after a page-generator upgrade, stale Title Case fallback labels remain in old PageProposals/PublishedPageSpecs) → the application layer triggers regeneration or cleanup keyed on a
generatorVersionbump; no DDL is written. - Published page snapshots are never changed silently; regeneration must be explicit (a generator version bump or an operator command), consistent with the existing "contract change → stale → republish" chain.
Phased landing
[Introduce the
schema_migrationsversion table and a unified executor (goose,internal/db/migrate, version tablegoose_db_version); AutoMigrate is narrowed to the brand-new empty database branch (a baseline bridge, one-shot only).][Startup compatibility validation (
MinimumRequiredVersion; after catch-up, below the minimum refuses to start); cross-process session locks (MySQLGET_LOCK/ PG advisory lock) cover the entire "version-table probe + baseline + catch-up" span, wired intoEnsureDatabase(PG database-creation race tolerance) andMigrateGame; single-database mode (multiGame: false) also runs throughEnsureUpToDateand no longer bare-runs AutoMigrate on every startup.][Existing compensation hooks (enum backfill, legacy table/column/index cleanup, openapi backfill) rewritten as numbered migrations 0002–0004 (goose Go migrations, registered in
internal/svc/migrations.go, sharing the same implementation as the baseline bridge); the hooks narrow from "run on every startup" to "baseline one-shot only". The stray root-level SQL files (001/002) are deleted (002 folded into 0002).][CI migration tests cover all four dialects (the
migrate-matrixjob inci.yml: real MySQL 8.4 + Postgres 16 + SQL Server 2022 services, including concurrent first-open lock verification; SQLite is covered by unit tests) and Atlas validation (the community-editionatlas migrate validatechecksatlas.sumand file ordering; note thatatlas migrate linthas been Pro-paid since v0.38, so rule-based linting is unavailable for now).]SQL Server JSON columns:
internal/model.JSON(jsontype.go) is the dialect-aware replacement fordatatypes.JSON— semantics aligned method by method (the mysql CAST in Value/Scan/MarshalJSON/GormValue), with one difference: theGormDBDataTypedefault branch returnsnvarchar(max)(datatypes.JSON returns empty for sqlserver, so the gorm driver falls back to the literaljson, a type SQL Server does not have).datatypes.JSONMapnatively includes the sqlserver branch and models keep using it. Session locks usesp_getapplock(note go-mssqldb does not rewrite?placeholders inside multi-statement batches; the resource name is inlined).[Fan-out batch rolling tool:
croupier-server db fanout(readsgame_envs, catches up the meta database and each game database in turn, and prints a report table;--dry-runonly reports versions without executing DDL; a database referenced by the registry but physically absent is markedmissing-database— runtime lazy creation is not an error).]
Review checklist
Check before merging:
rg -n 'AutoMigrate\(' internal cmd --glob '!**/*_test.go'A new AutoMigrate call must live in the "brand-new empty database bootstrap" branch, otherwise it is a review failure; any DDL change must come with a numbered migration file — editing only the model struct tags is not acceptable.
Known debt
Constructor-time AutoMigrate has been largely converged (the audit / metrics tables folded into the internal/svc baseline callback migrateAuxModels; constructors no longer execute DDL). The remaining AutoMigrate calls and their classification:
internal/model/migration*.go— the baseline bridge itself; compliant.internal/svc/service_context.go(autoMigrate / autoMigrateMeta / migrateAuxModels) — baseline callbacks; compliant.internal/platform/approvals/— theApprovalModeland workflow tables; the PG/SQLite stores use their own independent DSN (not the server meta database), so self-creating and self-managing is a reasonable shape.internal/platform/registry/agent_session_db.go— called explicitly by the svc baseline (the table structs live in the platform package to avoid a model→platform reverse dependency).internal/platform/monitoring/certificates/— the model is already in theMetaModelsbaseline; theStore.AutoMigratemethod remains as a test-fixture entry point.
