2014-08-11 01:42:57 +00:00
|
|
|
# migrate
|
|
|
|
|
|
|
|
[![Build Status](https://travis-ci.org/mattes/migrate.svg?branch=master)](https://travis-ci.org/mattes/migrate)
|
2014-08-11 03:06:38 +00:00
|
|
|
[![GoDoc](https://godoc.org/github.com/mattes/migrate?status.svg)](https://godoc.org/github.com/mattes/migrate)
|
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
migrate is a migration helper written in Go. Use it in your existing Golang code
|
|
|
|
or run migration commands via the CLI.
|
2014-08-11 01:42:57 +00:00
|
|
|
|
2014-08-13 01:40:36 +00:00
|
|
|
```
|
|
|
|
Go Code import github.com/mattes/migrate/migrate
|
|
|
|
CLI go get github.com/mattes/migrate
|
|
|
|
```
|
2014-08-11 01:42:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
## Available Drivers
|
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
* [PostgreSQL](https://github.com/mattes/migrate/tree/master/driver/postgres)
|
2014-08-11 01:42:57 +00:00
|
|
|
* 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
|
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
# create new migration file in path
|
|
|
|
migrate -url driver://url -path ./migrations create migration_file_xyz
|
2014-08-11 01:42:57 +00:00
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
# apply all available migrations
|
|
|
|
migrate -url driver://url -path ./migrations up
|
2014-08-11 01:42:57 +00:00
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
# roll back all migrations
|
|
|
|
migrate -url driver://url -path ./migrations down
|
2014-08-11 01:42:57 +00:00
|
|
|
|
|
|
|
# roll back the most recently applied migration, then run it again.
|
2014-08-13 00:38:29 +00:00
|
|
|
migrate -url driver://url -path ./migrations redo
|
2014-08-11 01:42:57 +00:00
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
# run down and then up command
|
|
|
|
migrate -url driver://url -path ./migrations reset
|
2014-08-11 01:42:57 +00:00
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
# show the current migration version
|
|
|
|
migrate -url driver://url -path ./migrations version
|
2014-08-11 01:42:57 +00:00
|
|
|
|
|
|
|
# apply the next n migrations
|
2014-08-13 00:38:29 +00:00
|
|
|
migrate -url driver://url -path ./migrations migrate +1
|
|
|
|
migrate -url driver://url -path ./migrations migrate +2
|
|
|
|
migrate -url driver://url -path ./migrations migrate +n
|
|
|
|
|
|
|
|
# roll back the previous n migrations
|
|
|
|
migrate -url driver://url -path ./migrations migrate -1
|
|
|
|
migrate -url driver://url -path ./migrations migrate -2
|
|
|
|
migrate -url driver://url -path ./migrations migrate -n
|
2014-08-11 01:42:57 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
## Usage in Go
|
2014-08-11 01:42:57 +00:00
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
See GoDoc here: http://godoc.org/github.com/mattes/migrate/migrate
|
2014-08-11 01:42:57 +00:00
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
```go
|
2014-08-11 01:42:57 +00:00
|
|
|
import "github.com/mattes/migrate/migrate"
|
|
|
|
|
2014-08-13 01:40:36 +00:00
|
|
|
// use synchronous versions of migration functions ...
|
|
|
|
// means: run the migrations and return a slice of errors at the end
|
2014-08-13 00:38:29 +00:00
|
|
|
errors, ok := migrate.UpSync("driver://url", "./path")
|
|
|
|
if !ok {
|
|
|
|
fmt.Println("Oh no ...")
|
|
|
|
// do sth with error slice
|
|
|
|
}
|
|
|
|
|
2014-08-13 01:40:36 +00:00
|
|
|
// use the asynchronous version of migration functions ...
|
2014-08-13 00:38:29 +00:00
|
|
|
pipe := migrate.NewPipe()
|
|
|
|
go migrate.Up(pipe, "driver://url", "./path")
|
|
|
|
// pipe is basically just a channel
|
|
|
|
// write your own channel listener. see writePipe() in main.go as an example.
|
2014-08-11 01:42:57 +00:00
|
|
|
```
|
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
## Migrations files
|
|
|
|
|
|
|
|
The format of migration files looks like this:
|
2014-08-11 01:42:57 +00:00
|
|
|
|
|
|
|
```
|
2014-08-13 00:38:29 +00:00
|
|
|
001_initial_plan_to_do_sth.up.sql # up migration instructions
|
|
|
|
001_initial_plan_to_do_sth.down.sql # down migration instructions
|
2014-08-11 01:42:57 +00:00
|
|
|
```
|
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
Why two files? This way you could still do sth like
|
|
|
|
``psql -f ./db/migrations/001_initial_plan_to_do_sth.up.sql`` and there is no
|
|
|
|
need for any custom markup language to divide up and down migrations.
|
2014-08-11 01:42:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
## Credits
|
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
* https://bitbucket.org/liamstask/goose
|
|
|
|
|
|
|
|
|