| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 6 | "amadan.net/rastrillo/rastrillo/db" |
| 7 | "amadan.net/rastrillo/rastrillo/migrate" |
| 8 | "io" |
| 9 | "log/slog" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func TestCoordinatorMigrationUpgrade(t *testing.T) { |
| 15 | path := filepath.Join(t.TempDir(), "coordinator-upgrade.db") |
| 16 | d, err := db.Open(path, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 17 | must(t, err) |
| 18 | t.Cleanup(func() { must(t, d.Close()) }) |
| 19 | old := &migrate.Set{} |
| 20 | for _, m := range equipo.BootSchema.All() { |
| 21 | if m.ID != "equipo/0021_coordinator" && m.ID != "equipo/0031_notifications" && m.ID != "equipo/0033_plan_notifications" && m.ID != "equipo/0035_chat_stream" { |
| 22 | old.Add(m) |
| 23 | } |
| 24 | } |
| 25 | _, err = migrate.Apply(ctx, d, old) |
| 26 | must(t, err) |
| 27 | _, err = d.Writer().Exec("INSERT INTO audit_events(actor,kind,record_id,at) VALUES('historical','fixture','record',CURRENT_TIMESTAMP)") |
| 28 | must(t, err) |
| 29 | _, err = d.Writer().Exec("UPDATE service SET schema_version=7 WHERE id=1") |
| 30 | must(t, err) |
| 31 | result, err := migrate.Apply(ctx, d, equipo.BootSchema) |
| 32 | must(t, err) |
| 33 | if len(result.Applied) != 4 || result.Applied[0] != "equipo/0021_coordinator" || result.Applied[1] != "equipo/0031_notifications" || result.Applied[2] != "equipo/0033_plan_notifications" || result.Applied[3] != "equipo/0035_chat_stream" { |
| 34 | t.Fatal("wrong upgrade", result.Applied) |
| 35 | } |
| 36 | f := &fixture{DB: d, Store: equipo.NewStore(d)} |
| 37 | if f.count(t, "SELECT count(*) FROM audit_events WHERE actor='historical'") != 1 { |
| 38 | t.Fatal("upgrade lost history") |
| 39 | } |
| 40 | if f.count(t, "SELECT schema_version FROM service") != 8 { |
| 41 | t.Fatal("core schema version not advanced") |
| 42 | } |
| 43 | for range 2 { |
| 44 | core, err := team.OpenShared(ctx, d.Writer(), path, team.Options{Clock: team.RealClock{}, Authority: equipo.NewAuthority(f.Store, nil)}) |
| 45 | must(t, err) |
| 46 | must(t, core.Close()) |
| 47 | } |
| 48 | fresh := newFixture(t) |
| 49 | upgraded, err := migrate.Read(ctx, d.Writer()) |
| 50 | must(t, err) |
| 51 | baseline, err := migrate.Read(ctx, fresh.DB.Writer()) |
| 52 | must(t, err) |
| 53 | if !upgraded.Equal(baseline) { |
| 54 | t.Fatal("upgrade schema differs", upgraded.Diff(baseline)) |
| 55 | } |
| 56 | } |
| 57 | |