From 977c6b136e6bff1b1cb5d8ef25e4abd0fd37fd34 Mon Sep 17 00:00:00 2001 From: half-ogre Date: Sat, 21 Mar 2026 20:29:02 -0700 Subject: [PATCH] don't create the migrations table to list --- pgkit/migrator.go | 25 +++++++-------- pgkit/pgkit_test.go | 74 ++++++++++++++++++++++++++------------------- 2 files changed, 56 insertions(+), 43 deletions(-) diff --git a/pgkit/migrator.go b/pgkit/migrator.go index fbc1560..ebfac02 100644 --- a/pgkit/migrator.go +++ b/pgkit/migrator.go @@ -129,18 +129,6 @@ func (m *migrator) ListMigrations(db DB, dirPath string) ([]Migration, error) { migrationsFS := os.DirFS(dirPath) - // Create migrations tracking table if it doesn't exist - _, err := db.Exec(context.Background(), ` - CREATE TABLE IF NOT EXISTS pgkit_migrations ( - id SERIAL PRIMARY KEY, - filename VARCHAR(255) UNIQUE NOT NULL, - applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW() - ) - `) - if err != nil { - return nil, kit.WrapError(err, "failed to create pgkit_migrations table") - } - // Get all migration files from directory entries, err := fs.ReadDir(migrationsFS, ".") if err != nil { @@ -163,6 +151,19 @@ func (m *migrator) ListMigrations(db DB, dirPath string) ([]Migration, error) { return migrations[i].Version < migrations[j].Version }) + // Check if the migrations tracking table exists (don't create it — that's RunMigrations' job) + var tableExists bool + err = db.QueryRow(context.Background(), + "SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name = 'pgkit_migrations')").Scan(&tableExists) + if err != nil { + return nil, kit.WrapError(err, "failed to check for pgkit_migrations table") + } + + if !tableExists { + // No migrations have been run yet — return all as unapplied + return migrations, nil + } + // Get all applied migrations with timestamps rows, err := db.Query(context.Background(), "SELECT filename, applied_at FROM pgkit_migrations") if err != nil { diff --git a/pgkit/pgkit_test.go b/pgkit/pgkit_test.go index fcfb09d..1fa2c60 100644 --- a/pgkit/pgkit_test.go +++ b/pgkit/pgkit_test.go @@ -439,6 +439,17 @@ func TestListMigrationsFromDir(t *testing.T) { }) } +func tableExistsRow(exists bool) func(ctx context.Context, query string, args ...any) Row { + return func(ctx context.Context, query string, args ...any) Row { + return &FakeRow{ + ScanFake: func(dest ...any) error { + *dest[0].(*bool) = exists + return nil + }, + } + } +} + func TestListMigrations(t *testing.T) { t.Run("returns_all_migrations_from_directory_with_applied_status_and_timestamps", func(t *testing.T) { appliedTime := time.Date(2025, 1, 1, 12, 0, 0, 0, time.UTC) @@ -457,9 +468,7 @@ func TestListMigrations(t *testing.T) { ErrFake: func() error { return nil }, } fakeDB := &FakeDB{ - ExecFake: func(ctx context.Context, query string, args ...any) (sql.Result, error) { - return nil, nil - }, + QueryRowFake: tableExistsRow(true), QueryFake: func(ctx context.Context, query string, args ...any) (Rows, error) { return fakeRows, nil }, @@ -483,6 +492,20 @@ func TestListMigrations(t *testing.T) { assert.Nil(t, migrations[1].AppliedAt) }) + t.Run("returns_all_migrations_as_unapplied_when_table_does_not_exist", func(t *testing.T) { + fakeDB := &FakeDB{ + QueryRowFake: tableExistsRow(false), + } + + migrator := NewMigrator() + migrations, err := migrator.ListMigrations(fakeDB, "testdata") + + assert.NoError(t, err) + assert.Len(t, migrations, 2) + assert.False(t, migrations[0].Applied) + assert.False(t, migrations[1].Applied) + }) + t.Run("returns_migrations_sorted_by_version", func(t *testing.T) { fakeRows := &FakeRows{ NextFake: func() bool { return false }, @@ -490,9 +513,7 @@ func TestListMigrations(t *testing.T) { ErrFake: func() error { return nil }, } fakeDB := &FakeDB{ - ExecFake: func(ctx context.Context, query string, args ...any) (sql.Result, error) { - return nil, nil - }, + QueryRowFake: tableExistsRow(true), QueryFake: func(ctx context.Context, query string, args ...any) (Rows, error) { return fakeRows, nil }, @@ -526,10 +547,12 @@ func TestListMigrations(t *testing.T) { assert.EqualError(t, err, "directory path cannot be empty") }) - t.Run("returns_error_when_creating_migrations_table_fails", func(t *testing.T) { + t.Run("returns_error_when_checking_migrations_table_fails", func(t *testing.T) { fakeDB := &FakeDB{ - ExecFake: func(ctx context.Context, query string, args ...any) (sql.Result, error) { - return nil, assert.AnError + QueryRowFake: func(ctx context.Context, query string, args ...any) Row { + return &FakeRow{ + ScanFake: func(dest ...any) error { return assert.AnError }, + } }, } @@ -538,14 +561,12 @@ func TestListMigrations(t *testing.T) { assert.Nil(t, migrations) assert.Error(t, err) - assert.Contains(t, err.Error(), "failed to create pgkit_migrations table") + assert.Contains(t, err.Error(), "failed to check for pgkit_migrations table") }) t.Run("returns_error_when_directory_does_not_exist", func(t *testing.T) { fakeDB := &FakeDB{ - ExecFake: func(ctx context.Context, query string, args ...any) (sql.Result, error) { - return nil, nil - }, + QueryRowFake: tableExistsRow(true), } migrator := NewMigrator() @@ -558,9 +579,7 @@ func TestListMigrations(t *testing.T) { t.Run("returns_error_when_querying_applied_migrations_fails", func(t *testing.T) { fakeDB := &FakeDB{ - ExecFake: func(ctx context.Context, query string, args ...any) (sql.Result, error) { - return nil, nil - }, + QueryRowFake: tableExistsRow(true), QueryFake: func(ctx context.Context, query string, args ...any) (Rows, error) { return nil, assert.AnError }, @@ -581,9 +600,7 @@ func TestListMigrations(t *testing.T) { CloseFake: func() error { return nil }, } fakeDB := &FakeDB{ - ExecFake: func(ctx context.Context, query string, args ...any) (sql.Result, error) { - return nil, nil - }, + QueryRowFake: tableExistsRow(true), QueryFake: func(ctx context.Context, query string, args ...any) (Rows, error) { return fakeRows, nil }, @@ -604,9 +621,7 @@ func TestListMigrations(t *testing.T) { ErrFake: func() error { return assert.AnError }, } fakeDB := &FakeDB{ - ExecFake: func(ctx context.Context, query string, args ...any) (sql.Result, error) { - return nil, nil - }, + QueryRowFake: tableExistsRow(true), QueryFake: func(ctx context.Context, query string, args ...any) (Rows, error) { return fakeRows, nil }, @@ -621,17 +636,14 @@ func TestListMigrations(t *testing.T) { }) t.Run("returns_empty_list_when_directory_has_no_sql_files", func(t *testing.T) { - fakeRows := &FakeRows{ - NextFake: func() bool { return false }, - CloseFake: func() error { return nil }, - ErrFake: func() error { return nil }, - } fakeDB := &FakeDB{ - ExecFake: func(ctx context.Context, query string, args ...any) (sql.Result, error) { - return nil, nil - }, + QueryRowFake: tableExistsRow(true), QueryFake: func(ctx context.Context, query string, args ...any) (Rows, error) { - return fakeRows, nil + return &FakeRows{ + NextFake: func() bool { return false }, + CloseFake: func() error { return nil }, + ErrFake: func() error { return nil }, + }, nil }, }