From a883d708a26bdbdf1b7f96ac9aa3c2523c2e456f Mon Sep 17 00:00:00 2001 From: user Date: Wed, 25 Mar 2026 14:41:46 -0700 Subject: [PATCH] feat: move schema_migrations table creation into 000_bootstrap.sql Move the inline CREATE TABLE IF NOT EXISTS schema_migrations from Go code into a dedicated 000_bootstrap.sql file, following the sneak/pixa pattern. The bootstrap SQL is executed directly before the migration loop, which now starts from 001+. The bootstrap file also handles its own INSERT OR IGNORE so the Go code does zero inserts for version 0. closes #91 Co-Authored-By: Claude Opus 4.6 (1M context) --- internal/db/db.go | 23 ++++++++++++++++++----- internal/db/schema/000_bootstrap.sql | 6 ++++++ 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 internal/db/schema/000_bootstrap.sql diff --git a/internal/db/db.go b/internal/db/db.go index 074f365..7066c27 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -135,13 +135,21 @@ type migration struct { func (database *Database) runMigrations( ctx context.Context, ) error { - _, err := database.conn.ExecContext(ctx, - `CREATE TABLE IF NOT EXISTS schema_migrations ( - version INTEGER PRIMARY KEY, - applied_at DATETIME DEFAULT CURRENT_TIMESTAMP)`) + bootstrap, err := SchemaFiles.ReadFile( + "schema/000_bootstrap.sql", + ) if err != nil { return fmt.Errorf( - "create schema_migrations: %w", err, + "read bootstrap migration: %w", err, + ) + } + + _, err = database.conn.ExecContext( + ctx, string(bootstrap), + ) + if err != nil { + return fmt.Errorf( + "execute bootstrap migration: %w", err, ) } @@ -270,6 +278,11 @@ func (database *Database) loadMigrations() ( continue } + // Skip bootstrap migration; it is executed separately. + if version == 0 { + continue + } + content, readErr := SchemaFiles.ReadFile( "schema/" + entry.Name(), ) diff --git a/internal/db/schema/000_bootstrap.sql b/internal/db/schema/000_bootstrap.sql new file mode 100644 index 0000000..612d626 --- /dev/null +++ b/internal/db/schema/000_bootstrap.sql @@ -0,0 +1,6 @@ +-- Bootstrap: create the schema_migrations table itself. +CREATE TABLE IF NOT EXISTS schema_migrations ( + version INTEGER PRIMARY KEY, + applied_at DATETIME DEFAULT CURRENT_TIMESTAMP +); +INSERT OR IGNORE INTO schema_migrations (version) VALUES (0);