migrate/cli
kshvakov e86f4a201c change include path from github.com/kshvakov to github.com/mattes 2017-06-21 17:24:55 +03:00
..
examples add example dockerfile 2017-02-23 13:13:02 -08:00
README.md add create command to cli 2017-06-14 21:59:11 +02:00
build_aws-s3.go build cli with all source and database drivers 2017-05-17 19:10:45 -07:00
build_cassandra.go add cassandra driver and function to retrieve networkSettings to get port bound to 9042 2017-06-05 11:03:49 +01:00
build_clickhouse.go change include path from github.com/kshvakov to github.com/mattes 2017-06-21 17:24:55 +03:00
build_github.go initial version 3.0.0 preview 2017-02-07 22:01:29 -08:00
build_go-bindata.go initial version 3.0.0 preview 2017-02-07 22:01:29 -08:00
build_google-cloud-storage.go build cli with all source and database drivers 2017-05-17 19:10:45 -07:00
build_mysql.go add mysql driver, add ENV to docker containers 2017-02-28 15:10:56 -08:00
build_postgres.go initial version 3.0.0 preview 2017-02-07 22:01:29 -08:00
build_ql.go build cli with all source and database drivers 2017-05-17 19:10:45 -07:00
build_redshift.go Added support for Redshift. 2017-05-11 15:47:40 -06:00
build_spanner.go Add Spanner driver 2017-06-04 21:13:49 +01:00
build_sqlite3.go added file cli/build_sqlite3.go 2017-06-05 11:02:51 +01:00
commands.go add create command to cli 2017-06-14 21:59:11 +02:00
log.go initial version 3.0.0 preview 2017-02-07 22:01:29 -08:00
main.go add create command to cli 2017-06-14 21:59:11 +02:00
version.go initial version 3.0.0 preview 2017-02-07 22:01:29 -08:00

README.md

migrate CLI

Installation

With Go toolchain

$ go get -u -d github.com/mattes/migrate/cli github.com/lib/pq
$ go build -tags 'postgres' -o /usr/local/bin/migrate github.com/mattes/migrate/cli

MacOS

(todo #156)

$ brew install migrate --with-postgres

Linux (*.deb package)

$ curl -L https://packagecloud.io/mattes/migrate/gpgkey | apt-key add -
$ echo "deb https://packagecloud.io/mattes/migrate/ubuntu/ xenial main" > /etc/apt/sources.list.d/migrate.list
$ apt-get update
$ apt-get install -y migrate

Download pre-build binary (Windows, MacOS, or Linux)

Release Downloads

$ curl -L https://github.com/mattes/migrate/releases/download/$version/migrate.$platform-amd64.tar.gz | tar xvz

Usage

$ 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)
  -lock-timeout N  Allow N seconds to acquire database lock (default 15)
  -verbose         Print verbose logging
  -version         Print version
  -help            Print usage

Commands:
  create [-ext E] [-dir D] NAME
               Create a set of timestamped up/down migrations titled NAME, in directory D with extension E
  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
  force V      Set version V but don't run migration (ignores dirty state)
  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

The CLI will gracefully stop at a safe point when SIGINT (ctrl+c) is received. Send SIGKILL for immediate halt.

Reading CLI arguments from somewhere else

ENV variables
$ migrate -database "$MY_MIGRATE_DATABASE"
JSON files

Check out https://stedolan.github.io/jq/

$ migrate -database "$(cat config.json | jq '.database')"
YAML files
$ migrate -database "$(cat config/database.yml | ruby -ryaml -e "print YAML.load(STDIN.read)['database']")"
$ migrate -database "$(cat config/database.yml | python -c 'import yaml,sys;print yaml.safe_load(sys.stdin)["database"]')"
```