migrate/README.md

125 lines
3.9 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)
2014-08-11 05:06:38 +02:00
[![GoDoc](https://godoc.org/github.com/mattes/migrate?status.svg)](https://godoc.org/github.com/mattes/migrate)
2014-08-13 23:58:30 +02:00
A migration helper written in Go. Use it in your existing Golang code
or run commands via the CLI.
2014-08-11 03:42:57 +02:00
2014-08-13 03:40:36 +02:00
```
2014-08-13 23:58:30 +02:00
GoCode import github.com/mattes/migrate/migrate
2014-10-07 11:32:33 +02:00
CLI go get -u github.com/mattes/migrate
2014-08-13 03:40:36 +02:00
```
2014-08-11 03:42:57 +02:00
2014-08-13 23:59:01 +02:00
__Features__
2014-08-13 23:58:30 +02:00
2014-08-14 00:05:58 +02:00
* Super easy to implement [Driver interface](http://godoc.org/github.com/mattes/migrate/driver#Driver).
2014-08-13 23:58:30 +02:00
* 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 03:42:57 +02:00
## Available Drivers
2014-08-13 02:38:29 +02:00
* [PostgreSQL](https://github.com/mattes/migrate/tree/master/driver/postgres)
* [Cassandra](https://github.com/mattes/migrate/tree/master/driver/cassandra)
2015-03-08 10:58:38 +01:00
* [SQLite](https://github.com/mattes/migrate/tree/master/driver/sqlite3)
2014-10-11 01:32:26 +02:00
* [MySQL](https://github.com/mattes/migrate/tree/master/driver/mysql) ([experimental](https://github.com/mattes/migrate/issues/1#issuecomment-58728186))
2014-08-11 03:42:57 +02: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 02:38:29 +02:00
# create new migration file in path
migrate -url driver://url -path ./migrations create migration_file_xyz
2014-08-11 03:42:57 +02:00
2014-08-13 02:38:29 +02:00
# apply all available migrations
migrate -url driver://url -path ./migrations up
2014-08-11 03:42:57 +02:00
2014-08-13 02:38:29 +02:00
# roll back all migrations
migrate -url driver://url -path ./migrations down
2014-08-11 03:42:57 +02:00
# roll back the most recently applied migration, then run it again.
2014-08-13 02:38:29 +02:00
migrate -url driver://url -path ./migrations redo
2014-08-11 03:42:57 +02:00
2014-08-13 02:38:29 +02:00
# run down and then up command
migrate -url driver://url -path ./migrations reset
2014-08-11 03:42:57 +02:00
2014-08-13 02:38:29 +02:00
# show the current migration version
migrate -url driver://url -path ./migrations version
2014-08-11 03:42:57 +02:00
# apply the next n migrations
2014-08-13 02:38:29 +02: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-10-04 09:48:56 +02:00
# go to specific migration
2014-12-19 02:50:58 -08:00
migrate -url driver://url -path ./migrations goto 1
migrate -url driver://url -path ./migrations goto 10
2014-10-04 09:48:56 +02:00
migrate -url driver://url -path ./migrations goto v
2014-08-11 03:42:57 +02:00
```
2014-08-13 02:38:29 +02:00
## Usage in Go
2014-08-11 03:42:57 +02:00
2014-08-13 02:38:29 +02:00
See GoDoc here: http://godoc.org/github.com/mattes/migrate/migrate
2014-08-11 03:42:57 +02:00
2014-08-13 02:38:29 +02:00
```go
2014-08-11 03:42:57 +02:00
import "github.com/mattes/migrate/migrate"
// Import any required drivers so that they are registered and available
import _ "github.com/mattes/migrate/driver/mysql"
2014-08-13 03:40:36 +02:00
// use synchronous versions of migration functions ...
2014-08-13 04:04:58 +02:00
allErrors, ok := migrate.UpSync("driver://url", "./path")
2014-08-13 02:38:29 +02:00
if !ok {
fmt.Println("Oh no ...")
2014-08-13 04:04:58 +02:00
// do sth with allErrors slice
2014-08-13 02:38:29 +02:00
}
2014-08-13 03:40:36 +02:00
// use the asynchronous version of migration functions ...
2014-08-13 02:38:29 +02: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 03:42:57 +02:00
```
2014-08-13 04:04:58 +02:00
## Migration files
2014-08-13 02:38:29 +02:00
The format of migration files looks like this:
2014-08-11 03:42:57 +02:00
```
2014-08-13 02:38:29 +02: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 04:04:58 +02:00
002_xxx.up.sql
002_xxx.down.sql
...
2014-08-11 03:42:57 +02:00
```
2014-08-13 02:38:29 +02: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 04:05:43 +02: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 03:42:57 +02:00
2014-08-13 14:06:02 +02:00
## Alternatives
2014-08-11 03:42:57 +02:00
2014-08-13 02:38:29 +02:00
* https://bitbucket.org/liamstask/goose
2014-08-15 02:35:23 +02:00
* https://github.com/tanel/dbmigrate
* https://github.com/BurntSushi/migration
2014-08-15 13:51:04 +02:00
* https://github.com/DavidHuie/gomigrate
* https://github.com/rubenv/sql-migrate
2014-08-13 02:38:29 +02:00