fix(migrations): derive migration counts dynamically in tests

Hardcoded counts (5) broke CI when migrations 0006 and 0007 were added.
Use readMigrations() to compute expected values so future migrations
don't require test updates.
This commit is contained in:
Houmgaor
2026-03-17 19:33:11 +01:00
parent 197164bc94
commit 6b5bbf6d0b

View File

@@ -58,20 +58,27 @@ func TestMigrateEmptyDB(t *testing.T) {
logger, _ := zap.NewDevelopment() logger, _ := zap.NewDevelopment()
allMigrations, err := readMigrations()
if err != nil {
t.Fatalf("readMigrations failed: %v", err)
}
wantCount := len(allMigrations)
wantVersion := allMigrations[wantCount-1].version
applied, err := Migrate(db, logger) applied, err := Migrate(db, logger)
if err != nil { if err != nil {
t.Fatalf("Migrate failed: %v", err) t.Fatalf("Migrate failed: %v", err)
} }
if applied != 5 { if applied != wantCount {
t.Errorf("expected 5 migrations applied, got %d", applied) t.Errorf("expected %d migrations applied, got %d", wantCount, applied)
} }
ver, err := Version(db) ver, err := Version(db)
if err != nil { if err != nil {
t.Fatalf("Version failed: %v", err) t.Fatalf("Version failed: %v", err)
} }
if ver != 5 { if ver != wantVersion {
t.Errorf("expected version 5, got %d", ver) t.Errorf("expected version %d, got %d", wantVersion, ver)
} }
} }
@@ -103,10 +110,18 @@ func TestMigrateExistingDBWithoutSchemaVersion(t *testing.T) {
logger, _ := zap.NewDevelopment() logger, _ := zap.NewDevelopment()
allMigrations, err := readMigrations()
if err != nil {
t.Fatalf("readMigrations failed: %v", err)
}
// Baseline (0001) is auto-marked, remaining are applied
wantApplied := len(allMigrations) - 1
wantVersion := allMigrations[len(allMigrations)-1].version
// Simulate an existing database that has the full 0001 schema applied // Simulate an existing database that has the full 0001 schema applied
// but no schema_version tracking yet (pre-migration-system installs). // but no schema_version tracking yet (pre-migration-system installs).
// First, run all migrations normally to get the real schema... // First, run all migrations normally to get the real schema...
_, err := Migrate(db, logger) _, err = Migrate(db, logger)
if err != nil { if err != nil {
t.Fatalf("Initial Migrate failed: %v", err) t.Fatalf("Initial Migrate failed: %v", err)
} }
@@ -117,22 +132,21 @@ func TestMigrateExistingDBWithoutSchemaVersion(t *testing.T) {
} }
// Migrate should detect existing DB and auto-mark baseline, // Migrate should detect existing DB and auto-mark baseline,
// then apply remaining migrations (0002-0005). // then apply remaining migrations.
applied, err := Migrate(db, logger) applied, err := Migrate(db, logger)
if err != nil { if err != nil {
t.Fatalf("Migrate failed: %v", err) t.Fatalf("Migrate failed: %v", err)
} }
// Baseline (0001) is auto-marked, then 0002-0005 are applied = 4 if applied != wantApplied {
if applied != 4 { t.Errorf("expected %d migrations applied (baseline auto-marked, rest applied), got %d", wantApplied, applied)
t.Errorf("expected 4 migrations applied (baseline auto-marked, 0002-0005 applied), got %d", applied)
} }
ver, err := Version(db) ver, err := Version(db)
if err != nil { if err != nil {
t.Fatalf("Version failed: %v", err) t.Fatalf("Version failed: %v", err)
} }
if ver != 5 { if ver != wantVersion {
t.Errorf("expected version 5, got %d", ver) t.Errorf("expected version %d, got %d", wantVersion, ver)
} }
} }