Skip to main content

Deploying

Embedding​

migrations/migrations.go
package migrations

import "embed"

//go:embed all:versions all:code
var Files embed.FS

go:embed refuses a folder with no files, so a new project needs a .gitkeep in each until it has migrations. all: makes the .gitkeep count.

One main.go then works everywhere. Embedded is used only when there is no migrations folder on disk, so in your repository diff writes real files, and in a container image up reads them from the binary.

package main

import (
"github.com/skudra-go/ant/dialect/postgres"
"github.com/skudra-go/ant/migrate"

"example.com/app/migrations"
"example.com/app/models"
)

func main() {
migrate.Main[models.AppDB](postgres.Dialect, migrate.Config{Embedded: migrations.Files})
}

When to apply​

As a separate step​

Run up in a Kubernetes job or a compose service that finishes before the server starts. The schema change doesn't depend on a code rollout, and a failure stops the deployment before anything changes. The blog example works this way.

At startup​

Call migrate.Up before the server listens. This suits small deployments.

err := migrate.Up(ctx, db, migrate.Config{FS: files})

Several instances can start at once. A PostgreSQL advisory lock lets one apply the migrations while the others wait, then find nothing left to do.

Connection poolers

Session locks don't survive a pooler in transaction mode, such as PgBouncer. Run migrations on a direct connection, or set Config.NoLock and make sure only one runs.

Hand the SQL to someone else​

When a DBA applies changes, or ant can't reach the database, script writes a single SQL file. Each block checks the history table first, so the file is safe to run more than once, and up agrees with the result afterwards.

go run ./cmd/migrate script > deploy.sql
-- Written by ant. Safe to run more than once: each block checks the history table first.

CREATE TABLE IF NOT EXISTS "ant_migrations" (
"kind" text NOT NULL,
"version" text NOT NULL,
"name" text NOT NULL,
"checksum" text NOT NULL,
"applied_at" timestamptz NOT NULL,
"duration_ms" bigint NOT NULL,
"success" boolean NOT NULL,
"error" text,
CONSTRAINT "ant_migrations_pkey" PRIMARY KEY ("kind", "version")
);

-- 0001_baseline
DO $ant$
BEGIN
IF NOT EXISTS (SELECT 1 FROM "ant_migrations" WHERE kind = 'versioned' AND version = '0001' AND success) THEN
EXECUTE $ant_sql$

A migration that runs without a transaction can't be wrapped in a guard, so script refuses a history that has one:

Error: ant: bad migration: 0010_waiting cannot run in a transaction, so it cannot be guarded. Use script --plain, or up

script --plain leaves out the guards and the history table. It joins the files in order, for a tool that keeps its own history:

go run ./cmd/migrate script --plain > all.sql

Leaving ant's tooling​

  • migrations/versions is plain SQL in a common layout. golang-migrate runs the .up.sql files as they are.
  • script --plain joins everything into one file for any other tool.
  • schema-dump prints the whole schema as SQL, from the structs.
  • The history table is an ordinary table you can drop.
  • Queries don't depend on migrations at all.