From 8b6aa74951533fee89f4cf0c0aa09094011f93ed Mon Sep 17 00:00:00 2001 From: clawbot Date: Tue, 17 Mar 2026 19:47:59 -0700 Subject: [PATCH] Remove backwards compat code: no installed base pre-1.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- internal/database/database.go | 46 +------------------------ internal/database/database_test.go | 55 ------------------------------ internal/database/schema/000.sql | 4 +-- 3 files changed, 2 insertions(+), 103 deletions(-) diff --git a/internal/database/database.go b/internal/database/database.go index b724b89..0b38b64 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -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 } diff --git a/internal/database/database_test.go b/internal/database/database_test.go index ede0d1c..46983c3 100644 --- a/internal/database/database_test.go +++ b/internal/database/database_test.go @@ -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") - } -} diff --git a/internal/database/schema/000.sql b/internal/database/schema/000.sql index c05b915..5999fad 100644 --- a/internal/database/schema/000.sql +++ b/internal/database/schema/000.sql @@ -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,