Database migrations. CLI and Golang library.
Go to file
Matthias Kadenbach 0861ff957d Update Readme 2017-01-23 13:36:22 -08:00
driver fix merge conflicts 2017-01-23 13:32:48 -08:00
file - Reverted repository from dimag-jfrog to mattes to prepare for pull request 2017-01-07 16:36:25 +02:00
migrate - Reverted repository from dimag-jfrog to mattes to prepare for pull request 2017-01-07 16:36:25 +02:00
pipe refactor cli 2014-08-14 00:22:56 +02:00
.gitignore - Reset all changes to the upstream branch 2017-01-07 16:30:40 +02:00
.travis.yml Bump travis versions to use Go 1.6 and 1.7 2016-10-23 15:05:54 -07:00
Dockerfile Fix test with optionated drivers 2015-10-15 12:49:43 -04:00
LICENSE improve comments and add license 2014-08-13 03:37:48 +02:00
Makefile - docker command should remain 'go-test' 2016-10-23 15:11:04 -07:00
README.md Update Readme 2017-01-23 13:36:22 -08:00
docker-compose.yml - Reverted repository from dimag-jfrog to mattes to prepare for pull request 2017-01-07 16:36:25 +02:00
main.go fix merge conflicts 2017-01-23 13:32:48 -08:00
version.go Update version.go 2017-01-19 14:11:19 +01:00

README.md

migrate

Build Status GoDoc

A migration helper written in Go. Use it in your existing Golang code or run commands via the CLI.

GoCode   import github.com/mattes/migrate/migrate
CLI      go get -u github.com/mattes/migrate

Features

  • Super easy to implement Driver interface.
  • Gracefully quit running migrations on ^C.
  • No magic search paths routines, no hard-coded config files.
  • CLI is build on top of the migrate package.

Available Drivers

Need another driver? Just implement the Driver interface and open a PR.

Usage from Terminal

# install
go get github.com/mattes/migrate

# create new migration file in path
migrate -url driver://url -path ./migrations create migration_file_xyz

# apply all available migrations
migrate -url driver://url -path ./migrations up

# roll back all migrations
migrate -url driver://url -path ./migrations down

# roll back the most recently applied migration, then run it again.
migrate -url driver://url -path ./migrations redo

# run down and then up command
migrate -url driver://url -path ./migrations reset

# show the current migration version
migrate -url driver://url -path ./migrations version

# apply the next n migrations
migrate -url driver://url -path ./migrations migrate +1
migrate -url driver://url -path ./migrations migrate +2
migrate -url driver://url -path ./migrations migrate +n

# roll back the previous n migrations
migrate -url driver://url -path ./migrations migrate -1
migrate -url driver://url -path ./migrations migrate -2
migrate -url driver://url -path ./migrations migrate -n

# go to specific migration
migrate -url driver://url -path ./migrations goto 1
migrate -url driver://url -path ./migrations goto 10
migrate -url driver://url -path ./migrations goto v

Usage in Go

See GoDoc here: http://godoc.org/github.com/mattes/migrate/migrate

import "github.com/mattes/migrate/migrate"

// Import any required drivers so that they are registered and available
import _ "github.com/mattes/migrate/driver/mysql"

// use synchronous versions of migration functions ...
allErrors, ok := migrate.UpSync("driver://url", "./path")
if !ok {
  fmt.Println("Oh no ...")
  // do sth with allErrors slice
}

// use the asynchronous version of migration functions ...
pipe := migrate.NewPipe()
go migrate.Up(pipe, "driver://url", "./path")
// pipe is basically just a channel
// write your own channel listener. see writePipe() in main.go as an example.

Migration files

The format of migration files looks like this:

1481574547_initial_plan_to_do_sth.up.sql     # up migration instructions
1481574547_initial_plan_to_do_sth.down.sql   # down migration instructions
1482438365_xxx.up.sql
1482438365_xxx.down.sql
...

Why two files? This way you could still do sth like psql -f ./db/migrations/1481574547_initial_plan_to_do_sth.up.sql and there is no need for any custom markup language to divide up and down migrations. Please note that the filename extension depends on the driver.

Alternatives