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:
@@ -103,6 +103,22 @@ func lastApplied(gdb *gorm.DB, plugins []party.Plugin, pluginID string) (string,
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// appliedSince returns the migrations of before that after no longer
|
||||
// lists as pending, in the order of before.
|
||||
func appliedSince(before, after []MigrationRef) []MigrationRef {
|
||||
still := make(map[MigrationRef]bool, len(after))
|
||||
for _, ref := range after {
|
||||
still[ref] = true
|
||||
}
|
||||
var applied []MigrationRef
|
||||
for _, ref := range before {
|
||||
if !still[ref] {
|
||||
applied = append(applied, ref)
|
||||
}
|
||||
}
|
||||
return applied
|
||||
}
|
||||
|
||||
// statusJSON is one migration of migrate:status --json.
|
||||
type statusJSON struct {
|
||||
Plugin string `json:"plugin"`
|
||||
@@ -154,12 +170,18 @@ asks.`,
|
||||
if err := confirmProduction(app, in, out, fmt.Sprintf("Apply %d migrations to the production database?", len(pending)), "Migration cancelled."); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := Migrate(gdb, plugins); err != nil {
|
||||
return err
|
||||
migrateErr := Migrate(gdb, plugins)
|
||||
// Report what Migrate actually applied: the pending
|
||||
// migrations before it ran that are no longer pending,
|
||||
// also when it failed partway.
|
||||
after, err := PendingMigrations(gdb, plugins)
|
||||
if err != nil {
|
||||
return errors.Join(migrateErr, err)
|
||||
}
|
||||
applied := appliedSince(pending, after)
|
||||
counts := map[string]int{}
|
||||
var order []string
|
||||
for _, ref := range pending {
|
||||
for _, ref := range applied {
|
||||
if counts[ref.Plugin] == 0 {
|
||||
order = append(order, ref.Plugin)
|
||||
}
|
||||
@@ -168,7 +190,10 @@ asks.`,
|
||||
for _, plugin := range order {
|
||||
out.Info(fmt.Sprintf("%s: %d applied", plugin, counts[plugin]))
|
||||
}
|
||||
out.Success(fmt.Sprintf("Applied %d migrations", len(pending)))
|
||||
if migrateErr != nil {
|
||||
return migrateErr
|
||||
}
|
||||
out.Success(fmt.Sprintf("Applied %d migrations", len(applied)))
|
||||
return nil
|
||||
})
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user