diff --git a/README.md b/README.md index 6b356ab..cdc8eb7 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,17 @@ # Go database migrate This is a simple database migration library for Go. This library is intended to be used with go-bindata. + We recommended using a go generate command to update the go-bindata file. +## Installation + +You can install this library with go get + +``` +go get git.fuyu.moe/Fuyu/migrate +``` + ## Example `files/0001.sql`: @@ -16,7 +25,8 @@ INSERT INTO tests VALUES ('migration test'); ``` Run `go-bindata --prefix files files` -*Optionally add the flags `-nomemcopy` and `-nometadata`* + +*Optionally you can add the flags `-nomemcopy` and `-nometadata`* `main.go`: @@ -24,17 +34,23 @@ Run `go-bindata --prefix files files` package main import ( - "migrate" + "database/sql" + "fmt" + + "git.fuyu.moe/Fuyu/migrate" + _ "github.com/lib/pq" ) func main() { - db, err = sql.Open(`postgres`, `host=/run/postgresql dbname=testdb sslmode=disable`) + db, err := sql.Open(`postgres`, `host=/run/postgresql dbname=testdb sslmode=disable`) if err != nil { fmt.Println(`Failed to connect to database. Message:`, err) + return } - err = migrate.Migrate(db, 1, migrate.Options{Schema:`testschema`}, migrations.Asset} + err = migrate.Migrate(db, 1, migrate.Options{Schema: `testschema`}, Asset) if err != nil { fmt.Println(`The migration failed! Message:`, err) + return } fmt.Println(`The database migration/update was successful`) diff --git a/migrate.go b/migrate.go index 1a73772..ec6db3f 100644 --- a/migrate.go +++ b/migrate.go @@ -29,7 +29,8 @@ const fileFormat = `%04d.sql` // AssetFunc is a function that returns the data for the given name type AssetFunc func(string) ([]byte, error) -// Migrate executes +// Migrate executes migrations to get to the desired version +// Downgrading is not supported as it could result in data loss func Migrate(db *sql.DB, version int, o Options, asset AssetFunc) error { if o.TableName == `` { o.TableName = DefaultTableName