chore: publish IntegraCI build modules 732ec6c83925

This commit is contained in:
dhimaz0445 committed 2026-10-28 08:09:16 +00:00
1 parent 7a84930dea
commit 5f8a24d014
133 files changed
+18990 -2

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
{
"atlas": {
"dialects": ["mysql", "postgres"],
"rollback": false,
"drift": false,
"snapshot": false,
"dry_run_sql": false,
"native_lint": true,
"checksum": "directory",
"online_change": []
},
"flyway": {
"dialects": ["mysql", "oracle", "postgres", "tsql"],
"rollback": false,
"drift": false,
"snapshot": false,
"dry_run_sql": false,
"native_lint": false,
"checksum": "file",
"online_change": []
},
"liquibase": {
"dialects": ["mysql", "oracle", "postgres", "tsql"],
"rollback": true,
"drift": true,
"snapshot": true,
"dry_run_sql": false,
"native_lint": false,
"checksum": "changeset",
"online_change": []
}
}
+228
View File
@@ -0,0 +1,228 @@
// Package engine is the migration-engine adapter interface of the db-migration
// module (ADR-0070 §1): DBV judges, an adapter only says how its tool runs.
//
// An adapter builds COMMANDS — the image and the exact argv to exec in it — and
// declares what it can do. It never sees a secret: the database URLs reach the
// container as environment variables ($DATABASE_URL, $REFERENCE_URL) that
// main.go binds as Dagger secrets, and the scripts read them from there. It does
// not decide what runs either: the files it is handed are the judged ones, and
// the module mounts them alone at /migrations (or /reference).
//
// Phase 1 (#2135, 2026-10-11) moved the module's existing tool calls behind
// this interface with no behaviour change: every argv is byte-identical to the
// one main.go built inline before, and engine_test.go pins each of them. The
// API mirrors the capabilities in app/platform/services/db_engine_adapters.py;
// both sides are compared against capabilities.json, so neither parses the
// other's language.
package engine
import (
"errors"
"fmt"
"sort"
)
// ErrUnsupported is returned for an operation the adapter does not drive.
var ErrUnsupported = errors.New("not supported by this migration tool")
// DefaultTool is the adapter the platform's grants are written for: a granted
// batch, a drift reference and a snapshot are Liquibase formatted SQL today.
// ADR-0070 §2 makes re-choosing the default a configuration change; this is
// that configuration.
const DefaultTool = "liquibase"
// Capabilities are what an adapter DRIVES in this module today — not what its
// tool could do in principle. A capability declared here is one a caller can
// use without the call failing with ErrUnsupported.
type Capabilities struct {
// Which of the DBV engine's four dialects (postgres, mysql, oracle, tsql)
// the module accepts this tool for (verdict.CheckTool), sorted. Accepted,
// not tested per dialect: per-dialect tests are ADR-0070 phase 2.
Dialects []string `json:"dialects"`
// Rollback of the last n changesets of a granted changelog.
Rollback bool `json:"rollback"`
// Drift: build the granted changelog into a reference and diff the live
// database against it.
Drift bool `json:"drift"`
// Snapshot of the live schema as JSON.
Snapshot bool `json:"snapshot"`
// DryRunSQL: render the SQL an apply would run without running it.
DryRunSQL bool `json:"dry_run_sql"`
// NativeLint: the tool's own lint runs after the DBV verdict, as an
// advisory signal (ADR-0070 §3), never as the verdict.
NativeLint bool `json:"native_lint"`
// Checksum is what the tool's history is keyed and checksummed by:
// "changeset" (id+author+file), "file" (versioned script) or "directory"
// (a sum file over the whole directory).
Checksum string `json:"checksum"`
// OnlineChange lists online schema-change tools the adapter drives
// (pgroll, gh-ost, ...). None yet (ADR-0070 §5, 6–12 months).
OnlineChange []string `json:"online_change"`
}
// Command is one tool invocation: the image to run and the argv to exec in it.
type Command struct {
Image string
Args []string
}
// Adapter runs one migration tool.
type Adapter interface {
// Name is the --tool value that selects this adapter.
Name() string
Capabilities() Capabilities
// Apply runs the judged files, in this order, against $DATABASE_URL.
Apply(files []string) (Command, error)
// Rollback undoes the last n changesets of file against $DATABASE_URL.
Rollback(file string, n int) (Command, error)
// Drift builds file into $REFERENCE_URL and diffs $DATABASE_URL against it.
Drift(file string) (Command, error)
// Snapshot prints the schema of $DATABASE_URL.
Snapshot() (Command, error)
}
// Images are the pinned images the adapters run. They stay declared in
// main.go, where tools/ci/check-liquibase-pins.py and Renovate look for them.
type Images struct {
Liquibase string
Flyway string
Atlas string
}
// Registry is the set of adapters, keyed by tool name.
type Registry map[string]Adapter
// New returns every adapter the module has, running the given images.
func New(images Images) Registry {
r := Registry{}
for _, a := range []Adapter{
Liquibase{Image: images.Liquibase},
Flyway{Image: images.Flyway},
Atlas{Image: images.Atlas},
} {
r[a.Name()] = a
}
return r
}
// For returns the adapter for tool. An unknown tool is an error, never another
// tool: until 2026-09-27 an unknown --tool fell through to atlas.
func (r Registry) For(tool string) (Adapter, error) {
a, ok := r[tool]
if !ok {
return nil, fmt.Errorf("--tool=%q is not a migration tool this module runs", tool)
}
return a, nil
}
// Names lists the registered tools, sorted.
func (r Registry) Names() []string {
out := make([]string, 0, len(r))
for name := range r {
out = append(out, name)
}
sort.Strings(out)
return out
}
func unsupported(tool, op string) error {
return fmt.Errorf("%s %s: %w", tool, op, ErrUnsupported)
}
// Liquibase is the default adapter (ADR-0070 §2: pinned below 5.0).
type Liquibase struct{ Image string }
func (Liquibase) Name() string { return "liquibase" }
func (Liquibase) Capabilities() Capabilities {
return Capabilities{
Dialects: []string{"mysql", "oracle", "postgres", "tsql"},
Rollback: true,
Drift: true,
Snapshot: true,
Checksum: "changeset",
OnlineChange: []string{},
}
}
// Apply runs each judged file as its own formatted-SQL changelog, in order:
// there is no master changelog to point it at, and that is exactly the set the
// engine saw. Files are positional arguments, never spliced into the script.
func (l Liquibase) Apply(files []string) (Command, error) {
return Command{Image: l.Image, Args: append([]string{"sh", "-c",
`for f in "$@"; do liquibase --searchPath=/migrations --changelog-file="$f" --url="$DATABASE_URL" update || exit 1; done`,
"sh"}, files...)}, nil
}
// Rollback undoes the last n changesets with Liquibase's rollback-count.
func (l Liquibase) Rollback(file string, n int) (Command, error) {
return Command{Image: l.Image, Args: []string{"sh", "-c",
`liquibase --searchPath=/migrations --changelog-file="$1" --url="$DATABASE_URL" rollback-count --count="$2"`,
"sh", file, fmt.Sprintf("%d", n)}}, nil
}
// Drift updates the empty reference database from the granted changelog
// (mounted at /reference), then diffs the live database against it.
func (l Liquibase) Drift(file string) (Command, error) {
return Command{Image: l.Image, Args: []string{"sh", "-c",
`liquibase --searchPath=/reference --changelog-file="$1" --url="$REFERENCE_URL" update >/dev/null && ` +
`liquibase --url="$DATABASE_URL" --referenceUrl="$REFERENCE_URL" diff`,
"sh", file}}, nil
}
func (l Liquibase) Snapshot() (Command, error) {
return Command{Image: l.Image, Args: []string{"sh", "-c",
`liquibase --url="$DATABASE_URL" snapshot --snapshotFormat=json`}}, nil
}
// Flyway applies a directory of versioned scripts. Placeholder replacement is
// off: a ${...} in judged SQL must reach the database as written, not be
// rewritten after the verdict.
type Flyway struct{ Image string }
func (Flyway) Name() string { return "flyway" }
func (Flyway) Capabilities() Capabilities {
return Capabilities{
Dialects: []string{"mysql", "oracle", "postgres", "tsql"},
Checksum: "file",
OnlineChange: []string{},
}
}
func (f Flyway) Apply([]string) (Command, error) {
return Command{Image: f.Image, Args: []string{"sh", "-c",
`flyway -url="$DATABASE_URL" -locations=filesystem:/migrations -placeholderReplacement=false migrate`}}, nil
}
func (Flyway) Rollback(string, int) (Command, error) {
return Command{}, unsupported("flyway", "rollback")
}
func (Flyway) Drift(string) (Command, error) { return Command{}, unsupported("flyway", "drift") }
func (Flyway) Snapshot() (Command, error) { return Command{}, unsupported("flyway", "snapshot") }
// Atlas is the Community Edition (Apache 2.0): PostgreSQL and MySQL only. Its
// own lint (`atlas migrate lint`) runs in main.go after the DBV verdict.
type Atlas struct{ Image string }
func (Atlas) Name() string { return "atlas" }
func (Atlas) Capabilities() Capabilities {
return Capabilities{
Dialects: []string{"mysql", "postgres"},
NativeLint: true,
Checksum: "directory",
OnlineChange: []string{},
}
}
func (a Atlas) Apply([]string) (Command, error) {
return Command{Image: a.Image, Args: []string{"sh", "-c",
`atlas migrate apply --dir file:///migrations --url "$DATABASE_URL"`}}, nil
}
func (Atlas) Rollback(string, int) (Command, error) {
return Command{}, unsupported("atlas", "rollback")
}
func (Atlas) Drift(string) (Command, error) { return Command{}, unsupported("atlas", "drift") }
func (Atlas) Snapshot() (Command, error) { return Command{}, unsupported("atlas", "snapshot") }
+205
View File
@@ -0,0 +1,205 @@
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)
}
}