migrate/README.md

110 lines
3.0 KiB
Markdown
Raw Normal View History

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 21:58:30 +00:00
A migration helper written in Go. Use it in your existing Golang code
or run commands via the CLI.
2014-08-11 01:42:57 +00:00
2014-08-13 01:40:36 +00:00
```
2014-08-13 21:58:30 +00:00
GoCode import github.com/mattes/migrate/migrate
2014-08-13 01:40:36 +00:00
CLI go get github.com/mattes/migrate
```
2014-08-11 01:42:57 +00:00
2014-08-13 21:59:01 +00:00
__Features__
2014-08-13 21:58:30 +00:00
* Super easy to implement Driver interface.
* Gracefully quit running migrations on ``^C``.
* No magic search paths routines, no hard-coded config files.
* CLI is build on top of the ``migrate package``.
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 ...
2014-08-13 02:04:58 +00:00
allErrors, ok := migrate.UpSync("driver://url", "./path")
2014-08-13 00:38:29 +00:00
if !ok {
fmt.Println("Oh no ...")
2014-08-13 02:04:58 +00:00
// do sth with allErrors slice
2014-08-13 00:38:29 +00:00
}
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 02:04:58 +00:00
## Migration files
2014-08-13 00:38:29 +00:00
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-13 02:04:58 +00:00
002_xxx.up.sql
002_xxx.down.sql
...
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
2014-08-13 02:05:43 +00:00
need for any custom markup language to divide up and down migrations. Please note
that the filename extension depends on the driver.
2014-08-11 01:42:57 +00:00
2014-08-13 12:06:02 +00:00
## Alternatives
2014-08-11 01:42:57 +00:00
2014-08-13 00:38:29 +00:00
* https://bitbucket.org/liamstask/goose