5.5 KiB
MySQL migration baseline
Status: DEV schemas are implemented through version 8 (local releases and digest-object locking). Formal entitlement policy acceptance and seed data remain pending. Read-only verification on 2026-09-30: business license_platform is version=7 dirty=false; isolated license_download_260930 is version=8 dirty=false. No business database migration was executed during the deployment-delivery slice.
Version 007 to 008
007 stores release metadata; 008 adds package_objects digest rows used for SELECT FOR UPDATE serialization and backfills them from existing releases using MAX(size_bytes). Before applying 008, quiesce uploads, back up database and private files, and verify every digest has a single consistent size and an existing matching object; MAX is not an integrity check or repair. Explicit up must finish clean before using the new publication code. Historical migrations are unchanged. 008 down drops the lock inventory and must not be used while new upload handlers are running; 007 down destroys release metadata. Do not run either against business data without backup and approval.
A file is linked into its digest path before the metadata/audit transaction commits. Ordinary publication failures remove only files linked by that request, preserving pre-existing shared digest objects. Commit acknowledgement failure retains content because commit may have succeeded. Crashes can leave temporary fragments or unreferenced digest files; SQL and filesystem are not one atomic transaction. Orphan reconciliation/automatic GC is NOT implemented.
Future reconciliation must quiesce publishers or participate in the same digest locks, inventory product_releases/package_objects and private files, enforce a safety age window, recheck references immediately before quarantine, and verify checksums. Never remove a file based only on a missing row observed during an active upload or an uncertain commit. Keep quarantine recoverable and require reviewed deletion. U07 chunk/resume and crash cleanup remain open.
Version 6 adds independent lease/last-seen fields and heartbeat/freeze operation records. It was explicitly applied to the dedicated MySQL integration database on 2026-09-30. Its down migration drops operation history and lease state; do not execute against existing business data without backup and approval. The v6 isolated down/up rehearsal is still pending. Startup never applies migrations.
Use versioned pairs named 000001_description.up.sql and 000001_description.down.sql. Never edit an applied migration; add a new version. Seed permissions with stable unique codes, never commit an administrator password. SQL targets MySQL 8.0 with UTC timestamps and utf8mb4.
MySQL DDL may commit implicitly. Do not assume a failed schema migration is transactionally reversible. Before release, run the chosen migration runner against an empty MySQL 8 database, test each down/up pair with fixtures, and record dirty-version recovery. Down migrations that remove business data require backup and explicit operator confirmation.
The runner is go run ./cmd/migrate from backend. It accepts a golang-migrate MySQL URL such as mysql://user:password@tcp(host:3306)/database?multiStatements=true or MIGRATION_DSN. Supported actions are up, down, version, and explicit force -version N (flags must precede the action); force is only for an operator-reviewed dirty migration and never runs automatically.
go run ./cmd/migrate -dsn "$MIGRATION_DSN" up
go run ./cmd/migrate -dsn "$MIGRATION_DSN" version
go run ./cmd/migrate -dsn "$MIGRATION_DSN" -version 12 force
``` The server does not run migrations on startup.
On 2026-09-30, a dedicated MySQL 8.0 container was used to verify an empty database up/down cycle and an intentionally invalid migration: failure left version 2 dirty, normal `up` was blocked, and explicit force to the last known-good version restored clean metadata. The probe migrations and container were removed after the test. Version 3 adds transactional license operation idempotency records for revoke/renew; it must be applied explicitly after version 2. No placeholder SQL is considered an executed business migration; formal entitlement schemas and CI database integration remain T04/T03 acceptance work.
Schema 10 adds unique administrator usernames. Existing accounts become `admin-{id}` and retain their passwords; login requests must send `username` instead of `email`. Run migrations before starting the current server or `admin-init`.
Schema 11 adds nullable `license_instances.secret_encrypted`. Apply before API startup. Configure `LICENSE_CREDENTIAL_ENCRYPTION_KEY_FILE` as an absolute path to an independent 32-byte raw random key. Mount read-only, back up securely with the database, and never commit it. Missing key preserves first-return-only creation. Existing hashes cannot recover secrets. Losing/changing the key makes ciphertext unreadable; rotation is not implemented. Down migration deletes retained ciphertext and requires backup and confirmation.
Schema 12 adds `product_update_policies`, one policy per product/platform/architecture/channel. Apply before enabling the update-policy administrator UI or `/instances/:instance_id/updates/check`. Only published stable-SemVer releases can be policy targets; the minimum supported version cannot exceed the target. The update response is signed with the configured current P-256 key and expires after 15 minutes. Down migration deletes policy rows and requires an operator-reviewed backup/confirmation. Existing portal snapshot and download APIs remain compatible.