Migrate for Go

github.com/malcolmston/migrate

ActiveRecord-style schema migrations for Go.

 GitHubports rails/rails

A small, dependency-free, ActiveRecord-flavoured schema-migration toolkit for Go built entirely on top of the standard library's database/sql package — no third-party packages, no cgo. A Migration carries a uint64 Version, a Name, and a pair of directions expressed either as a Go func(ctx, *sql.Tx) error or as raw SQL text; a Migrator wraps a *sql.DB, maintains a schema_migrations bookkeeping table, and drives migrations forward and backward with Migrate, Up, Down, Rollback, MigrateTo, Redo and Status. Every migration runs in its own transaction, so a failure rolls back cleanly and re-running is idempotent. Migrations register programmatically or load from a directory / io/fs.FS of <version>_<name>.up.sql / .down.sql pairs, and a tiny schema DSL (CreateTable, AddColumn, AddIndex, typed column helpers, foreign keys) emits predictable, greppable ANSI SQL.

Install

shell
$ go get github.com/malcolmston/migrate

Quick start

main.go
import "github.com/malcolmston/migrate"

mg := migrate.New(db) // any database/sql *sql.DB
mg.Register(migrate.Migration{
    Version: 20240101,
    Name:    "create_users",
    UpSQL: migrate.CreateTable("users", func(t *migrate.Table) {
        t.String("email", migrate.NotNull(), migrate.Unique())
        t.Timestamps()
    }),
    DownSQL: migrate.DropTable("users"),
})
if err := mg.Migrate(context.Background()); err != nil {
    log.Fatal(err)
}

Features

  • Versioned, reversible migrations — a Migration with a uint64 Version, applied ascending and rolled back descending
  • Go or SQL directions — Up/Down as func(ctx, *sql.Tx) error, or UpSQL/DownSQL raw text
  • Transaction per migration — a failure rolls back, does not record the version, and halts; re-running Migrate is idempotent
  • Full command set — Migrate, Up, Down, Rollback, MigrateTo, Redo and Status on the Migrator
  • File loading — LoadDir and LoadFS read <version>_<name>.up.sql / .down.sql pairs from any io/fs.FS
  • Schema DSL — CreateTable with typed helpers (String, Text, Integer, Boolean, Timestamps) plus AddColumn, AddIndex, DropTable, RenameColumn
  • Rails-style references & foreign keys — Table.References with WithForeignKey, ReferenceNotNull and ReferenceTable
  • Zero dependencies — pure Go standard library over database/sql, no cgo, nothing to audit but the toolchain
Migrate