migrate/cli/README.md

118 lines
3.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
#### Download pre-build binary (Windows, MacOS, or Linux)
[Release Downloads](https://github.com/golang-migrate/migrate/releases)
2017-02-11 19:13:27 +00:00
2017-02-10 04:00:38 +00:00
```
$ curl -L https://github.com/golang-migrate/migrate/releases/download/$version/migrate.$platform-amd64.tar.gz | tar xvz
2017-02-10 04:00:38 +00:00
```
2017-02-11 19:13:27 +00:00
#### MacOS
2018-05-12 22:25:19 +00:00
We have not released support for homebrew yet, but there is a live issue here: [todo #156](https://github.com/mattes/migrate/issues/156)
2017-02-17 05:12:33 +00:00
Any help to make this happen would be appreciated!
2017-02-11 19:13:27 +00:00
2017-02-23 21:36:34 +00:00
#### Linux (*.deb package)
2017-02-11 19:13:27 +00:00
```
2018-01-20 10:10:22 +00:00
$ curl -L https://packagecloud.io/golang-migrate/migrate/gpgkey | apt-key add -
$ echo "deb https://packagecloud.io/golang-migrate/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
```
#### With Go toolchain
2017-02-11 19:13:27 +00:00
```
$ go get -u -d github.com/golang-migrate/migrate/cli
$ cd $GOPATH/src/github.com/golang-migrate/migrate/cli
$ dep ensure
$ go build -tags 'postgres' -o /usr/local/bin/migrate github.com/golang-migrate/migrate/cli
2017-02-11 19:13:27 +00:00
```
##### Notes:
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 19:13:27 +00:00
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:
2018-06-26 16:16:04 +00: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-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 -source file://path/to/migrations -database postgres://localhost:5432/database up 2
2017-02-11 19:13:27 +00:00
```
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"]')"
```