fix(lagoon): one migration set list for Migrate and PendingMigrations

- Migrate iterates migrationSets, so the startup summary, --pretend, the
  production question, the applied lines and migrate:status --json read
  the same list; each set keeps its error wording through a label
- migrate reports pending-before minus pending-after as its applied
  lines and result line, also when Migrate fails partway (then it prints
  what was applied and returns the error)
- TestMigrateAppliedLines (Docker) pins both (WR-07)
This commit is contained in:
Jakub Zych
2026-10-22 02:36:19 +02:00
parent a9025c0d12
commit 020fe838ea
4 changed files with 101 additions and 52 deletions

View File

@@ -294,3 +294,59 @@ func TestMigrateStatusJSON(t *testing.T) {
}
}
}
// TestMigrateAppliedLines (WR-07): migrate's "N applied" lines count what
// Migrate applied, also when a later set fails: the sets before the failure
// are reported, the error is returned and no success line is printed. The
// next run reports only what it applied.
func TestMigrateAppliedLines(t *testing.T) {
db, dsn := dedicatedDB(t, "lagoon_applied_lines")
gdb, err := Use(t.Context(), db)
if err != nil {
t.Fatal(err)
}
create := func(name, sql string) *gormigrate.Migration {
return &gormigrate.Migration{
ID: "202610220001_create_" + name,
Migrate: func(tx *gorm.DB) error { return tx.Exec(sql).Error },
Rollback: func(tx *gorm.DB) error { return tx.Exec(`DROP TABLE IF EXISTS ` + name).Error },
}
}
alpha := migPlugin{id: "demo.okay", migrations: []*gormigrate.Migration{create("applied_okay", `CREATE TABLE applied_okay (id BIGSERIAL PRIMARY KEY)`)}}
broken := migPlugin{id: "demo.broken", migrations: []*gormigrate.Migration{create("applied_broken", `CREATE TABLE applied_broken (id BOGUS_TYPE)`)}}
framework, err := PendingMigrations(gdb, nil)
if err != nil {
t.Fatal(err)
}
out, errOut, code, _ := runLagoon(t, "local", dsn, []party.Plugin{alpha, broken}, "migrate")
if code != bonfire.ExitFailure || !strings.Contains(errOut, "demo.broken") {
t.Fatalf("migrate with a failing set: exit %d\n%s%s", code, out, errOut)
}
if !strings.Contains(out, "demo.okay: 1 applied\n") {
t.Errorf("the set applied before the failure is not reported:\n%s", out)
}
seen := map[string]bool{}
for _, ref := range framework {
if !seen[ref.Plugin] && !strings.Contains(out, ref.Plugin+": ") {
t.Errorf("framework set %s not reported:\n%s", ref.Plugin, out)
}
seen[ref.Plugin] = true
}
if strings.Contains(out, "demo.broken: ") || strings.Contains(out, "Applied ") {
t.Errorf("a failed set or a success line reported:\n%s", out)
}
fixed := migPlugin{id: "demo.broken", migrations: []*gormigrate.Migration{create("applied_broken", `CREATE TABLE applied_broken (id BIGSERIAL PRIMARY KEY)`)}}
out, errOut, code, _ = runLagoon(t, "local", dsn, []party.Plugin{alpha, fixed}, "migrate")
if code != bonfire.ExitOK {
t.Fatalf("second migrate: exit %d\n%s%s", code, out, errOut)
}
if !strings.Contains(out, "demo.broken: 1 applied\n") || !strings.Contains(out, "Applied 1 migrations") || strings.Contains(out, "demo.okay: ") {
t.Errorf("second migrate result lines:\n%s", out)
}
out, _, _, _ = runLagoon(t, "local", dsn, []party.Plugin{alpha, fixed}, "migrate")
if !strings.Contains(out, "Nothing to migrate") {
t.Errorf("third migrate:\n%s", out)
}
}