goose/examples/go-migrations
Michael Fridman 99d73b7c76
Upgrade golangci-lint (#928)
2025-03-27 21:25:56 -04:00
..
00001_create_users_table.sql feat: add support for `*sql.DB`-registered Go migration (#450) 2023-01-25 08:15:50 -05:00
00002_rename_root.go docs: update the go-migrations examples (#583) 2023-08-11 08:35:22 -04:00
00003_add_user_no_tx.go docs: update the go-migrations examples (#583) 2023-08-11 08:35:22 -04:00
README.md feat: add support for `*sql.DB`-registered Go migration (#450) 2023-01-25 08:15:50 -05:00
main.go Upgrade golangci-lint (#928) 2025-03-27 21:25:56 -04:00

README.md

SQL + Go migrations

This example: Custom goose binary with built-in Go migrations

$ go build -o goose-custom *.go
$ ./goose-custom sqlite3 ./foo.db status
    Applied At                  Migration
    =======================================
    Pending                  -- 00001_create_users_table.sql
    Pending                  -- 00002_rename_root.go
    Pending                  -- 00003_add_user_no_tx.go

$ ./goose-custom sqlite3 ./foo.db up
    OK   00001_create_users_table.sql (711.58µs)
    OK   00002_rename_root.go (302.08µs)
    OK   00003_add_user_no_tx.go (648.71µs)
    goose: no migrations to run. current version: 3

$ ./goose-custom sqlite3 ./foo.db status
    Applied At                  Migration
    =======================================
    00001_create_users_table.sql
    00002_rename_root.go
    00003_add_user_no_tx.go

Best practice: Split migrations into a standalone package

  1. Move main.go into your cmd/ directory

  2. Rename package name in all *_.go migration files from main to migrations.

  3. Import this migrations package from your custom cmd/main.go file:

    import (
        // Invoke init() functions within migrations pkg.
        _ "github.com/pressly/goose/example/migrations-go"
    )