Repository navigation
Replace startup AutoMigrate with versioned database migrations - #1279
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace startup AutoMigrate with versioned database migrations
Summary
Replaces production schema creation through manager-constructor
AutoMigratecalls with an ordered, ledger-backed migration system.pkg/migrationsrunner withschema_migrations, database locking, fingerprints, and optionalDown.pkg/schemaper-database gate so constructors still work in tests and auxiliary databases but become no-ops for the migrated primary database.osctrl-cli migrate --dbcommand, including--downfor reversible future migrations.Closes #1262.
Motivation
Every manager ran GORM
AutoMigrateduring construction. There was no migration history, no explicit rollout path, no rollback mechanism, and no way to detect that a schema change had already been applied. Model changes were production-impacting and rolling upgrades could observe mixed schemas.Design
pkg/schemaA small per-
*gorm.DBgate:AutoMigrate(db, models...)runs normally for unmanaged databases.MarkMigrated(db)marks the primary database after the migration runner succeeds.AutoMigratecalls.pkg/migrationsMigration{Version, Name, Up, Down, Fingerprint}.schema_migrationsledger records version, name, applied time, and fingerprint.GET_LOCKserialize replicas.Runapplies pending migrations and aborts on the first failure.Downreverts the most recent migration that defines a safe inverse.pkg/migrations/allCentral registry for every domain:
Register(nil)(CLI mode) registers every feature migration.Startup flow
osctrl-apiandosctrl-tlsnow:AutoMigratecalls are now no-ops on the primary database.CLI
migrateapplies all registered migrations.--downreverts the most recent reversible migration.Manager refactor
Every manager now exposes a
Migrate/MigrateFlagsfunction and calls it from its constructor:The constructors keep their existing signatures and behavior; only the schema-creation path changed.
Upgrade behavior
Downis available for future migrations that define a safe inverse.Tests
pkg/schema: gate, mark, forget, and auxiliary-database behavior.pkg/migrations: ordering, idempotency, failure handling,Down, and fingerprint mismatch.pkg/migrations/all: runs every migration on SQLite and compares the migrated table set with every package’sMigratetable set. This is the CI guard against registering a model in a package migration without adding it to the versioned registry.Validation
go test ./...— passed.git diff --check— clean.Known limitations and follow-ups
scim,posture,vulns,santa) use static fingerprints. Schema changes there still require a new migration version; the table-set guard does not inspect their internal model lists.Downis for future data migrations with safe inverses.