Configuration
Pass migrate.Config to migrate.Main for the command line tool, or to migrate.Up to apply migrations from your own code. Every field has a default, so migrate.Config{} works.
| Field | Default | Purpose |
|---|---|---|
Dir | migrations | The folder |
VersionsDir, CodeDir | versions, code | The two folders inside it |
FS, Embedded | none | Read from go:embed, always, or when the folder is missing |
Source | files | Replace storage entirely |
PerDialect | off | Keep each dialect's files in Dir/<dialect> |
Name, Parse | 0001_name.up.sql | Write and read file names, with migrate.Timestamped built in |
Render | schema.Script | Replace how a plan becomes SQL |
HistoryTable, HistorySchema | ant_migrations | Where applied migrations are recorded |
DSN | none | The fallback after --dsn and DATABASE_URL |
Skip | none | Versions to leave unapplied |
OutOfOrder | off | Let an older pending migration run |
Ignore | none | Names for drift to overlook, such as legacy_* or users.handmade_idx: see adopting |
NoLock | off | Turn off the migration lock |
SeedHooks | off | Run model hooks while seeding |
Out, OnApply | quiet in Up | Progress text, and a callback per migration |
Read migrations from somewhere else
Source lists migrations from anywhere, such as a table or an object store. Implement Store too for the commands that write.
type Source interface {
List(ctx context.Context) ([]migrate.Migration, error)
Snapshot(ctx context.Context) ([]byte, error)
}
Name files your own way
Name chooses a new file's name, and Parse reads a version and name back. migrate.Timestamped makes collisions between branches rare: see working in a team.
Change the SQL that is written
Render receives the plan and can wrap the default. This one sets a lock timeout in every generated file:
config := migrate.Config{
Render: func(plan *schema.Plan, d schema.Dialect) (string, error) {
text, err := schema.Script(d, plan)
if err != nil {
return "", err
}
// Directives such as -- ant:no-transaction are read from the
// comments at the top, so the SET goes after them.
header, body, _ := strings.Cut(text, "\n\n")
return header + "\n\nSET lock_timeout = '5s';\n\n" + body, nil
},
}
Anything you add goes after those comments. A directive below the first statement would not be read, so up refuses such a file.
See it tested: tests/features/migrations/source_test.go