Remove backwards compat code: no installed base pre-1.0
- Simplify bootstrapMigrationsTable to just check table existence and apply 000.sql if missing — no legacy DB detection - Remove ensureBootstrapVersionRecorded (legacy backfill path) - Remove applyBootstrapMigration (separate helper, now inlined) - Remove TestBootstrapMigrationsTable_ExistingTableBackwardsCompat - Simplify 000.sql header comment
This commit is contained in:
@@ -148,9 +148,7 @@ func collectMigrations() ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// bootstrapMigrationsTable ensures the schema_migrations table exists
|
// bootstrapMigrationsTable ensures the schema_migrations table exists
|
||||||
// by applying 000.sql directly. For databases that already have the
|
// by applying 000.sql if the table is missing.
|
||||||
// table (created by older code), it records version "000" for
|
|
||||||
// consistency.
|
|
||||||
func bootstrapMigrationsTable(ctx context.Context, db *sql.DB, log *slog.Logger) error {
|
func bootstrapMigrationsTable(ctx context.Context, db *sql.DB, log *slog.Logger) error {
|
||||||
var tableExists int
|
var tableExists int
|
||||||
|
|
||||||
@@ -162,47 +160,9 @@ func bootstrapMigrationsTable(ctx context.Context, db *sql.DB, log *slog.Logger)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if tableExists > 0 {
|
if tableExists > 0 {
|
||||||
return ensureBootstrapVersionRecorded(ctx, db, log)
|
|
||||||
}
|
|
||||||
|
|
||||||
return applyBootstrapMigration(ctx, db, log)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ensureBootstrapVersionRecorded checks whether version "000" is already
|
|
||||||
// recorded in an existing schema_migrations table and inserts it if not.
|
|
||||||
func ensureBootstrapVersionRecorded(ctx context.Context, db *sql.DB, log *slog.Logger) error {
|
|
||||||
var recorded int
|
|
||||||
|
|
||||||
err := db.QueryRowContext(ctx,
|
|
||||||
"SELECT COUNT(*) FROM schema_migrations WHERE version = ?",
|
|
||||||
bootstrapVersion,
|
|
||||||
).Scan(&recorded)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to check bootstrap migration status: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if recorded > 0 {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = db.ExecContext(ctx,
|
|
||||||
"INSERT INTO schema_migrations (version) VALUES (?)",
|
|
||||||
bootstrapVersion,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to record bootstrap migration: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if log != nil {
|
|
||||||
log.Info("recorded bootstrap migration for existing table", "version", bootstrapVersion)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// applyBootstrapMigration reads and executes 000.sql to create the
|
|
||||||
// schema_migrations table on a fresh database.
|
|
||||||
func applyBootstrapMigration(ctx context.Context, db *sql.DB, log *slog.Logger) error {
|
|
||||||
content, err := schemaFS.ReadFile("schema/000.sql")
|
content, err := schemaFS.ReadFile("schema/000.sql")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to read bootstrap migration 000.sql: %w", err)
|
return fmt.Errorf("failed to read bootstrap migration 000.sql: %w", err)
|
||||||
@@ -225,10 +185,6 @@ func applyBootstrapMigration(ctx context.Context, db *sql.DB, log *slog.Logger)
|
|||||||
return fmt.Errorf("failed to record bootstrap migration: %w", err)
|
return fmt.Errorf("failed to record bootstrap migration: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if log != nil {
|
|
||||||
log.Info("bootstrap migration applied successfully", "version", bootstrapVersion)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -222,58 +222,3 @@ func TestBootstrapMigrationsTable_FreshDatabase(t *testing.T) {
|
|||||||
t.Errorf("expected version 000 to be recorded, got count %d", recorded)
|
t.Errorf("expected version 000 to be recorded, got count %d", recorded)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBootstrapMigrationsTable_ExistingTableBackwardsCompat(t *testing.T) {
|
|
||||||
db := openTestDB(t)
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
// Simulate an older database that created the table via inline SQL
|
|
||||||
// (without recording version "000").
|
|
||||||
_, err := db.Exec(`
|
|
||||||
CREATE TABLE schema_migrations (
|
|
||||||
version TEXT PRIMARY KEY,
|
|
||||||
applied_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
||||||
)
|
|
||||||
`)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create legacy table: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert a fake migration to prove the table already existed.
|
|
||||||
_, err = db.Exec("INSERT INTO schema_migrations (version) VALUES ('001')")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to insert legacy version: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := bootstrapMigrationsTable(ctx, db, nil); err != nil {
|
|
||||||
t.Fatalf("bootstrapMigrationsTable failed: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Version "000" must now be recorded.
|
|
||||||
var recorded int
|
|
||||||
|
|
||||||
err = db.QueryRow(
|
|
||||||
"SELECT COUNT(*) FROM schema_migrations WHERE version = '000'",
|
|
||||||
).Scan(&recorded)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to check version: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if recorded != 1 {
|
|
||||||
t.Errorf("expected version 000 to be recorded for legacy DB, got count %d", recorded)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The existing "001" row must still be there.
|
|
||||||
var legacyCount int
|
|
||||||
|
|
||||||
err = db.QueryRow(
|
|
||||||
"SELECT COUNT(*) FROM schema_migrations WHERE version = '001'",
|
|
||||||
).Scan(&legacyCount)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to check legacy version: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if legacyCount != 1 {
|
|
||||||
t.Errorf("legacy version 001 row missing after bootstrap")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
-- Migration 000: Schema migrations tracking table
|
-- Migration 000: Schema migrations tracking table
|
||||||
-- This must be the first migration applied. The bootstrap logic in
|
-- Applied as a bootstrap step before the normal migration loop.
|
||||||
-- database.go applies it directly (bypassing the normal migration
|
|
||||||
-- loop) when the schema_migrations table does not yet exist.
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||||
version TEXT PRIMARY KEY,
|
version TEXT PRIMARY KEY,
|
||||||
|
|||||||
Reference in New Issue
Block a user