diff --git a/mothership/internal/db/migrate.go b/mothership/internal/db/migrate.go index 6a3c904..2aba06b 100644 --- a/mothership/internal/db/migrate.go +++ b/mothership/internal/db/migrate.go @@ -83,7 +83,12 @@ func (m *Migrator) Register(migrations ...Migration) { func (m *Migrator) CurrentVersion(ctx context.Context) (int, error) { m.mu.Lock() defer m.mu.Unlock() + return m.currentVersionLocked(ctx) +} +// currentVersionLocked returns the current schema version without acquiring the lock. +// The caller must hold m.mu. +func (m *Migrator) currentVersionLocked(ctx context.Context) (int, error) { // Check if schema_migrations table exists var tableName string err := m.db.QueryRowContext(ctx, @@ -114,7 +119,7 @@ func (m *Migrator) Pending(ctx context.Context) ([]Migration, error) { m.mu.Lock() defer m.mu.Unlock() - current, err := m.CurrentVersion(ctx) + current, err := m.currentVersionLocked(ctx) if err != nil { return nil, err } @@ -135,7 +140,7 @@ func (m *Migrator) Migrate(ctx context.Context) error { m.mu.Lock() defer m.mu.Unlock() - current, err := m.CurrentVersion(ctx) + current, err := m.currentVersionLocked(ctx) if err != nil { return fmt.Errorf("get current version: %w", err) } diff --git a/mothership/internal/db/migrate_test.go b/mothership/internal/db/migrate_test.go index 1178b5a..9c87fa1 100644 --- a/mothership/internal/db/migrate_test.go +++ b/mothership/internal/db/migrate_test.go @@ -130,7 +130,7 @@ func TestMigrateFromV1(t *testing.T) { updated_at INTEGER NOT NULL ); - INSERT INTO auth (id, install_secret) VALUES (1, X'0000000000000000000000000000000000000000000000000000000000000000'); + INSERT INTO auth (id, install_secret, updated_at) VALUES (1, X'0000000000000000000000000000000000000000000000000000000000000000', strftime('%s', 'now') * 1000); INSERT INTO schema_migrations (version, applied_at, description) VALUES (1, strftime('%s', 'now') * 1000, 'initial schema'); diff --git a/mothership/internal/db/migrations.go b/mothership/internal/db/migrations.go index 87cf5e8..10c50b2 100644 --- a/mothership/internal/db/migrations.go +++ b/mothership/internal/db/migrations.go @@ -3,8 +3,6 @@ package db import ( "database/sql" - "fmt" - "time" ) // AllMigrations returns the complete list of schema migrations in order.