Setup Your Migration Workflow
With your schema defined as code, the next step is to set up how changes are applied to your database. If you haven't chosen a workflow yet, see Choose a workflow.
- Versioned
- Declarative (State-based)
How it works
Atlas keeps track of schema changes in a migration directory containing SQL migration scripts. Each file is named with a version and an optional label:
<version>_<label>.sql
For example: 20240520182336_add_users_table.sql. By default, Atlas uses a YYYYMMDDHHMMSS
timestamp as the version.
Step 1: Configuration
Add a migration block to your atlas.hcl to define where migration files are stored:
env "local" {
url = getenv("DB_URL")
dev = "docker://postgres/16/dev"
schema {
src = "file://schema"
}
migration {
dir = "file://migrations"
}
}
Step 2: Create the baseline migration
The first migration in your directory is called the baseline. It captures the full schema as it exists today, so that all future migrations are diffs on top of it.
Generate the baseline by running:
atlas migrate diff --env local baseline
Two new files are created under the migrations/ directory:
migrations/
├── 20240604131146_baseline.sql
└── atlas.sum
20240604131146_baseline.sqlcontains the fullCREATE TABLE,CREATE INDEX, etc. statements for your current schema.atlas.sumis the integrity file that protects against conflicts between developers working in parallel.
How does migrate diff work?
- Reads the desired schema from the location defined in the
schema.srcfield inatlas.hcl. - Reads the state represented by your migrations directory.
- Calculates the diff and verifies the migration plan on the dev database.
- Writes a new migration file and updates
atlas.sum.
Step 3: Apply the baseline
How you apply the baseline depends on whether you are starting with a new (empty) database or adopting Atlas on an existing database that already has tables.
New database
For a new database with no existing tables, apply migrations normally:
atlas migrate apply --env local
Atlas executes the baseline migration and records it in the revision table.
Existing database
If your database already contains the schema described by the baseline (e.g. a production database
you are now bringing under Atlas management), the baseline migration should not be executed. Instead,
use the --baseline flag to tell Atlas to mark it as already applied and start tracking from the
next migration:
atlas migrate apply --env local --baseline "20240604131146"
Replace 20240604131146 with the version from your baseline filename.
If your project already uses Flyway, Liquibase, goose, or another migration tool, see Importing an Existing Database for instructions on importing your existing migration history into Atlas.
Step 4: Verify
Confirm the migration directory is in sync with your schema by running migrate diff again:
atlas migrate diff --env local
The migration directory is synced with the desired state, no changes to be made.
How it works
In the declarative workflow, there is no migration directory. You edit your schema files directly,
and Atlas computes the diff against the target database at apply time. Changes are planned and
applied in a single step using atlas schema apply.
Configuration
Your atlas.hcl only needs url, dev, and schema.src (no migration block):
env "local" {
url = getenv("DB_URL")
dev = "docker://postgres/16/dev"
schema {
src = "file://schema"
}
}
Verify
Confirm that Atlas sees no difference between your schema files and the live database:
atlas schema apply --env local --dry-run
Schemas are synced, no changes to be made.
For more details, see the Declarative Apply guide.