Skip to main content

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.

FieldDefaultPurpose
DirmigrationsThe folder
VersionsDir, CodeDirversions, codeThe two folders inside it
FS, EmbeddednoneRead from go:embed, always, or when the folder is missing
SourcefilesReplace storage entirely
PerDialectoffKeep each dialect's files in Dir/<dialect>
Name, Parse0001_name.up.sqlWrite and read file names, with migrate.Timestamped built in
Renderschema.ScriptReplace how a plan becomes SQL
HistoryTable, HistorySchemaant_migrationsWhere applied migrations are recorded
DSNnoneThe fallback after --dsn and DATABASE_URL
SkipnoneVersions to leave unapplied
OutOfOrderoffLet an older pending migration run
IgnorenoneNames for drift to overlook, such as legacy_* or users.handmade_idx: see adopting
NoLockoffTurn off the migration lock
SeedHooksoffRun model hooks while seeding
Out, OnApplyquiet in UpProgress 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.