Migrations overview
diff compares your structs with a stored snapshot and writes the difference as a plain SQL file. You review and edit the file, then up applies it.
- Change a structAdd a field, a tag, an index
- diffCompares models with a snapshot. No database needed
- Read the .sqlPlain SQL, yours to edit. Warnings sit above risky lines
- upApplies it, under a lock, once
Writing a migration needs no database. Migrations are ordinary SQL, so other tools can read the files and you can stop using ant's runner at any time. There are no down migrations: to undo a change, write a new one.
New or existing database
Starting a new project
- Write your structs
diffreads them. migrate diff create_tablesWrites the first SQL file. No database needed.- Read the fileIt's plain SQL you can edit.
migrate upApplies it and records that it ran.
Bringing an existing database
pg_dump --schema-onlyYour schema, as it is today.- Write structs to matchBy hand, or with an AI assistant and this prompt.
migrate driftLists every difference. Fix the structs until there are none left.migrate adoptYour dump becomes migration 0001.baselinemarks it as already applied.
diff, read the SQL, run up. check fails your CI if someone forgot.What this means for a project
- Adopting doesn't touch your tables.
baselinerecords your schema as already applied. It adds one table,ant_migrations. - You can keep your migration tool.
diffandcheckdon't need a database. ant can write the SQL for golang-migrate or your current tool to run. - You can start with just queries. Structs that match your tables are all you need to read and write.
- You can leave. The files are ordinary SQL in a common layout. The history table is yours to drop.
On MySQL, MariaDB, SQLite and SQL Server, ant reads and writes existing tables. The schema stays with your current tool.
Set up
The command line tool is a small program in your own module, because Go can't load structs from another module.
package main
import (
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/skudra-go/ant/dialect/postgres"
"github.com/skudra-go/ant/migrate"
"example.com/app/models"
)
func main() {
migrate.Main[models.AppDB](postgres.Dialect)
}
Every command is go run ./cmd/migrate <command>. The ones that need a database read DATABASE_URL, or take --dsn.
Write the first migration
export DATABASE_URL='postgres://user:password@localhost:5432/app?sslmode=disable'
go run ./cmd/migrate diff create_tables
Wrote migrations/versions/0001_create_tables.up.sql
create table posts
create table users
create index posts_user_id_idx on posts
add foreign key posts_user_id_fkey on posts
Review it, then apply it with: up
CREATE TABLE "posts" (
"id" bigint GENERATED BY DEFAULT AS IDENTITY,
"user_id" bigint NOT NULL,
"title" text NOT NULL,
CONSTRAINT "posts_pkey" PRIMARY KEY ("id")
);
CREATE TABLE "users" (
"id" bigint GENERATED BY DEFAULT AS IDENTITY,
"name" text NOT NULL,
"email" text NOT NULL,
"created_at" timestamptz NOT NULL,
CONSTRAINT "users_pkey" PRIMARY KEY ("id"),
CONSTRAINT "users_email_key" UNIQUE ("email")
);
CREATE INDEX "posts_user_id_idx" ON "posts" ("user_id");
ALTER TABLE "posts" ADD CONSTRAINT "posts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id");
Apply migrations
go run ./cmd/migrate up
Applied 0001_create_tables (6ms)
Commit everything under migrations/. The snapshot is what the next diff compares against.
migrations/
├── versions/
│ ├── 0001_create_tables.up.sql runs once
│ └── schema.snapshot.json what the models looked like
└── code/ reruns when a file changes
Change a model
User gets a nullable Bio and a required Age:
type User struct {
ID int64
Name string
Email string `ant:"unique"`
CreatedAt time.Time
Bio *string
Age int64
}
diff --dry-run prints the SQL and writes nothing:
go run ./cmd/migrate diff add_bio_and_age --dry-run
ALTER TABLE "users" ADD COLUMN "bio" text;
-- WARNING: existing rows of users get 0 in age. Change the DEFAULT below to fill them differently.
ALTER TABLE "users" ADD COLUMN "age" bigint NOT NULL DEFAULT 0;
ALTER TABLE "users" ALTER COLUMN "age" DROP DEFAULT;
Run diff without the flag to write 0002_add_bio_and_age.up.sql, then up. Generated SQL covers what diff does for each kind of change.
See where things stand
status lists every migration:
go run ./cmd/migrate status
MIGRATION STATE APPLIED
0001_create_tables applied 2026-11-08 15:16:30 UTC
0002_add_bio_and_age pending
1 waiting. Apply with: up
up --dry-run prints the SQL of what is pending and runs none of it. drift compares the live database with what the migrations describe:
go run ./cmd/migrate drift
No drift: the database matches the snapshot.
Check in CI
check fails when a struct changed without a migration. It needs no database.
go run ./cmd/migrate check
Error: ant: models have changes with no migration. Run diff to write them:
add column users.bio
add column users.age
It exits with status 1. With nothing to write, it prints OK: models, snapshot, and migrations agree. and exits with 0. It also fails when the snapshot and the newest file disagree, as after a bad merge: see working in a team.
Next to GORM, ent and sqlc
db.AutoMigrate(&User{})
Changes the live database directly, with no file to review, and doesn't delete unused columns. For versioned migrations, GORM's docs point to Atlas.
atlas migrate diff add_bio \
--dir "file://ent/migrate/migrations" \
--to "ent://ent/schema" \
--dev-url "docker://mysql/8/ent"
Versioned SQL files you can review. They need ent's schema DSL and a dev database to replay the history on.
sqlc does not perform database migrations for you.
From sqlc's docs. sqlc reads the files of your migration tool.
Wrote migrations/versions/0002_add_bio.up.sql
add column users.bio
Review it, then apply it with: up
ALTER TABLE "users" ADD COLUMN "bio" text;
diffcompares your structs with a stored snapshot.- Risky statements get a warning above them. Renames keep their data.
Next
- Models to tables: how structs become columns.
- Generated SQL: warnings, renames, type changes and backfills.
- Hand-written changes: views, functions, data, and seeds.
- Deploying: embedding, and when to apply.
- Adopting a database: start from a schema you already have.
See it tested: tests/features/migrations