2017-02-11 11:13:27 -08:00
|
|
|
# migrate CLI
|
2017-02-09 20:00:38 -08:00
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
2018-07-05 10:53:05 -07:00
|
|
|
#### Download pre-build binary (Windows, MacOS, or Linux)
|
|
|
|
|
|
|
|
[Release Downloads](https://github.com/golang-migrate/migrate/releases)
|
2017-02-11 11:13:27 -08:00
|
|
|
|
2017-02-09 20:00:38 -08:00
|
|
|
```
|
2018-07-05 10:53:05 -07:00
|
|
|
$ curl -L https://github.com/golang-migrate/migrate/releases/download/$version/migrate.$platform-amd64.tar.gz | tar xvz
|
2017-02-09 20:00:38 -08:00
|
|
|
```
|
|
|
|
|
2017-02-11 11:13:27 -08:00
|
|
|
#### MacOS
|
|
|
|
|
2018-10-29 17:27:48 +01:00
|
|
|
```
|
|
|
|
$ brew install golang-migrate
|
|
|
|
```
|
2017-02-11 11:13:27 -08:00
|
|
|
|
2017-02-23 13:36:34 -08:00
|
|
|
#### Linux (*.deb package)
|
2017-02-11 11:13:27 -08:00
|
|
|
|
|
|
|
```
|
2018-01-20 02:10:22 -08:00
|
|
|
$ curl -L https://packagecloud.io/golang-migrate/migrate/gpgkey | apt-key add -
|
2018-11-08 02:04:39 -08:00
|
|
|
$ echo "deb https://packagecloud.io/golang-migrate/migrate/ubuntu/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/migrate.list
|
2017-02-11 11:13:27 -08:00
|
|
|
$ apt-get update
|
2017-02-23 13:36:34 -08:00
|
|
|
$ apt-get install -y migrate
|
2017-02-11 11:13:27 -08:00
|
|
|
```
|
|
|
|
|
2018-07-05 10:53:05 -07:00
|
|
|
#### With Go toolchain
|
2017-02-11 11:13:27 -08:00
|
|
|
|
2018-11-28 01:00:46 -08:00
|
|
|
##### Unversioned
|
2017-02-11 11:13:27 -08:00
|
|
|
```
|
2018-11-28 00:44:59 -08:00
|
|
|
$ go get -tags 'postgres' -u github.com/golang-migrate/migrate/cmd/migrate
|
2017-02-11 11:13:27 -08:00
|
|
|
```
|
|
|
|
|
2018-11-28 01:00:46 -08:00
|
|
|
##### Versioned
|
|
|
|
|
|
|
|
```
|
|
|
|
$ go get -u -d github.com/golang-migrate/migrate/cmd/migrate
|
|
|
|
$ cd $GOPATH/src/github.com/golang-migrate/migrate/cmd/migrate
|
|
|
|
$ git checkout $TAG # e.g. v4.1.0
|
|
|
|
$ go build -tags 'postgres' -ldflags="-X main.Version=$(git describe --tags)" -o $GOPATH/bin/migrate github.com/golang-migrate/migrate/cmd/migrate
|
|
|
|
```
|
|
|
|
|
2018-07-05 10:53:05 -07:00
|
|
|
##### Notes:
|
2018-10-29 10:55:22 -07:00
|
|
|
1. Requires a version of Go that [supports modules](https://golang.org/cmd/go/#hdr-Preliminary_module_support). e.g. Go 1.11+
|
2018-07-05 10:53:05 -07:00
|
|
|
1. This example builds the cli which will only work with postgres. In order
|
|
|
|
to build the cli for use with other databases, replace the `postgres` build tag
|
|
|
|
with the appropriate database tag(s) for the databases desired. The tags
|
|
|
|
correspond to the names of the sub-packages underneath the
|
|
|
|
[`database`](../database) package.
|
|
|
|
1. Similarly to the database build tags, if you need to support other sources, use the appropriate build tag(s).
|
|
|
|
1. Support for build constraints will be removed in the future: https://github.com/golang-migrate/migrate/issues/60
|
2017-02-11 11:13:27 -08:00
|
|
|
|
2017-02-09 20:00:38 -08:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
```
|
|
|
|
$ migrate -help
|
|
|
|
Usage: migrate OPTIONS COMMAND [arg...]
|
|
|
|
migrate [ -version | -help ]
|
|
|
|
|
|
|
|
Options:
|
2017-02-11 19:15:54 -08:00
|
|
|
-source Location of the migrations (driver://url)
|
2017-07-11 21:42:51 -07:00
|
|
|
-path Shorthand for -source=file://path
|
2017-02-11 19:15:54 -08:00
|
|
|
-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
|
2017-02-09 20:00:38 -08:00
|
|
|
|
|
|
|
Commands:
|
2018-06-26 09:16:04 -07:00
|
|
|
create [-ext E] [-dir D] [-seq] [-digits N] [-format] NAME
|
|
|
|
Create a set of timestamped up/down migrations titled NAME, in directory D with extension E.
|
|
|
|
Use -seq option to generate sequential up/down migrations with N digits.
|
|
|
|
Use -format option to specify a Go time format string.
|
2017-02-09 20:00:38 -08:00
|
|
|
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
|
2017-02-19 15:15:00 -08:00
|
|
|
force V Set version V but don't run migration (ignores dirty state)
|
2017-02-09 20:00:38 -08:00
|
|
|
version Print current migration version
|
2017-02-11 11:13:27 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
So let's say you want to run the first two migrations
|
2017-02-09 20:00:38 -08:00
|
|
|
|
2017-02-11 11:13:27 -08:00
|
|
|
```
|
2018-08-05 23:43:55 +02:00
|
|
|
$ migrate -source file://path/to/migrations -database postgres://localhost:5432/database up 2
|
2017-02-11 11:13:27 -08:00
|
|
|
```
|
2017-02-09 20:00:38 -08:00
|
|
|
|
2017-02-11 11:13:27 -08:00
|
|
|
If your migrations are hosted on github
|
2017-02-09 20:00:38 -08:00
|
|
|
|
|
|
|
```
|
2017-02-11 11:13:27 -08:00
|
|
|
$ 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"]')"
|
|
|
|
```
|