| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "log/slog" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo" |
| 10 | "amadan.net/rastrillo/rastrillo/db" |
| 11 | "amadan.net/rastrillo/rastrillo/migrate" |
| 12 | ) |
| 13 | |
| 14 | func TestPlaybookMigration(t *testing.T) { |
| 15 | d, err := db.Open(filepath.Join(t.TempDir(), "playbooks.db"), slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 16 | must(t, err) |
| 17 | t.Cleanup(func() { must(t, d.Close()) }) |
| 18 | old := &migrate.Set{} |
| 19 | for _, m := range equipo.BootSchema.All() { |
| 20 | if m.ID != "equipo/0025_playbooks" { |
| 21 | old.Add(m) |
| 22 | } |
| 23 | } |
| 24 | _, err = migrate.Apply(ctx, d, old) |
| 25 | must(t, err) |
| 26 | f := &fixture{DB: d} |
| 27 | f.exec(t, "INSERT INTO teams(id,name,roster_epoch) VALUES('saved','Saved',2)") |
| 28 | f.exec(t, "INSERT INTO tasks(id,requester,request_id,team_id,revision,plan_revision,kind) VALUES('old','person','request','saved',5,2,'investigation')") |
| 29 | f.exec(t, "INSERT INTO plan_revisions(task_id,revision,plan,actor_kind,actor,changed_keys) VALUES('old',2,'saved plan','person','person','[]')") |
| 30 | f.exec(t, "INSERT INTO audit_events(actor,kind,record_id) VALUES('person','old.audit','old')") |
| 31 | result, err := migrate.Apply(ctx, d, equipo.BootSchema) |
| 32 | must(t, err) |
| 33 | if len(result.Applied) != 1 || result.Applied[0] != "equipo/0025_playbooks" { |
| 34 | t.Fatal(result.Applied) |
| 35 | } |
| 36 | if f.count(t, "SELECT revision FROM tasks WHERE id='old'") != 5 || f.count(t, "SELECT count(*) FROM plan_revisions WHERE plan='saved plan'") != 1 || f.count(t, "SELECT count(*) FROM audit_events WHERE kind='old.audit'") != 1 { |
| 37 | t.Fatal("migration changed history") |
| 38 | } |
| 39 | if f.count(t, "SELECT count(*) FROM playbooks") != 0 { |
| 40 | t.Fatal("migration silently installed templates") |
| 41 | } |
| 42 | fresh := newFixture(t) |
| 43 | upgraded, err := migrate.Read(ctx, d.Writer()) |
| 44 | must(t, err) |
| 45 | clean, err := migrate.Read(ctx, fresh.DB.Writer()) |
| 46 | must(t, err) |
| 47 | if !upgraded.Equal(clean) { |
| 48 | t.Fatal(upgraded.Diff(clean)) |
| 49 | } |
| 50 | result, err = migrate.Apply(ctx, d, equipo.BootSchema) |
| 51 | must(t, err) |
| 52 | if len(result.Applied) != 0 { |
| 53 | t.Fatal("migration repeated") |
| 54 | } |
| 55 | } |
| 56 | |