Database migrations. CLI and Golang library.
Go to file
Matthias Kadenbach e6163082d7 add restore-import-paths and rewrite-import-paths 2017-02-08 18:01:01 -08:00
cli initial version 3.0.0 preview 2017-02-07 22:01:29 -08:00
database fixing travis 2017-02-08 01:49:57 -08:00
migrate initial version 3.0.0 preview 2017-02-07 22:01:29 -08:00
source add go coverage 2017-02-08 16:17:04 -08:00
testing fixing travis 2017-02-08 01:49:57 -08:00
.gitignore add go coverage 2017-02-08 16:17:04 -08:00
.travis.yml add go coverage 2017-02-08 16:17:04 -08:00
LICENSE initial version 3.0.0 preview 2017-02-07 22:01:29 -08:00
Makefile add restore-import-paths and rewrite-import-paths 2017-02-08 18:01:01 -08:00
README.md add go coverage 2017-02-08 16:17:04 -08:00
log.go initial version 3.0.0 preview 2017-02-07 22:01:29 -08:00
migrate.go initial version 3.0.0 preview 2017-02-07 22:01:29 -08:00
migrate_test.go initial version 3.0.0 preview 2017-02-07 22:01:29 -08:00
migration.go initial version 3.0.0 preview 2017-02-07 22:01:29 -08:00
util.go initial version 3.0.0 preview 2017-02-07 22:01:29 -08:00

README.md

migrate

Build Status GoDoc Coverage Status

Database migrations written in Go. Use as CLI or import as library.

go get -u -tags 'postgres' -o migrate github.com/mattes/migrate/cli

import (
  "github.com/mattes/migrate"
  _ "github.com/mattes/migrate/database/postgres"
)

Databases

Database drivers are responsible for applying migrations to databases. Implementing a new database driver is easy. Just implement database/driver interface

Migration Sources

Source Drivers read migrations from various locations. Implementing a new source driver is easy. Just implement the source/driver interface.

CLI usage

# dowload, build and install the CLI tool
# -tags takes database and source drivers and will only build those
$ go get -u -tags 'postgres' -o migrate github.com/mattes/migrate/cli

$ migrate -help
Usage: migrate OPTIONS COMMAND [arg...]
       migrate [ -version | -help ]

Options:
  -source      Location of the migrations (driver://url)
  -path        Shorthand for -source=file://path
  -database    Run migrations against this database (driver://url)
  -prefetch N  Number of migrations to load in advance before executing (default 10)
  -verbose     Print verbose logging
  -version     Print version
  -help        Print usage

Commands:
  goto V       Migrate to version V
  up [N]       Apply all or N up migrations
  down [N]     Apply all or N down migrations
  drop         Drop everyting inside database
  version      Print current migration version


# so let's say you want to run the first two migrations
migrate -database postgres://localhost:5432/database up 2

# if your migrations are hosted on github
migrate -source github://mattes:personal-access-token@mattes/migrate_test \
  -database postgres://localhost:5432/database down 2

Use in your Go project

import (
  "github.com/mattes/migrate/migrate"
  _ "github.com/mattes/migrate/database/postgres"
  _ "github.com/mattes/migrate/source/github"
)

func main() {
  m, err := migrate.New("github://mattes:personal-access-token@mattes/migrate_test",
    "postgres://localhost:5432/database?sslmode=enable")
  m.Steps(2)
}

Migration files

Each migration version has an up and down migration.

1481574547_create_users_table.up.sql
1481574547_create_users_table.down.sql

Development and Testing

Tests require Docker (for database driver testing).

make test-short DATABASE='postgres'
make test

Alternatives