Testing Framework: Test Database Schemas, Migrations, and Plans
The Atlas testing framework runs unit and integration tests against your database. Schema tests verify functions, views, triggers, and constraints, and migration and plan tests check what each change does to the data. Test files hold the structure in HCL and the logic in plain SQL, and every case runs on a clean dev database, locally and in CI.
Tests can load production-like rows from a dataset and profile every statement they run: the locks it takes, the tables it rewrites, and the space it needs. Profile expectations set bounds on those numbers, so a change that would block production fails the test before it is merged. Data Scripts are tested the same way. Datasets and profiling are in preview.
Test every change before it merges
Whether a person or a coding agent wrote the change, the same tests run in the agent's session and again in CI. Profile expectations fail a change that would block a production table.
Test Types
Atlas has a test command for each workflow: atlas schema test for the schema, atlas migrate test
for versioned migrations, atlas schema plan test for declarative migration plans, and
atlas script test for Data Scripts. A test case is a block named after its kind:
test "schema", test "migrate", test "plan", or test "script". The examples below cover the four
kinds, plus three capabilities they build on: table-driven cases, datasets, and profiling.
Testing Schemas
Assert the behavior of views, functions, and constraints with exec, catch, and assert over plain SQL.
Testing Migrations
Migrate the dev database version by version, and verify the data migrations between versions.
Testing Plans
Apply a declarative migration plan to a known schema state, and verify the result.
Testing with Data
Seed tests with an RDS snapshot export, CSV files, or generated rows, sampled and masked.
Profiling
Measure the locks, rewrites, and space each migration, query, or function needs.
Table-Driven Tests
Generate a test case for each item of a map or a set with for_each.
Template Databases
On PostgreSQL, build the schema once and run every test group on a fresh copy.
Testing Data Scripts
Assert what a Data Script prints, and expect the errors it should raise.
Testing in GitHub Actions
Run atlas migrate test on GitHub Actions with ariga/atlas-action/migrate/test.
- Write unit and integration tests for functions, stored procedures, triggers, views, and constraints in plain SQL, each on a clean dev database.
- Test data migrations version by version, and declarative migration plans against a known schema state.
- Catch migrations that would lock or rewrite large tables: profile them against production-like datasets (in preview).
- Run the tests in CI with the
migrate/testandschema/testGitHub Actions.
Testing requires Atlas Pro. New to Atlas Pro? Sign up for a free 30-day trial, or log in from the terminal:
atlas login
- Schemas
- Migrations
- Plans
- Data Scripts
- Table-Driven
- Datasets
- Profiling
A schema test runs against the desired schema. Atlas creates the schema on the dev database before
each test case and cleans it up after. exec expects a statement to pass, and catch expects one
to fail:
test "schema" "postal" {
parallel = true
exec {
sql = "select '12345'::us_postal_code"
}
catch {
sql = "select 'hello'::us_postal_code"
}
}
atlas schema test --dev-url "docker://postgres/15/dev" --url "file://schema.hcl" .
On PostgreSQL, schema tests can also call pgTAP assertion functions. See pgTAP and Atlas.
A migration test starts from the zero state of the migration directory and migrates it version by version. Insert rows between two versions to verify the data migration that connects them:
test "migrate" "20240613061102" {
# Migrate to version 20240613061046.
migrate {
to = "20240613061046"
}
# Insert some test data.
exec {
sql = "INSERT INTO users (name) VALUES ('Ada Lovelace')"
}
# Migrate to version 20240613061102.
migrate {
to = "20240613061102"
}
# Verify the correctness of the data migration.
exec {
sql = "SELECT first_name, last_name FROM users"
output = "Ada, Lovelace"
}
}
atlas migrate test --dev-url "docker://postgres/15/dev" .
A plan test brings the dev database to a schema state with schema, applies a declarative
migration plan to it with apply, and checks the result:
test "plan" "20240613061102" {
# Initial state is schema.v1.sql.
schema {
url = "file://snapshots/schema.v1.sql"
}
# Insert some test data.
exec {
sql = <<-SQL
INSERT INTO "users" ("name") VALUES
('Peter Parker'),
('Bruce Wayne'),
('Victor Von Doom'),
('Groot')
SQL
}
# Apply the tested plan.
apply {
url = "file://plans/20240613061102.plan.hcl"
}
# Verify the correctness of the data migration.
exec {
sql = "SELECT first_name, last_name FROM users ORDER BY id"
format = table
output = <<-TAB
first_name | last_name
------------+-----------
Peter | Parker
Bruce | Wayne
Victor | Von Doom
Groot |
TAB
}
}
atlas schema plan test --dev-url "docker://postgres/15/dev" .
A script test runs Data Scripts against a fresh database: point the case at a
schema, seed it, run the script with the script command, and assert on the result. The script
command also runs inside schema, migration, and plan tests:
test "script" "cancel_pending" {
schema {
url = "file://schema.sql"
}
exec {
sql = "INSERT INTO orders (id, status) VALUES (1, 'pending'), (2, 'pending'), (3, 'shipped')"
}
script "exec" {
file = "scripts.hcl"
run = "^cancel_pending$"
}
script "query" {
file = "scripts.hcl"
run = "^pending_count$"
output = "0"
}
}
atlas script test --env dev --run cancel_pending
for_each generates a test case for each item of a map or a set:
test "schema" "seed" {
for_each = [
{input: "hello", expected: "HELLO"},
{input: "world", expected: "WORLD"},
]
log {
message = "Testing ${each.value.input} -> ${each.value.expected}"
}
exec {
sql = "SELECT upper('${each.value.input}')"
output = each.value.expected
}
}
atlas schema test --env dev
A dataset block registers rows: an Amazon RDS snapshot exported to S3, a directory of CSV files,
or rows generated from the schema. The dataset command loads them into the test database, so the
assertions run over production-like rows instead of an empty schema:
dataset "rds_snapshot" "prod" {
src = "s3://backups/exports/prod-2026-10-13/appdb?region=us-east-1"
exclude = ["analytics.events*"]
}
test "schema" "invoices" {
dataset {
use = dataset.rds_snapshot.prod
}
exec {
sql = "SELECT count(*) FROM billing.invoice_lines l JOIN billing.invoices i ON i.id = l.invoice_id"
output = "9000"
}
# The keys of the snapshot are kept, and new rows get keys after them.
exec {
sql = "INSERT INTO billing.tenants (slug, display_name) VALUES ('after-load', 'After Load') RETURNING id"
output = "251"
}
}
export AWS_PROFILE=exports-reader
atlas schema test --dev-url "docker://postgres/18/dev" --url "file://schema.sql" .
Datasets are in preview and open to a subset of Teams and Enterprise customers.
Add --profile to atlas migrate test or atlas schema test to measure what the tests run: the
locks each statement takes and for how long, the tables it rewrites, and the disk, log, and
temporary space it needs:
atlas migrate test --dev-url "docker://postgres/18/dev" --profile .
An expect "profile" block sets bounds on those numbers.
The online set below allows no table rewrites and at most five seconds of blocked reads or writes,
and the retype test applies it to version 6, adding a 1GB bound on the copy of orders:
dataset "synthetic" "orders" {
batch = 5000
table "users" {
rows = 20000
total = 2000000
column "id" {
value = row.index
}
}
table "orders" {
rows = 200000
total = 50000000
column "user_id" {
value = row.index % 20000
}
column "status" {
value = format("s%d", row.index % 4)
}
}
}
# Online migrations rewrite no table,
# and block reads or writes for at most five seconds.
expect "profile" "online" {
metric "db.table.rewrites" {
eq = 0
}
metric "db.lock.time" {
le = "5s"
}
}
test "migrate" "retype" {
migrate {
to = "1"
}
dataset {
use = dataset.synthetic.orders
}
migrate {
to = "5"
}
migrate {
to = "6"
expect "profile" {
use = [expect.profile.online]
metric "db.table.copy" {
table = "orders"
le = "1GB"
}
}
}
}
Version 6 changes a column type:
ALTER TABLE orders ALTER COLUMN amount TYPE bigint;
atlas migrate test --dev-url "docker://postgres/18/dev" .
The dataset generates 200,000 rows of orders, and its total sets the 50,000,000 rows the table
holds in production. When a bound does not hold, the test fails at the command's line, and the error
names the statement that broke it. Expect blocks are checked with or without --profile.
Profiling is in preview and open to a subset of Teams and Enterprise customers.
Running Tests
Every test command runs on a dev database, passed with --dev-url. Pass a
directory to run the test files in it, or name a test file. Each command reads what it tests from its
own source:
atlas schema testrunstest "schema"cases against--url: a database URL, an HCL, SQL, or ORM schema, or a migration directory.atlas migrate testrunstest "migrate"cases against--dir, the migration directory (file://migrationsby default).atlas schema plan testrunstest "plan"cases, each applying the plan file in itsapplyblock.atlas script testrunstest "script"cases, and eachscriptcommand names the file it loads.
Add --run with a regular expression to run only the matching tests, or --env to read the URLs
from an env block in atlas.hcl:
atlas schema test --env local schema.test.hcl
Get Started with a Coding Agent
With a coding agent, install the Atlas agent skills. One command installs them:
- Claude Code
- OpenAI Codex
- Grok Build (Cursor)
- GitHub Copilot
Install the skills from a terminal. It installs the Atlas plugin, with all its skills, for every project on this machine.
claude plugin marketplace add ariga/atlas
claude plugin install atlas@ariga
Then start the onboarding skill in Claude Code. See Claude Code with Atlas for the other ways to use the skills.
/atlas:atlas-onboard
Install the skills from a terminal. Run it from the repository root. It installs every Atlas skill in .agents/skills/.
npx skills add ariga/atlas -a codex -y
Then start the onboarding skill in OpenAI Codex. See OpenAI Codex with Atlas for the other ways to use the skills.
$atlas-onboard
Install the skills from a terminal. Run it from the repository root. It installs every Atlas skill in .grok/skills/.
npx skills add ariga/atlas -a grok -y
Then start the onboarding skill in Grok Build (Cursor).
/atlas-onboard
Install the skills from a terminal. Run it from the repository root. It installs every Atlas skill in .agents/skills/.
npx skills add ariga/atlas -a github-copilot -y
Then start the onboarding skill in GitHub Copilot. See GitHub Copilot with Atlas for the other ways to use the skills.
/atlas-onboard
The atlas skill runs atlas schema test and atlas migrate test on the changes the agent makes,
before it reports them done. To set up Atlas in a new project, start the
atlas-onboard skill.
Next Steps
Testing Database Functions
Write tests for database functions with atlas schema test
Testing Database Views
Test view definitions and the data they return
Testing Stored Procedures
Test procedure logic and the data integrity it maintains
Testing Database Triggers
Test trigger logic and the events it handles
Testing Postgres Domains
Test the constraints and logic of PostgreSQL domain types
Testing Data Migrations
Seed, backfill, and validate data integrity in migration tests