Files
integraci-dagger-modules/db-migration/README.md
T

213 lines
12 KiB
Markdown

# db-migration (Dagger module)
Judges a service's migration SQL with **IntegraCI's DB-change engine** — the DBV
policy engine behind the tenant's own DB-change policy
(`POST /api/v1/db-changes/analyze`) — and applies migrations with the
tenant's own tool: `liquibase` (default), `flyway` or `atlas`.
Builds with `dagger functions` on engine v0.21.8 (verified 2026-09-27).
## Tools, images and databases
Every image is pinned by tag and digest in `main.go` (checked 2026-09-27).
| Tool | Image | Licence | Databases |
| --- | --- | --- | --- |
| `liquibase` | `liquibase/liquibase:4.31.1-alpine` — the version the platform runs | Apache 2.0 | PostgreSQL, SQL Server, Oracle, MariaDB, DB2, Firebird, Snowflake, SQLite, H2, HSQLDB (drivers in the image) |
| `flyway` | `flyway/flyway:10.22.0-alpine` | Apache 2.0 (Community) | as Flyway Community supports |
| `atlas` | `arigaio/atlas:1.3.3-community-alpine` | Apache 2.0 (Community Edition) | PostgreSQL, MySQL, MariaDB, SQLite |
- **MySQL with Liquibase:** the image has no MySQL Connector/J (it is GPL and
not shipped), so a `jdbc:mysql:` URL finds no driver. Use a `jdbc:mariadb:`
URL; the MariaDB driver talks to MySQL.
- **Atlas is the Community Edition.** The standard `arigaio/atlas` image is
licensed under the proprietary Atlas MSA; this module ran it until
2026-09-27. Community migrates PostgreSQL and MySQL only, so `--tool=atlas`
on any other dialect or engine is refused before anything runs.
- **`--tool` is checked.** Anything but `liquibase`, `flyway`, `atlas` or
`none` is refused. Until 2026-09-27 an unknown value (a typo) fell through
to atlas and applied the directory with a tool nobody chose.
- **What the SQL is judged as.** Name nothing, and the platform decides: in the
engines of the service's own databases when `--service-id` is given (its
`spec.resources` and the DB schemas registered to it — what the deploy gate
judges with too), and as PostgreSQL otherwise. Or name the database
**engine** (`--engine`): the platform judges in that engine's dialect, and
an engine it has no rules for comes back unjudged (an approver decides, so
the step fails under `--block-destructive`; `GET /api/v1/db-engines`). Or
name the SQL **dialect** (`--dialect`: postgres, mysql, oracle, tsql) — not
both. `--engine` needs a platform with engine support; an older one refuses
it with HTTP 422, and the step fails. Before 2026-09-27 `--dialect` defaulted
to `postgres` and was sent on every call, so a warehouse or MongoDB
directory was judged as PostgreSQL.
## Engine adapters (ADR-0070 §1)
DBV judges; the tool that applies a change is an **adapter** in `engine/`. An
adapter builds the command (the pinned image and the exact argv) and declares
what it drives. It never sees a secret — `main.go` binds `$DATABASE_URL` /
`$REFERENCE_URL` as Dagger secrets and mounts only the judged files — and it
never picks the files: they are the ones the engine judged.
```go
type Adapter interface {
Name() string // the --tool value
Capabilities() Capabilities
Apply(files []string) (Command, error) // judged files, in order
Rollback(file string, n int) (Command, error) // last n changesets of a granted changelog
Drift(file string) (Command, error) // build file into $REFERENCE_URL, diff $DATABASE_URL
Snapshot() (Command, error) // schema of $DATABASE_URL
}
```
An operation an adapter does not drive returns `engine.ErrUnsupported`; it
never runs something else. What each adapter drives today:
| Adapter | Dialects accepted | apply | rollback | drift | snapshot | native lint | checksum |
| --- | --- | --- | --- | --- | --- | --- | --- |
| `liquibase` (default, `engine.DefaultTool`) | postgres, mysql, oracle, tsql | yes | yes | yes | yes | — | changeset |
| `flyway` | postgres, mysql, oracle, tsql | yes | — | — | — | — | file |
| `atlas` | postgres, mysql | yes | — | — | — | yes (advisory) | directory |
- **Grants are written for the default adapter.** `execute`, `drift` and
`snapshot` run under a platform grant whose changelog is Liquibase
formatted SQL, so they use `engine.DefaultTool`. Re-choosing the default
(ADR-0070 §2) is that one constant plus its mirror in the API.
- **The pins stay in `main.go`.** `tools/ci/check-liquibase-pins.py` and
Renovate read them there; `engine.New` receives them.
- **The API mirrors the declarations** in
`apps/api/app/platform/services/db_engine_adapters.py`. Both sides are
tested against `engine/capabilities.json`; neither parses the other's
language. `ci_presets.DB_MIGRATION_TOOLS` is derived from it (adapters plus
`none`).
- **Dialects are accepted, not tested per dialect.** They are what
`verdict.CheckTool` lets through; per-dialect adapter tests are ADR-0070
phase 2. Not driven by any adapter yet: dry-run SQL, `status`, and online
schema change (pgroll, gh-ost).
- **Phase 1 (#2135, 2026-10-11) changed no behaviour.** Every argv is
byte-identical to the one `main.go` built inline before;
`engine/engine_test.go` pins each one.
## Functions
### `lint`
Sends every `.sql` file under `dir` to IntegraCI and fails when the tenant's
policy blocks a change — or requires approval for one, which nobody can give
inside a CI step (the change then goes through IntegraCI's governed apply).
For `atlas` it then runs `atlas migrate lint` — only after the policy has
passed every file, because the lint **replays** the files on its dev database
— and fails when that reports an error (a destructive change, for example);
the lint's output is in the error. It sees the judged directory alone, so an
`atlas.hcl` in the repo is not read. Without `--dev-url` (a secret: a DSN
carries a password) the lint starts a throwaway PostgreSQL for this run only
(`postgres:16.13-alpine`, as a Dagger service); another dialect needs
`--dev-url`. The previous default, `docker://postgres/16/dev`, needs a Docker
CLI inside the atlas container and never worked, so `--tool=atlas` failed in
every generated pipeline (none passes `--dev-url`).
```sh
dagger call lint \
--source . \
--dir migrations \
--api-url "$INTEGRACI_URL" \
--token env://INTEGRACI_TOKEN
```
| Param | Default | Description |
| ------------------- | -------------------------- | -------------------------------------------------------- |
| `source` | (required) | Source directory containing migrations |
| `dir` | `migrations` | Migrations directory relative to source |
| `tool` | `liquibase` | `liquibase` \| `atlas` \| `flyway` \| `none` (skip) |
| `api-url` | (required unless `none`) | IntegraCI API URL — the engine that judges the SQL |
| `token` | (required unless `none`) | IntegraCI API token |
| `dialect` | (unset: the platform picks) | `postgres` \| `mysql` \| `oracle` \| `tsql` |
| `engine` | (optional, instead of `dialect`) | Database engine; the platform picks the dialect (`GET /db-engines`) |
| `environment-id` | (optional) | Tenant environment whose policy and stage apply |
| `block-destructive` | `true` | Also fail on `requires_approval` (`blocked` always fails) |
| `dev-url` | (a throwaway PostgreSQL) | Secret: dev DB atlas replays the migrations on (`atlas` only) |
**It fails closed.** The step fails when nothing has judged the SQL:
- no `--api-url`/`--token`;
- no `.sql` file in `dir` at all;
- a file the engine cannot read: an XML/YAML/JSON changelog, a `.pgsql` or
other suffix a tool may be configured to run, a Java migration. Only `.sql`,
docs (`.md`, `.txt`, `.rst`, `README`, `LICENSE`), `.gitkeep` and
`atlas.sum` may sit in `dir`. Use Liquibase formatted SQL, or `--tool=none`
to opt out of governance;
- with `--tool=liquibase`, a Liquibase directive that runs or skips text the
engine reads as a comment: `--precondition*`/`--preconditions`,
`--include`/`--includeAll`, `--property`, `--validCheckSum`,
`--ignoreLines`. Measured 2026-09-27: `--precondition-sql-check
expectedResult:0 WITH d AS (DELETE FROM users RETURNING 1) SELECT count(*)
FROM d` is judged "additive" and Liquibase executes it. `--liquibase
formatted sql`, `--changeset` (with attributes), `--rollback` and
`--comment` stay allowed;
- a file holding a `${…}` placeholder. Flyway and Liquibase substitute it
after the engine has judged the text, so what would run is not what was
judged (`${flyway:filename}` in a file named `V9__x');DROP TABLE t;--.sql`);
- a file over 200 kB;
- an answer from IntegraCI that is not a verdict (non-2xx, a redirect, no
`decision`).
Measured 2026-09-26 on engine v0.21.8: hidden files (`.x/V1.sql`, `.V1.sql`)
are listed and judged; a symlinked directory is listed as a plain entry, so it
is refused, not followed.
`--block-destructive=false` only lets `requires_approval` through. A change the
tenant's policy `blocked` is not the pipeline's to waive.
A Jenkins build that holds no IntegraCI token (a pull-request build never does)
renders the stage as a visible "DB migration lint NOT RUN" instead of failing;
the governed apply judges every changeset again.
### `apply`
Judges the migrations the same way, refuses what the policy refuses, then runs
the tool against `--db-url` (a secret, never on the command line).
The tool sees **only `dir`**, mounted alone with an empty working directory, so
a `liquibase.properties` or flyway config in the repo cannot point it at SQL
nobody judged. Liquibase runs each judged `.sql` file as its own formatted-SQL
changelog, ordered with numbers compared as numbers (`V2` before `V10`); flyway
runs with `-placeholderReplacement=false`.
**Breaking for anyone who wired `apply` by hand** (no generated pipeline calls
it): the repo's `liquibase.properties`, `flyway.conf`/`flyway.toml` and
`atlas.hcl` are no longer read. That drops contexts/labels (every changeset
runs), a custom history table or schema (the tool sees an empty history and
re-runs everything), and a master changelog's order and file paths (Liquibase
stores the path relative to `dir`, so changesets recorded under another path
run again). Check a database's history table before pointing `apply` at it.
```sh
dagger call apply --source . --dir migrations \
--db-url env://DATABASE_URL --api-url "$INTEGRACI_URL" --token env://INTEGRACI_TOKEN
```
`applied` is read from each tool's own summary (Liquibase `Run: N` per
changelog, Flyway `Successfully applied N migrations`, one atlas
`-- migrating version` line per file). Before 2026-09-27 it counted flyway and
atlas markers for every tool, so a Liquibase apply reported 0 and a Flyway one 1.
## Before 2026-09-26
The lint could not fail for `liquibase` or `flyway`: it ran `validate || true`
(which needs a live database anyway), looked for destructive keywords in the
tool's output (which carries no SQL), and POSTed a summary to
`/api/v1/services/{id}/migrations`, a route the API never served.
## Tests
```sh
dagger develop # regenerate the gitignored SDK bindings
go test ./... # verdict/: HTTP contract, decisions, file split, applied count;
# engine/: adapter argv, capabilities, the capabilities.json contract
```
`apps/api/tests/test_dagger_db_migration_lint_contract.py` checks the other
side: the route and request fields exist in the API, and every CI template
passes an API URL and a token. `test_dagger_db_migration_tools_match_the_presets.py`
checks that `--tool` accepts exactly the tools the CI presets offer, and
`test_db_engine_adapters_match_the_module.py` that the API's adapter registry
matches `engine/capabilities.json` and that `dagger.json` includes `engine/`.