package engine import ( "bytes" "encoding/json" "errors" "os" "reflect" "sort" "strings" "testing" "dagger/db-migration/verdict" ) var images = Images{Liquibase: "lb-image", Flyway: "fw-image", Atlas: "at-image"} // The argv main.go built inline before #2135, copied verbatim from // origin/main 13a3918e2. Phase 1 promised no behaviour change; this is the // proof. Changing a script here is a behaviour change: review it as one. // // MUTATION-CHECK: drop `|| exit 1` from the Liquibase apply loop, or // `-placeholderReplacement=false` from Flyway → FAILS. func TestCommandsAreTheOnesTheModuleRanBefore(t *testing.T) { r := New(images) for _, tc := range []struct { name string got func() (Command, error) want Command }{ {"liquibase apply", func() (Command, error) { return r["liquibase"].Apply([]string{"a.sql", "b c.sql"}) }, Command{"lb-image", []string{"sh", "-c", `for f in "$@"; do liquibase --searchPath=/migrations --changelog-file="$f" --url="$DATABASE_URL" update || exit 1; done`, "sh", "a.sql", "b c.sql"}}}, {"liquibase rollback", func() (Command, error) { return r["liquibase"].Rollback("granted.sql", 3) }, Command{"lb-image", []string{"sh", "-c", `liquibase --searchPath=/migrations --changelog-file="$1" --url="$DATABASE_URL" rollback-count --count="$2"`, "sh", "granted.sql", "3"}}}, {"liquibase drift", func() (Command, error) { return r["liquibase"].Drift("granted.sql") }, Command{"lb-image", []string{"sh", "-c", `liquibase --searchPath=/reference --changelog-file="$1" --url="$REFERENCE_URL" update >/dev/null && ` + `liquibase --url="$DATABASE_URL" --referenceUrl="$REFERENCE_URL" diff`, "sh", "granted.sql"}}}, {"liquibase snapshot", func() (Command, error) { return r["liquibase"].Snapshot() }, Command{"lb-image", []string{"sh", "-c", `liquibase --url="$DATABASE_URL" snapshot --snapshotFormat=json`}}}, {"flyway apply", func() (Command, error) { return r["flyway"].Apply([]string{"V1__x.sql"}) }, Command{"fw-image", []string{"sh", "-c", `flyway -url="$DATABASE_URL" -locations=filesystem:/migrations -placeholderReplacement=false migrate`}}}, {"atlas apply", func() (Command, error) { return r["atlas"].Apply([]string{"1_x.sql"}) }, Command{"at-image", []string{"sh", "-c", `atlas migrate apply --dir file:///migrations --url "$DATABASE_URL"`}}}, } { got, err := tc.got() if err != nil { t.Errorf("%s: %v", tc.name, err) continue } if !reflect.DeepEqual(got, tc.want) { t.Errorf("%s:\n got %q\nwant %q", tc.name, got, tc.want) } } } // A file name is an argument, never part of the script: a name carrying shell // syntax must not reach `sh -c`'s script string. func TestFileNamesNeverReachTheScript(t *testing.T) { evil := `x.sql"; touch /pwned; echo "` l := New(images)["liquibase"] for name, cmd := range map[string]func() (Command, error){ "apply": func() (Command, error) { return l.Apply([]string{evil}) }, "rollback": func() (Command, error) { return l.Rollback(evil, 1) }, "drift": func() (Command, error) { return l.Drift(evil) }, } { c, err := cmd() if err != nil { t.Fatalf("%s: %v", name, err) } if strings.Contains(c.Args[2], "pwned") { t.Errorf("%s: the file name reached the script: %q", name, c.Args[2]) } if c.Args[3] != "sh" || c.Args[4] != evil { t.Errorf("%s: the file name is not the first positional argument: %q", name, c.Args) } } } // An operation an adapter does not declare fails with ErrUnsupported — it // never runs something else — and one it declares never does. func TestUnsupportedOperationsMatchTheCapabilities(t *testing.T) { for _, a := range New(images) { caps := a.Capabilities() for op, c := range map[string]struct { declared bool call func() (Command, error) }{ "rollback": {caps.Rollback, func() (Command, error) { return a.Rollback("f.sql", 1) }}, "drift": {caps.Drift, func() (Command, error) { return a.Drift("f.sql") }}, "snapshot": {caps.Snapshot, func() (Command, error) { return a.Snapshot() }}, } { cmd, err := c.call() if c.declared && err != nil { t.Errorf("%s declares %s but it fails: %v", a.Name(), op, err) } if !c.declared && (!errors.Is(err, ErrUnsupported) || cmd.Image != "" || cmd.Args != nil) { t.Errorf("%s does not declare %s, got %q, %v", a.Name(), op, cmd, err) } } if _, err := a.Apply([]string{"f.sql"}); err != nil { t.Errorf("%s apply: %v", a.Name(), err) } } } // Every adapter runs its own pinned image, never another tool's. func TestEachAdapterRunsItsOwnImage(t *testing.T) { want := map[string]string{"liquibase": "lb-image", "flyway": "fw-image", "atlas": "at-image"} for name, a := range New(images) { c, _ := a.Apply([]string{"f.sql"}) if c.Image != want[name] { t.Errorf("%s runs %q, want %q", name, c.Image, want[name]) } } } // --tool selects exactly one adapter; an unknown or misspelt one is an error, // not another tool (it fell through to atlas until 2026-09-27). func TestForRefusesAnUnknownTool(t *testing.T) { r := New(images) for _, tool := range []string{"liquibase", "flyway", "atlas"} { a, err := r.For(tool) if err != nil || a.Name() != tool { t.Errorf("For(%q) = %v, %v", tool, a, err) } } for _, tool := range []string{"", "none", "Liquibase", "liquibse", "sqitch"} { if a, err := r.For(tool); err == nil { t.Errorf("For(%q) = %v, want an error", tool, a.Name()) } } if r["liquibase"] == nil || DefaultTool != "liquibase" { t.Errorf("the default adapter is %q and must be registered", DefaultTool) } } // The adapters are exactly the tools --tool takes, minus "none" (which opts // out of governance and runs nothing). func TestAdaptersAreTheModulesTools(t *testing.T) { var tools []string for tool := range verdict.MigrationTools { if tool != "none" { tools = append(tools, tool) } } sort.Strings(tools) if got := New(images).Names(); !reflect.DeepEqual(got, tools) { t.Errorf("adapters %v, but --tool takes %v", got, tools) } } // The declared dialects are the ones verdict.CheckTool accepts the tool for — // a declaration that disagreed would advertise a tool the module then refuses, // or hide one it runs. func TestDeclaredDialectsAreTheOnesCheckToolAccepts(t *testing.T) { for _, a := range New(images) { declared := map[string]bool{} for _, d := range a.Capabilities().Dialects { declared[d] = true } for _, d := range []string{"postgres", "mysql", "oracle", "tsql"} { accepted := verdict.CheckTool(a.Name(), d) == nil if accepted != declared[d] { t.Errorf("%s: dialect %s declared=%v, CheckTool accepts=%v", a.Name(), d, declared[d], accepted) } } if !sort.StringsAreSorted(a.Capabilities().Dialects) { t.Errorf("%s: dialects not sorted: %v", a.Name(), a.Capabilities().Dialects) } } } // capabilities.json is the contract the API's registry // (app/platform/services/db_engine_adapters.py) is tested against too. // // MUTATION-CHECK: flip Liquibase Rollback to false, or add an adapter without // a JSON entry → FAILS here; edit the JSON only → FAILS here and in the API test. func TestCapabilitiesMatchTheContract(t *testing.T) { raw, err := os.ReadFile("capabilities.json") if err != nil { t.Fatalf("capabilities.json is the cross-language contract and must exist: %v", err) } var contract map[string]Capabilities dec := json.NewDecoder(bytes.NewReader(raw)) dec.DisallowUnknownFields() if err := dec.Decode(&contract); err != nil { t.Fatalf("capabilities.json: %v", err) } got := map[string]Capabilities{} for name, a := range New(images) { got[name] = a.Capabilities() } if !reflect.DeepEqual(got, contract) { t.Errorf("adapters declare\n %+v\nbut capabilities.json says\n %+v", got, contract) } }