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:
@@ -60,58 +60,23 @@ func migrator(gdb *gorm.DB, pluginID string, migrations []*gormigrate.Migration)
|
||||
// Migrate runs the framework-owned system_files, backend-admin,
|
||||
// deferred-binding (DeferredBindingMigrations, history DeferredHistoryID) and
|
||||
// job-queue (QueueMigrations) sets first, then each plugin's HasMigrations set
|
||||
// in party.Activate order.
|
||||
// in party.Activate order. It applies the sets migrationSets lists, the list
|
||||
// PendingMigrations reads, and stops at the first set that fails.
|
||||
func Migrate(gdb *gorm.DB, plugins []party.Plugin) error {
|
||||
if gdb == nil {
|
||||
return fmt.Errorf("lagoon: gorm db is nil")
|
||||
}
|
||||
m, err := migrator(gdb, "summercms.attach", attach.Migrations)
|
||||
sets, err := migrationSets(gdb, plugins)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.Migrate(); err != nil {
|
||||
return fmt.Errorf("lagoon: migrate system_files: %w", err)
|
||||
}
|
||||
admin, err := migrator(gdb, "summercms.cabana", BackendAdminMigrations)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := admin.Migrate(); err != nil {
|
||||
return fmt.Errorf("lagoon: migrate backend admin: %w", err)
|
||||
}
|
||||
deferred, err := migrator(gdb, DeferredHistoryID, DeferredBindingMigrations)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := deferred.Migrate(); err != nil {
|
||||
return fmt.Errorf("lagoon: migrate deferred bindings: %w", err)
|
||||
}
|
||||
sqlDB, err := gdb.DB()
|
||||
if err != nil {
|
||||
return fmt.Errorf("lagoon: migrate queue: %w", err)
|
||||
}
|
||||
queue, err := migrator(gdb, QueueHistoryID, QueueMigrations(sqlDB))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := queue.Migrate(); err != nil {
|
||||
return fmt.Errorf("lagoon: migrate queue: %w", err)
|
||||
}
|
||||
for _, p := range plugins {
|
||||
hm, ok := p.(pact.HasMigrations)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
migrations := hm.Migrations()
|
||||
if len(migrations) == 0 {
|
||||
continue
|
||||
}
|
||||
m, err := migrator(gdb, p.ID(), migrations)
|
||||
for _, s := range sets {
|
||||
m, err := migrator(gdb, s.id, s.migrations)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.Migrate(); err != nil {
|
||||
return fmt.Errorf("lagoon: migrate %s: %w", p.ID(), err)
|
||||
return fmt.Errorf("lagoon: migrate %s: %w", s.label, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -211,7 +176,10 @@ type MigrationRef struct {
|
||||
|
||||
// migrationSet is one history table's declared migrations.
|
||||
type migrationSet struct {
|
||||
id string
|
||||
id string
|
||||
// label names the set in Migrate's errors: "system_files", "backend
|
||||
// admin", "deferred bindings", "queue" or the plugin ID.
|
||||
label string
|
||||
migrations []*gormigrate.Migration
|
||||
}
|
||||
|
||||
@@ -224,10 +192,10 @@ func migrationSets(gdb *gorm.DB, plugins []party.Plugin) ([]migrationSet, error)
|
||||
return nil, fmt.Errorf("lagoon: migration sets: %w", err)
|
||||
}
|
||||
sets := []migrationSet{
|
||||
{"summercms.attach", attach.Migrations},
|
||||
{"summercms.cabana", BackendAdminMigrations},
|
||||
{DeferredHistoryID, DeferredBindingMigrations},
|
||||
{QueueHistoryID, QueueMigrations(sqlDB)},
|
||||
{"summercms.attach", "system_files", attach.Migrations},
|
||||
{"summercms.cabana", "backend admin", BackendAdminMigrations},
|
||||
{DeferredHistoryID, "deferred bindings", DeferredBindingMigrations},
|
||||
{QueueHistoryID, "queue", QueueMigrations(sqlDB)},
|
||||
}
|
||||
for _, p := range plugins {
|
||||
hm, ok := p.(pact.HasMigrations)
|
||||
@@ -235,7 +203,7 @@ func migrationSets(gdb *gorm.DB, plugins []party.Plugin) ([]migrationSet, error)
|
||||
continue
|
||||
}
|
||||
if ms := hm.Migrations(); len(ms) > 0 {
|
||||
sets = append(sets, migrationSet{p.ID(), ms})
|
||||
sets = append(sets, migrationSet{p.ID(), p.ID(), ms})
|
||||
}
|
||||
}
|
||||
return sets, nil
|
||||
|
||||
Reference in New Issue
Block a user