| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io" |
| 6 | "log/slog" |
| 7 | "path/filepath" |
| 8 | "reflect" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/equipo" |
| 12 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 13 | "amadan.net/oficina/equipo/internal/taskplan" |
| 14 | "amadan.net/rastrillo/rastrillo/db" |
| 15 | "amadan.net/rastrillo/rastrillo/migrate" |
| 16 | ) |
| 17 | |
| 18 | func TestPlanMigrationUpgrade(t *testing.T) { |
| 19 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 20 | path := filepath.Join(t.TempDir(), "upgrade.db") |
| 21 | d, err := db.Open(path, logger) |
| 22 | must(t, err) |
| 23 | t.Cleanup(func() { must(t, d.Close()) }) |
| 24 | old := &migrate.Set{} |
| 25 | for _, m := range equipo.BootSchema.All() { |
| 26 | if m.ID != "equipo/0020_plans" && m.ID != "equipo/0032_plan_defaults" && m.ID != "equipo/0033_plan_notifications" { |
| 27 | old.Add(m) |
| 28 | } |
| 29 | } |
| 30 | _, err = migrate.Apply(ctx, d, old) |
| 31 | must(t, err) |
| 32 | f := &fixture{DB: d, Store: equipo.NewStore(d)} |
| 33 | must(t, f.Store.Bootstrap(ctx, []string{owner.Address})) |
| 34 | must(t, d.Writer().QueryRow("SELECT id FROM teams").Scan(&f.TeamID)) |
| 35 | f.exec(t, "INSERT INTO tasks(id,requester,request_id,project_id,revision,job_id,state,description,team_id,merge_owner) VALUES('historical',?,'request',4,7,0,'merged','old bug',?,?)", owner.Address, f.TeamID, owner.Address) |
| 36 | audits := f.count(t, "SELECT count(*) FROM audit_events") |
| 37 | result, err := migrate.Apply(ctx, d, equipo.BootSchema) |
| 38 | must(t, err) |
| 39 | if !reflect.DeepEqual(result.Applied, []string{"equipo/0020_plans", "equipo/0032_plan_defaults", "equipo/0033_plan_notifications"}) { |
| 40 | t.Fatal("wrong migration", result.Applied) |
| 41 | } |
| 42 | var task equipo.Task |
| 43 | must(t, d.G.First(&task, "id='historical'").Error) |
| 44 | if task.Revision != 7 || task.State != "merged" || task.Description != "old bug" || task.Kind != taskplan.Bug || task.PlanRevision != 1 || task.ApprovalRevision != 0 || f.count(t, "SELECT count(*) FROM audit_events") != audits { |
| 45 | t.Fatal("upgrade lost history", task) |
| 46 | } |
| 47 | var raw string |
| 48 | must(t, d.Writer().QueryRow("SELECT plan FROM plan_revisions WHERE task_id='historical' AND revision=1").Scan(&raw)) |
| 49 | var seeded taskplan.Plan |
| 50 | must(t, json.Unmarshal([]byte(raw), &seeded)) |
| 51 | if !reflect.DeepEqual(seeded, taskplan.BugPlan(task.ID, task.ProjectID)) { |
| 52 | t.Fatal("migration did not seed BugPlan", seeded) |
| 53 | } |
| 54 | for i, st := range equipo.BugPlan(task) { |
| 55 | if seeded.Steps[i].Key != st.Key { |
| 56 | t.Fatal("seed changed legacy step identity") |
| 57 | } |
| 58 | } |
| 59 | _, err = migrate.Apply(ctx, d, equipo.BootSchema) |
| 60 | must(t, err) |
| 61 | if f.count(t, "SELECT count(*) FROM plan_revisions WHERE task_id='historical'") != 1 { |
| 62 | t.Fatal("history seeded twice") |
| 63 | } |
| 64 | for range 2 { |
| 65 | x, err := team.OpenShared(ctx, d.Writer(), path, team.Options{Clock: team.RealClock{}, Authority: equipo.NewAuthority(f.Store, nil)}) |
| 66 | must(t, err) |
| 67 | must(t, x.Close()) |
| 68 | } |
| 69 | fresh := newFixture(t) |
| 70 | upgraded, err := migrate.Read(ctx, d.Writer()) |
| 71 | must(t, err) |
| 72 | baseline, err := migrate.Read(ctx, fresh.DB.Writer()) |
| 73 | must(t, err) |
| 74 | if !upgraded.Equal(baseline) { |
| 75 | t.Fatal("upgraded schema differs from fresh", upgraded.Diff(baseline)) |
| 76 | } |
| 77 | } |
| 78 | |