2016-10-21 20:01:00 +02:00
|
|
|
# Go database migrate
|
|
|
|
|
|
|
|
This is a simple database migration library for Go. This library is intended to be used with go-bindata.
|
2016-10-21 22:48:11 +02:00
|
|
|
|
2016-10-21 20:01:00 +02:00
|
|
|
We recommended using a go generate command to update the go-bindata file.
|
|
|
|
|
2016-10-21 22:48:11 +02:00
|
|
|
## Installation
|
|
|
|
|
|
|
|
You can install this library with go get
|
|
|
|
|
|
|
|
```
|
|
|
|
go get git.fuyu.moe/Fuyu/migrate
|
|
|
|
```
|
|
|
|
|
2016-10-21 20:01:00 +02:00
|
|
|
## Example
|
|
|
|
|
|
|
|
`files/0001.sql`:
|
|
|
|
|
|
|
|
```sql
|
|
|
|
CREATE TABLE tests (
|
|
|
|
Name varchar(100) NOT NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
INSERT INTO tests VALUES ('migration test');
|
|
|
|
```
|
|
|
|
|
|
|
|
Run `go-bindata --prefix files files`
|
2016-10-21 22:48:11 +02:00
|
|
|
|
|
|
|
*Optionally you can add the flags `-nomemcopy` and `-nometadata`*
|
2016-10-21 20:01:00 +02:00
|
|
|
|
|
|
|
`main.go`:
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-10-21 22:48:11 +02:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.fuyu.moe/Fuyu/migrate"
|
|
|
|
_ "github.com/lib/pq"
|
2016-10-21 20:01:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-10-21 22:48:11 +02:00
|
|
|
db, err := sql.Open(`postgres`, `host=/run/postgresql dbname=testdb sslmode=disable`)
|
2016-10-21 20:01:00 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(`Failed to connect to database. Message:`, err)
|
2016-10-21 22:48:11 +02:00
|
|
|
return
|
2016-10-21 20:01:00 +02:00
|
|
|
}
|
2016-10-21 22:48:11 +02:00
|
|
|
err = migrate.Migrate(db, 1, migrate.Options{Schema: `testschema`}, Asset)
|
2016-10-21 20:01:00 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(`The migration failed! Message:`, err)
|
2016-10-21 22:48:11 +02:00
|
|
|
return
|
2016-10-21 20:01:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(`The database migration/update was successful`)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## File names:
|
|
|
|
|
|
|
|
The names of your migration files should look like this:
|
|
|
|
|
|
|
|
```
|
|
|
|
0001.sql
|
|
|
|
0002.sql
|
|
|
|
...
|
|
|
|
0010.sql
|
|
|
|
...
|
|
|
|
0325.sql
|
|
|
|
```
|