Skip to content

Replace startup AutoMigrate with versioned database migrations - #1279

Merged
javuto merged 1 commit into
developfrom
versioned-database-migrations
Nov 29, 2026
Merged

javuto merged 1 commit into
developfrom
versioned-database-migrations

Conversation

@javuto

@javuto javuto commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Replace startup AutoMigrate with versioned database migrations

Summary

Replaces production schema creation through manager-constructor AutoMigrate calls with an ordered, ledger-backed migration system.

  • New pkg/migrations runner with schema_migrations, database locking, fingerprints, and optional Down.
  • New pkg/schema per-database gate so constructors still work in tests and auxiliary databases but become no-ops for the migrated primary database.
  • New osctrl-cli migrate --db command, including --down for reversible future migrations.
  • Startup aborts on migration failure.

Closes #1262.

Motivation

Every manager ran GORM AutoMigrate during 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/schema

A small per-*gorm.DB gate:

  • AutoMigrate(db, models...) runs normally for unmanaged databases.
  • MarkMigrated(db) marks the primary database after the migration runner succeeds.
  • Managed primary databases skip constructor AutoMigrate calls.
  • Tests and separate logging databases continue to auto-migrate as before.

pkg/migrations

  • Ordered registry of Migration{Version, Name, Up, Down, Fingerprint}.
  • schema_migrations ledger records version, name, applied time, and fingerprint.
  • Postgres advisory lock and MySQL GET_LOCK serialize replicas.
  • SQLite uses an in-process mutex, matching its normal single-process deployment.
  • Run applies pending migrations and aborts on the first failure.
  • Down reverts the most recent migration that defines a safe inverse.
  • Applied migrations with a changed fingerprint are rejected with:
changed after it was applied; add a new migration version

pkg/migrations/all

Central registry for every domain:

  • Core tables are always registered.
  • Feature-gated tables are registered only when enabled:
    • alerts
    • posture
    • vulnerabilities
    • Santa
    • health
  • Logging tables are always registered because per-environment DB sinks can be enabled without changing the global logger type.
  • Register(nil) (CLI mode) registers every feature migration.

Startup flow

osctrl-api and osctrl-tls now:

  1. Connect to the database.
  2. Register migrations for their enabled features.
  3. Run pending migrations.
  4. Mark primary and read-replica handles as schema-managed.
  5. Construct managers, whose AutoMigrate calls are now no-ops on the primary database.
  6. Abort startup if any migration fails.

CLI

osctrl-cli migrate --db
osctrl-cli migrate --db --down
  • migrate applies all registered migrations.
  • --down reverts the most recent reversible migration.
  • DB-mode guard tests are updated for the new command.

Manager refactor

Every manager now exposes a Migrate/MigrateFlags function and calls it from its constructor:

  • settings, users, environments, nodes, tags, queries, carves, logging
  • audit log, service config, log sinks, auth providers, MFA
  • console, file explorer, changes, extensions
  • node config, node flags, osquery versions
  • SCIM, service commands
  • posture, vulnerabilities, Santa, alerts, health

The constructors keep their existing signatures and behavior; only the schema-creation path changed.

Upgrade behavior

  • Existing installations are baselined by running the idempotent migrations once.
  • Missing tables and columns are created before managers start.
  • Feature-gated migrations are applied when the feature is enabled, preserving the historical “no tables when disabled” behavior.
  • Initial table-creation migrations are forward-only; Down is available for future migrations that define a safe inverse.
  • New columns/models in explicit migrations are guarded by schema fingerprints, forcing a new version instead of editing an applied migration.

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’s Migrate table set. This is the CI guard against registering a model in a package migration without adding it to the versioned registry.
  • CLI DB-only command allowlist updated.
  • Full existing test suite remains green.

Validation

  • go test ./... — passed.
  • git diff --check — clean.
  • No new Go dependencies.

Known limitations and follow-ups

  • The raw custom migrations (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.
  • SQLite has no cross-process advisory lock. Use Postgres or MySQL for multi-replica deployments.
  • Initial table-creation migrations are intentionally not reversible; Down is for future data migrations with safe inverses.
  • A future CI check could reject edits to any applied migration automatically by comparing generated schema fingerprints in a dry-run database.

@javuto javuto added refactor Refactorization of code backend Backend related issues labels Nov 29, 2026
@javuto
javuto merged commit c6e1a29 into develop Nov 29, 2026
8 checks passed
@javuto
javuto deleted the versioned-database-migrations branch November 29, 2026 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend related issues refactor Refactorization of code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace startup AutoMigrate with versioned database migrations

1 participant