migrate/cli/README.md

108 lines
2.7 KiB
Markdown
Raw Normal View History

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
```
2017-05-18 17:28:39 +00:00
$ go get -u -d github.com/mattes/migrate/cli github.com/lib/pq
2017-04-21 19:23:01 +00:00
$ go build -tags 'postgres' -o /usr/local/bin/migrate github.com/mattes/migrate/cli
2017-02-10 04:00:38 +00:00
```
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
```
2017-02-23 21:36:34 +00:00
#### Linux (*.deb package)
2017-02-11 19:13:27 +00:00
```
2017-02-23 21:36:34 +00:00
$ 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
2017-02-11 19:13:27 +00:00
$ apt-get update
2017-02-23 21:36:34 +00:00
$ apt-get install -y migrate
2017-02-11 19:13:27 +00:00
```
#### 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:
-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:
2017-06-10 12:03:17 +00:00
create [-ext E] [-dir D] NAME
Create a set of timestamped up/down migrations titled NAME, in directory D with extension E
2017-02-10 04:00:38 +00: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 23:15:00 +00:00
force V Set version V but don't run migration (ignores dirty state)
2017-02-10 04:00:38 +00:00
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"]')"
```