Dead simple Go migration tool and library that keeps your migrations inside a binary (or your application's own binary) for ease of use.
go get -v github.com/erizocosmico/mig/...
First thing we should do is the following:
- Go to the root of your project
- Run
mkdir migrations
- Run
mig scaffold --db postgres
(you can change postgres for any of the supported database drivers)
By now we'll have something like this:
| myproject/
|- migrations/
|- cmd/
|- migrate/
|- main.go
That migrate
command is the binary we'll use to manage our migrations. Note that we didn't have to configure anything in the scaffold command other than the database because we used the default ./migrations
as the package for our migrations.
Now we can start writing our migrations.
mig new initial_schema
mig new add_sessions_table
mig new add_profile_picture_column
That command will add new migration files inside the migrations
directory.
You can edit them and place your migrations. It's Go code, so you can do whatever thing you want in there.
The migration files generated will look like this:
package migrations
import "github.com/erizocosmico/mig"
func main() {
mig.Register(
func(db mig.DB) error {
// your up code
},
func(db mig.DB) error {
// your down code
},
)
}
You will be thinking "do I have to make all the execs and if err != nil by hand?". No! mig
's got you covered! There are some utility functions mig.ExecAll
and mig.DropAll
that should cover almost all your use cases. Check them out in the documentation.
Now, to execute you can run the generated command or build it and use it as a binary.
go ./cmd/migrate/main.go --help
or (assuming your ~/$GOPATH/bin
is in your PATH)
go install ./cmd/migrate/...
migrate --help
There are 3 commands available in the migration manager:
up
runs all the migrations.rollback
executes the down for the current version, leaving the database in the previous state e.g. if database is in version 3, this would get it to version 2.to-version
get the database to a specific version.
migrate up --url postgres://postgres:@0.0.0.0:5432/testing?sslmode=disable
You can pass the URL as an environment variable as well:
DBURL=postgres://postgres:@0.0.0.0:5432/testing?sslmode=disable migrate to-version 5
Lucky for you, the API can be used programmatically as well. Do you want to import your migrations? Easy, import them, it's just Go code.
package main
import (
_ "my/package/migrations"
)
// do something
Check the documentation to see the list of available functionality, which is the same that is available using the generated command.
Why could this be useful? In case you want your binary to autoupdate itself accordingly. The downside of this is that all migrations code would be inside your main binary. That's why the mig
tool scaffolds a separate command just for migration management.
go-pg/migrations for the inspiration. This library is basically migrations
but it creates the migrations without needing to query the database or create the manager command yourself for the migrations. It also supports more databases than just PostgreSQL.
MIT, see LICENSE.