| 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 TestPlanModelChoiceMigration(t *testing.T) { |
| 15 | d, err := db.Open(filepath.Join(t.TempDir(), "choices.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/0026_connection_modes" { |
| 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 connection_choices(person,project_id,revision,connection_ids_json,pinned_id) VALUES('saved',7,9,'["a","b"]','b')`) |
| 28 | f.exec(t, `INSERT INTO audit_events(actor,kind,record_id) VALUES('saved','saved.audit','record')`) |
| 29 | result, err := migrate.Apply(ctx, d, equipo.BootSchema) |
| 30 | must(t, err) |
| 31 | if len(result.Applied) != 1 || result.Applied[0] != "equipo/0026_connection_modes" { |
| 32 | t.Fatal(result.Applied) |
| 33 | } |
| 34 | if f.count(t, `SELECT count(*) FROM connection_choices WHERE person='saved' AND project_id=7 AND revision=9 AND connection_ids_json='["a","b"]' AND pinned_id='b' AND mode='best_fit'`) != 1 || f.count(t, `SELECT count(*) FROM audit_events WHERE kind='saved.audit'`) != 1 { |
| 35 | t.Fatal("migration changed ticks, pin or history") |
| 36 | } |
| 37 | fresh := newFixture(t) |
| 38 | upgraded, err := migrate.Read(ctx, d.Writer()) |
| 39 | must(t, err) |
| 40 | clean, err := migrate.Read(ctx, fresh.DB.Writer()) |
| 41 | must(t, err) |
| 42 | if !upgraded.Equal(clean) { |
| 43 | t.Fatal(upgraded.Diff(clean)) |
| 44 | } |
| 45 | result, err = migrate.Apply(ctx, d, equipo.BootSchema) |
| 46 | must(t, err) |
| 47 | if len(result.Applied) != 0 { |
| 48 | t.Fatal("migration repeated") |
| 49 | } |
| 50 | } |
| 51 | |