Remove backwards compat code: no installed base pre-1.0
All checks were successful
check / check (push) Successful in 1m49s

- 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:
clawbot
2026-03-17 19:47:59 -07:00
parent 3d92f2625b
commit 8b6aa74951
3 changed files with 2 additions and 103 deletions

View File

@@ -148,9 +148,7 @@ func collectMigrations() ([]string, error) {
}
// bootstrapMigrationsTable ensures the schema_migrations table exists
// by applying 000.sql directly. For databases that already have the
// table (created by older code), it records version "000" for
// consistency.
// by applying 000.sql if the table is missing.
func bootstrapMigrationsTable(ctx context.Context, db *sql.DB, log *slog.Logger) error {
var tableExists int
@@ -162,47 +160,9 @@ func bootstrapMigrationsTable(ctx context.Context, db *sql.DB, log *slog.Logger)
}
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
}
_, 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")
if err != nil {
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)
}
if log != nil {
log.Info("bootstrap migration applied successfully", "version", bootstrapVersion)
}
return nil
}

View File

@@ -222,58 +222,3 @@ func TestBootstrapMigrationsTable_FreshDatabase(t *testing.T) {
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")
}
}

View File

@@ -1,7 +1,5 @@
-- Migration 000: Schema migrations tracking table
-- This must be the first migration applied. The bootstrap logic in
-- database.go applies it directly (bypassing the normal migration
-- loop) when the schema_migrations table does not yet exist.
-- Applied as a bootstrap step before the normal migration loop.
CREATE TABLE IF NOT EXISTS schema_migrations (
version TEXT PRIMARY KEY,