Skip to content

Graphic preview

100%
Page type: reference

Database test commands

Exact flags, steps, assertions, reports, and isolation behavior for migration and schema tests.

Edge documents the current development build and may describe unreleased behavior. To use those commands, build Ptah from master. For an installed release, select its version in the header.

Ptah has two native declarative test commands:

Terminal window
ptah migrations test [flags]
ptah schema test [flags]

Both load *.yaml and *.yml files from --dir, execute cases in file order, render a report, and return a non-zero status when any case fails.

Flag Migrations test Schema test Default
--dir Yes Yes ./tests
--migrations-dir Yes No ./migrations
--root-dir Go annotation directory used by apply_schema Desired-schema source: a SQL, YAML, HCL, or DBML file, a Go annotation directory, or a live database URL ./models
--seed-dir Default for seed steps Default for seed steps No default
--dir-format auto, ptah, or atlas No ptah
--db-url Optional explicit throwaway database Optional explicit throwaway database Ephemeral SQLite
--schema No Repeatable desired-schema allow-list; an unqualified object belongs to the schema the URL names, as described under Isolation; database-wide extensions remain regardless of installation placement All schemas
--var No Repeatable name=value value for a variable block of an HCL desired-schema file or a .test.hcl document No overrides
--run Go regular expression matched against case names Go regular expression matched against case names All cases
--report text, json, or html text, json, or html text

Each file contains cases, each case contains name and steps, and each step sets exactly one action:

Action Value Scope
migrate_to Non-negative integer version, latest, or 0 Migration tests
apply_schema true Additively converge the selected desired schema using the live target
seed Mapping with required env; optional dir overrides --seed-dir Both
exec SQL string Both
assert Mapping with query and one condition Both

Assertion conditions are row_count (a non-negative integer), scalar, and error_contains. Unknown fields, unnamed cases, empty step lists, and steps with zero or multiple actions are errors. Every seed step must either set dir or receive a run-level --seed-dir.

Without --db-url, every case gets a separate ephemeral SQLite database. With --db-url, all cases share the explicit caller-owned database and the caller must isolate and clean it: the objects the run provisions stay there after it. The explicit target must be disposable because test steps mutate schema and data. Reusing it is supported for idempotent cases, but concurrent runners are not serialized and arbitrary SQL is not rolled back.

apply_schema preserves unrelated objects, uses live dialect capabilities and identifier semantics, and can repair supported drift. Roles and grants are rejected because their effects can escape the database-local test lifecycle. On MySQL and MariaDB the step runs its plan on a strict session, as schema apply does, so a change that a row a test stored does not fit fails the step rather than rewriting the row; see A session without a strict SQL mode.

--schema counts an object the desired schema leaves unqualified in the schema the throwaway database creates it in, and --db-url names that schema:

Throwaway database Schema of an unqualified object
SQLite, and the ephemeral default main
PostgreSQL family the one search_path schema the URL sets, otherwise public
SQL Server dbo
MySQL and MariaDB the database the URL names

So CREATE TABLE users with --db-url 'postgres://localhost/dev?search_path=app' is kept by --schema app, and --schema public keeps nothing and is refused. A search_path that lists more than one schema, such as app,public, is refused when the selection depends on it: the server creates the object in the first of those schemas that exists, which is not known before the run. A --source-db-url source is paired the same way, through the schema its own session reads: the introspection leaves that schema’s objects unqualified.

schema test names its desired schema with one selector: --root-dir for a directory of Go annotations, --schema-file for a .sql, .yaml, .yml, .hcl, or .dbml file, and --source-db-url for a database whose live schema is introspected. Naming two is refused before the throwaway database is provisioned. --root-dir also accepts a file or a database URL, which is what it did before the other two selectors existed; such a run reports on standard error which selector names that source exactly. HCL files also accept repeatable --var name=value values. The same values reach a .test.hcl document’s own variable blocks: a supplied value wins over a default and satisfies a block that has none, and a block with neither is refused by name.

Exit Meaning
0 Every case passed.
1 The runner completed and at least one case failed.
2 Usage, input, connection, interruption, setup, or report failure.

For examples and CI guidance, see Test migrations and schemas. For the embeddable runner contract, see Public Go API.