Skip to main content

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.

  1. Change a structAdd a field, a tag, an index
  2. diffCompares models with a snapshot. No database needed
  3. Read the .sqlPlain SQL, yours to edit. Warnings sit above risky lines
  4. 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

  1. Write your structsdiff reads them.
  2. migrate diff create_tablesWrites the first SQL file. No database needed.
  3. Read the fileIt's plain SQL you can edit.
  4. migrate upApplies it and records that it ran.

Bringing an existing database

  1. pg_dump --schema-onlyYour schema, as it is today.
  2. Write structs to matchBy hand, or with an AI assistant and this prompt.
  3. migrate driftLists every difference. Fix the structs until there are none left.
  4. migrate adoptYour dump becomes migration 0001. baseline marks it as already applied.
From here, both are the same. Change a struct, run 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. baseline records your schema as already applied. It adds one table, ant_migrations.
  • You can keep your migration tool. diff and check don'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.
PostgreSQL only, for now

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.

cmd/migrate/main.go
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
migrations/versions/0001_create_tables.up.sql
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​

GORM
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.

ent, with 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
sqlc does not perform database migrations for you.

From sqlc's docs. sqlc reads the files of your migration tool.

go run ./cmd/migrate diff add_bio
Wrote migrations/versions/0002_add_bio.up.sql
add column users.bio
Review it, then apply it with: up
0002_add_bio.up.sql
ALTER TABLE "users" ADD COLUMN "bio" text;
  • diff compares your structs with a stored snapshot.
  • Risky statements get a warning above them. Renames keep their data.

Next​