2017-02-11 19:13:27 +00:00
|
|
|
# migrate CLI
|
2017-02-10 04:00:38 +00:00
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
2017-02-11 19:13:27 +00:00
|
|
|
#### With Go toolchain
|
|
|
|
|
2017-02-10 04:00:38 +00:00
|
|
|
```
|
|
|
|
$ go get -u -tags 'postgres' -o migrate github.com/mattes/migrate/cli
|
|
|
|
```
|
|
|
|
|
2017-02-11 19:13:27 +00:00
|
|
|
#### MacOS
|
|
|
|
|
2017-02-17 05:12:33 +00:00
|
|
|
([todo #156](https://github.com/mattes/migrate/issues/156))
|
|
|
|
|
2017-02-11 19:13:27 +00:00
|
|
|
```
|
|
|
|
$ brew install migrate --with-postgres
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Linux (with deb package)
|
|
|
|
|
|
|
|
```
|
|
|
|
# TODO: add key and repo
|
|
|
|
$ apt-get update
|
|
|
|
$ apt-get install migrate
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Download pre-build binary (Windows, MacOS, or Linux)
|
|
|
|
|
|
|
|
[Release Downloads](https://github.com/mattes/migrate/releases)
|
|
|
|
|
|
|
|
```
|
|
|
|
$ curl -L https://github.com/mattes/migrate/releases/download/$version/migrate.$platform-amd64.tar.gz | tar xvz
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2017-02-10 04:00:38 +00:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
```
|
|
|
|
$ migrate -help
|
|
|
|
Usage: migrate OPTIONS COMMAND [arg...]
|
|
|
|
migrate [ -version | -help ]
|
|
|
|
|
|
|
|
Options:
|
2017-02-12 03:15:54 +00:00
|
|
|
-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
|
2017-02-10 04:00:38 +00:00
|
|
|
|
|
|
|
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
|
2017-02-11 19:13:27 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
So let's say you want to run the first two migrations
|
2017-02-10 04:00:38 +00:00
|
|
|
|
2017-02-11 19:13:27 +00:00
|
|
|
```
|
|
|
|
$ migrate -database postgres://localhost:5432/database up 2
|
|
|
|
```
|
2017-02-10 04:00:38 +00:00
|
|
|
|
2017-02-11 19:13:27 +00:00
|
|
|
If your migrations are hosted on github
|
2017-02-10 04:00:38 +00:00
|
|
|
|
|
|
|
```
|
2017-02-11 19:13:27 +00: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"]')"
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2017-02-10 04:00:38 +00:00
|
|
|
|