Working in a team
| What happened | What ant does | What to do |
|---|---|---|
| The same version number | Every command refuses, and names both files | Renumber the one that hasn't been applied anywhere |
| A snapshot merge conflict | The latest line changes with every migration, so this is on purpose | Take either side, keep both migrations, renumber if needed, then run snapshot |
| A clean merge that lost the snapshot | check compares latest with the newest file, and fails if they differ | Run snapshot |
| A migration older than what ran | up refuses it, because it was written against a schema that is gone | Renumber 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.
A squash writes from the models, so hand-written extensions, triggers, and data are gone from the file. --keep joins the old files instead.
See it tested: tests/features/migrations/team_test.go