migrate/README.md

78 lines
2.2 KiB
Markdown
Raw Normal View History

2014-08-11 03:42:57 +02:00
# migrate
[![Build Status](https://travis-ci.org/mattes/migrate.svg?branch=master)](https://travis-ci.org/mattes/migrate)
migrate can be used as CLI or can be imported into your existing Go code.
## Available Drivers
* [Postgres](https://github.com/mattes/migrate/tree/master/driver/postgres)
* Bash (planned)
Need another driver? Just implement the [Driver interface](http://godoc.org/github.com/mattes/migrate/driver#Driver) and open a PR.
## Usage from Terminal
```bash
# install
go get github.com/mattes/migrate
# create new migration
migrate -url="postgres://user@host:port/database" -path=./db/migrations create
2014-08-11 03:42:57 +02:00
# apply all *up* migrations
migrate -url="postgres://user@host:port/database" -path=./db/migrations up
2014-08-11 03:42:57 +02:00
# apply all *down* migrations
migrate -url="postgres://user@host:port/database" -path=./db/migrations down
2014-08-11 03:42:57 +02:00
# roll back the most recently applied migration, then run it again.
migrate -url="postgres://user@host:port/database" -path=./db/migrations redo
2014-08-11 03:42:57 +02:00
# down and up again
migrate -url="postgres://user@host:port/database" -path=./db/migrations reset
2014-08-11 03:42:57 +02:00
# show current migration version
migrate -url="postgres://user@host:port/database" -path=./db/migrations version
2014-08-11 03:42:57 +02:00
# apply the next n migrations
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate +1
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate +2
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate +n
2014-08-11 03:42:57 +02:00
# apply the *down* migration of the current version
# and the previous n-1 migrations
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate -1
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate -2
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate -n
2014-08-11 03:42:57 +02:00
```
## Usage from within Go
See http://godoc.org/github.com/mattes/migrate/migrate
```golang
import "github.com/mattes/migrate/migrate"
migrate.Up("postgres://user@host:port/database", "./db/migrations")
2014-08-11 03:42:57 +02:00
// ...
// ...
```
## Migrations format
```
./db/migrations/001_initial.up.sql
./db/migrations/001_initial.down.sql
```
Why two files? This way you could do sth like ``psql -f ./db/migrations/001_initial.up.sql``.
## Credits
* https://bitbucket.org/liamstask/goose