| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo" |
| 5 | "amadan.net/rastrillo/rastrillo/db" |
| 6 | "amadan.net/rastrillo/rastrillo/migrate" |
| 7 | "io" |
| 8 | "log/slog" |
| 9 | "path/filepath" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestNotificationMigrationUpgrade(t *testing.T) { |
| 14 | path := filepath.Join(t.TempDir(), "notifications.db") |
| 15 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 16 | d, err := db.Open(path, logger) |
| 17 | must(t, err) |
| 18 | old := &migrate.Set{} |
| 19 | for _, m := range equipo.BootSchema.All() { |
| 20 | if m.ID != "equipo/0031_notifications" && m.ID != "equipo/0033_plan_notifications" { |
| 21 | old.Add(m) |
| 22 | } |
| 23 | } |
| 24 | _, err = migrate.Apply(ctx, d, old) |
| 25 | must(t, err) |
| 26 | s := equipo.NewStore(d) |
| 27 | must(t, s.Bootstrap(ctx, []string{owner.Address})) |
| 28 | msg, err := s.SendMessage(ctx, owner, "", "historical", equipo.Mutation{RequestID: "old"}) |
| 29 | must(t, err) |
| 30 | var team string |
| 31 | must(t, d.Writer().QueryRow("SELECT id FROM teams").Scan(&team)) |
| 32 | _, err = d.Writer().Exec("INSERT INTO tasks(id,requester,request_id,team_id,revision,state,plan_revision,wait_code) VALUES('old-task',?,'old-task',?,1,'proposed',1,'plan.awaiting_approval')", owner.Address, team) |
| 33 | must(t, err) |
| 34 | applied, err := migrate.Apply(ctx, d, equipo.BootSchema) |
| 35 | must(t, err) |
| 36 | if len(applied.Applied) != 2 || applied.Applied[0] != "equipo/0031_notifications" { |
| 37 | t.Fatal(applied) |
| 38 | } |
| 39 | var n int |
| 40 | must(t, d.Writer().QueryRow("SELECT count(*) FROM notification_outbox WHERE resource_id='old-task'").Scan(&n)) |
| 41 | if n != 1 { |
| 42 | t.Fatal("upgrade lost existing actionable task", n) |
| 43 | } |
| 44 | fresh := newFixture(t) |
| 45 | upgraded, err := migrate.Read(ctx, d.Writer()) |
| 46 | must(t, err) |
| 47 | freshSchema, err := migrate.Read(ctx, fresh.DB.Writer()) |
| 48 | must(t, err) |
| 49 | if !upgraded.Equal(freshSchema) { |
| 50 | t.Fatal(upgraded.Diff(freshSchema)) |
| 51 | } |
| 52 | must(t, d.Close()) |
| 53 | d, err = db.Open(path, logger) |
| 54 | must(t, err) |
| 55 | t.Cleanup(func() { must(t, d.Close()) }) |
| 56 | applied, err = migrate.Apply(ctx, d, equipo.BootSchema) |
| 57 | must(t, err) |
| 58 | if len(applied.Applied) != 0 { |
| 59 | t.Fatal("notification migration reapplied") |
| 60 | } |
| 61 | page, err := equipo.NewStore(d).Conversation(ctx, owner, "", 50) |
| 62 | must(t, err) |
| 63 | if len(page.Messages) != 2 || page.Messages[0].ID != msg.ID { |
| 64 | t.Fatal("upgrade lost historical conversation") |
| 65 | } |
| 66 | } |
| 67 | |