229 lines
8.6 KiB
Go
229 lines
8.6 KiB
Go
// 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") }
|