https://github.com/danielfbm/migrate
https://github.com/danielfbm/migrate
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/danielfbm/migrate
- Owner: danielfbm
- License: mit
- Created: 2018-02-27T02:53:21.000Z (over 8 years ago)
- Default Branch: v1.3.1-vendor
- Last Pushed: 2018-02-27T03:19:07.000Z (over 8 years ago)
- Last Synced: 2024-12-26T07:09:29.085Z (over 1 year ago)
- Language: Go
- Size: 3.82 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
__[v3.0 in the making](https://github.com/danielfbm/migrate/tree/v3.0-prev)__
---
# migrate
[](https://travis-ci.org/mattes/migrate)
[](https://godoc.org/github.com/danielfbm/migrate)
A migration helper written in Go. Use it in your existing Golang code
or run commands via the CLI.
```
GoCode import github.com/danielfbm/migrate/migrate
CLI go get -u github.com/danielfbm/migrate
```
__Features__
* Super easy to implement [Driver interface](http://godoc.org/github.com/danielfbm/migrate/driver#Driver).
* 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``.
## Available Drivers
* [PostgreSQL](driver/postgres)
* [Cassandra](driver/cassandra)
* [SQLite](driver/sqlite3)
* [MySQL](driver/mysql) ([experimental](https://github.com/danielfbm/migrate/issues/1#issuecomment-58728186))
* [Neo4j](driver/neo4j)
* [Ql](driver/ql)
* [MongoDB](driver/mongodb)
* [CrateDB](driver/crate)
Need another driver? Just implement the [Driver interface](http://godoc.org/github.com/danielfbm/migrate/driver#Driver) and open a PR.
## Usage from Terminal
```bash
# install
go get github.com/danielfbm/migrate
# create new migration file in path
migrate -url driver://url -path ./migrations create migration_file_xyz
# apply all available migrations
migrate -url driver://url -path ./migrations up
# roll back all migrations
migrate -url driver://url -path ./migrations down
# roll back the most recently applied migration, then run it again.
migrate -url driver://url -path ./migrations redo
# run down and then up command
migrate -url driver://url -path ./migrations reset
# show the current migration version
migrate -url driver://url -path ./migrations version
# apply the next 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
# 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
# go to specific migration
migrate -url driver://url -path ./migrations goto 1
migrate -url driver://url -path ./migrations goto 10
migrate -url driver://url -path ./migrations goto v
```
## Usage in Go
See GoDoc here: http://godoc.org/github.com/danielfbm/migrate/migrate
```go
import "github.com/danielfbm/migrate/migrate"
// Import any required drivers so that they are registered and available
import _ "github.com/danielfbm/migrate/driver/mysql"
// use synchronous versions of migration functions ...
allErrors, ok := migrate.UpSync("driver://url", "./path")
if !ok {
fmt.Println("Oh no ...")
// do sth with allErrors slice
}
// use the asynchronous version of migration functions ...
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.
```
## Migration files
The format of migration files looks like this:
```
1481574547_initial_plan_to_do_sth.up.sql # up migration instructions
1481574547_initial_plan_to_do_sth.down.sql # down migration instructions
1482438365_xxx.up.sql
1482438365_xxx.down.sql
...
```
Why two files? This way you could still do sth like
``psql -f ./db/migrations/1481574547_initial_plan_to_do_sth.up.sql`` and there is no
need for any custom markup language to divide up and down migrations. Please note
that the filename extension depends on the driver.
## Alternatives
* https://bitbucket.org/liamstask/goose
* https://github.com/tanel/dbmigrate
* https://github.com/BurntSushi/migration
* https://github.com/DavidHuie/gomigrate
* https://github.com/rubenv/sql-migrate