Skip to main content

Working in a team

What happenedWhat ant doesWhat to do
The same version numberEvery command refuses, and names both filesRenumber the one that hasn't been applied anywhere
A snapshot merge conflictThe latest line changes with every migration, so this is on purposeTake either side, keep both migrations, renumber if needed, then run snapshot
A clean merge that lost the snapshotcheck compares latest with the newest file, and fails if they differRun snapshot
A migration older than what ranup refuses it, because it was written against a schema that is goneRenumber it, or set Config.OutOfOrder

Two migrations with the same number​

Both branches wrote 0002. After the merge every command refuses:

Error: ant: two migrations share a version: 0002_add_bio_and_age and 0002_add_nick. Renumber the one not yet applied anywhere

Rename the file no database has applied, then run check.

A snapshot that doesn't match the files​

The snapshot records which migration it was written for. When a merge keeps one side's snapshot and the other side's files, check fails:

Error: ant: snapshot does not match the migration files: it was written for 0018_enable_citext, and the newest file is 0017_drop_ip. After a merge, renumber if needed, then run snapshot

snapshot records the merged models with no new migration. If the merged models need one, diff writes it afterwards.

Avoid collisions with timestamps​

Config.Name = migrate.Timestamped names files with the time, so two branches rarely pick the same number.

package main

import (
"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, migrate.Config{Name: migrate.Timestamped})
}

Squashing​

squash replaces every file with a single baseline.

go run ./cmd/migrate squash
Wrote migrations/versions/0003_baseline.up.sql, replacing 2 migrations through 0002.
Warning: a squash keeps what the models describe. Anything written by hand, such as extensions, types, triggers, or data, is gone from this file. Add it back, or squash with --keep to join the old files instead.

The new file starts with -- ant:replaces 0002. A database that already has those migrations records it without running it, and a new database runs it. A database that hasn't reached 0002 is refused and has to catch up from an older release first.

What a squash loses

A squash writes from the models, so hand-written extensions, triggers, and data are gone from the file. --keep joins the old files instead.