Hand-written changes
Write an empty migration
diff --empty writes a blank file for extensions, types, triggers, or anything else the models can't express. It leaves the snapshot alone. If the models changed too, snapshot accepts them.
go run ./cmd/migrate diff enable_citext --empty
Wrote migrations/versions/0018_enable_citext.up.sql
-- Written by hand. Plain SQL, run in one transaction.
-- Add -- ant:no-transaction above for statements that cannot run in one.
CREATE EXTENSION IF NOT EXISTS citext;
Views and functions
Files in migrations/code run after the versioned ones, in name order, and again whenever they change, and after any versioned migration that runs later, since it may have dropped the view to change a column the view reads. The file stays the one definition of the view or function. Number them if one depends on another, as in 10_views/ and 20_functions/. Each file must be safe to run again.
DROP VIEW IF EXISTS post_stats;
CREATE VIEW post_stats AS SELECT user_id, count(*) AS posts FROM posts GROUP BY user_id;
Data, in Go
For changes SQL can't express, register a migration in Go. It runs once, in order with the files, inside a transaction, and tx is your context bound to that transaction.
func init() {
migrate.Register[AppDB](
"0005_split_names",
func(ctx context.Context, tx *AppDB) error {
_, err := ant.Raw[any](tx, "UPDATE users SET name = trim(name)").Exec(ctx)
return err
},
)
}
Put it in a package that cmd/migrate and any program calling migrate.Up both import. A model describes today's schema, not the schema at that point in history, so use ant.Raw in code that must outlive a model change. For work too big for one transaction, add migrate.NoTransaction and commit in batches.
Statements that refuse a transaction
-- ant:no-transaction at the top of a file runs it without one, a statement at a time. diff adds it when it writes CREATE INDEX CONCURRENTLY, and when a file adds an enum value that the same file then uses (see generated SQL).
-- ant:no-transaction
CREATE INDEX CONCURRENTLY "posts_title_idx" ON "posts" ("title");
The directive counts only in the comments at the top of the file. One below the first statement, as when a Render puts a SET first, would not be read, so up refuses the file.
A file that runs in ant's transaction can't hold a BEGIN, COMMIT or ROLLBACK of its own: its COMMIT would end ant's transaction, and a failure after it would leave half the file applied. up refuses such a file before anything of it runs. A file from another tool that manages its own transaction takes -- ant:no-transaction. ROLLBACK TO a savepoint stays inside the transaction and is fine.
If a file without a transaction fails partway, or the process running up dies in it, up stops until someone has looked: see troubleshooting.
Seed data
seed runs Go seeders, each in its own transaction, with model hooks off. Seeders run on every seed, so make them safe to repeat.
func init() {
migrate.Seed[AppDB]("roles", func(ctx context.Context, db *AppDB) error {
_, _, err := db.Roles.FirstOrCreate(ctx, &Role{Name: "admin"}, ant.Eq(&Roles.Name, "admin"))
return err
})
}
go run ./cmd/migrate seed
Config.SeedHooks turns hooks on. For data that should track a file, such as a list of countries, use an idempotent INSERT ... ON CONFLICT in migrations/code.
Tests and prototypes
db.EnsureCreated(ctx) builds tables straight from the structs, with the enums and sequences they use, and db.EnsureDeleted(ctx) removes them. go run ./cmd/migrate reset wipes a local database and applies everything. It asks for the database's name first, or takes --confirm name.
See it tested: tests/features/migrations/data_test.go