From 12dd99aa2cb1135b2edc4d94efc972bf34083934 Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Mon, 19 Jun 2017 17:28:55 -0400 Subject: [PATCH 1/5] Travis: Bump to go1.8 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8d058c0..fd350fe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ sudo: false language: go go: -- 1.7 +- 1.8 - tip install: From 3690b1fefd2f10841363b59b14eb839fcabfedc4 Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Mon, 19 Jun 2017 17:48:12 -0400 Subject: [PATCH 2/5] Refactor/simplify custom goose example with Go migrations --- example/migrations-go/00002_rename_root.go | 8 ++-- example/migrations-go/{cmd => }/main.go | 47 +++++++++++++++------- 2 files changed, 36 insertions(+), 19 deletions(-) rename example/migrations-go/{cmd => }/main.go (57%) diff --git a/example/migrations-go/00002_rename_root.go b/example/migrations-go/00002_rename_root.go index 7a645aa..069d40a 100644 --- a/example/migrations-go/00002_rename_root.go +++ b/example/migrations-go/00002_rename_root.go @@ -1,4 +1,4 @@ -package migrations +package main import ( "database/sql" @@ -7,10 +7,10 @@ import ( ) func init() { - goose.AddMigration(Up, Down) + goose.AddMigration(Up00002, Down00002) } -func Up(tx *sql.Tx) error { +func Up00002(tx *sql.Tx) error { _, err := tx.Exec("UPDATE users SET username='admin' WHERE username='root';") if err != nil { return err @@ -18,7 +18,7 @@ func Up(tx *sql.Tx) error { return nil } -func Down(tx *sql.Tx) error { +func Down00002(tx *sql.Tx) error { _, err := tx.Exec("UPDATE users SET username='root' WHERE username='admin';") if err != nil { return err diff --git a/example/migrations-go/cmd/main.go b/example/migrations-go/main.go similarity index 57% rename from example/migrations-go/cmd/main.go rename to example/migrations-go/main.go index 6b7dc7d..c589029 100644 --- a/example/migrations-go/cmd/main.go +++ b/example/migrations-go/main.go @@ -9,8 +9,7 @@ import ( "github.com/pressly/goose" - _ "github.com/pressly/goose/example/migrations-go" - + // Init DB drivers. _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" _ "github.com/mattn/go-sqlite3" @@ -27,7 +26,15 @@ func main() { flags.Parse(os.Args[1:]) args := flags.Args() - if len(args) != 3 { + + if len(args) > 1 && args[0] == "create" { + if err := goose.Run("create", nil, *dir, args[1:]...); err != nil { + log.Fatalf("goose run: %v", err) + } + return + } + + if len(args) < 3 { flags.Usage() return } @@ -63,7 +70,12 @@ func main() { log.Fatalf("-dbstring=%q: %v\n", dbstring, err) } - if err := goose.Run(command, db, *dir); err != nil { + arguments := []string{} + if len(args) > 3 { + arguments = append(arguments, args[3:]...) + } + + if err := goose.Run(command, db, *dir, arguments...); err != nil { log.Fatalf("goose run: %v", err) } } @@ -84,23 +96,28 @@ Drivers: redshift Examples: - goose postgres "user=postgres dbname=postgres sslmode=disable" up - goose mysql "user:password@/dbname" down goose sqlite3 ./foo.db status - goose redshift "postgres://user:password@qwerty.us-east-1.redshift.amazonaws.com:5439/db" create init sql + goose sqlite3 ./foo.db create init sql + goose sqlite3 ./foo.db create add_some_column sql + goose sqlite3 ./foo.db create fetch_user_data go + goose sqlite3 ./foo.db up + + goose postgres "user=postgres dbname=postgres sslmode=disable" status + goose mysql "user:password@/dbname" status + goose redshift "postgres://user:password@qwerty.us-east-1.redshift.amazonaws.com:5439/db" status Options: ` usageCommands = ` Commands: - up Migrate the DB to the most recent version available - up-to VERSION Migrate the DB to a specific VERSION - down Roll back the version by 1 - down-to VERSION Roll back to a specific VERSION - redo Re-run the latest migration - status Dump the migration status for the current DB - version Print the current version of the database - create Creates a blank migration template + up Migrate the DB to the most recent version available + up-to VERSION Migrate the DB to a specific VERSION + down Roll back the version by 1 + down-to VERSION Roll back to a specific VERSION + redo Re-run the latest migration + status Dump the migration status for the current DB + version Print the current version of the database + create NAME [sql|go] Creates new migration file with next version ` ) From 4df66b56ef8c03a35c8ccc5c6ce2c51302c09db0 Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Tue, 20 Jun 2017 12:07:18 -0400 Subject: [PATCH 3/5] Add example READMEs --- example/migrations-go/README.md | 42 +++++++++++++++++++++++++++++++++ example/migrations/README.md | 26 ++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 example/migrations-go/README.md create mode 100644 example/migrations/README.md diff --git a/example/migrations-go/README.md b/example/migrations-go/README.md new file mode 100644 index 0000000..1ff883c --- /dev/null +++ b/example/migrations-go/README.md @@ -0,0 +1,42 @@ +# SQL + Go migrations + +## Example custom goose binary with built-in Go migrations + +```bash +$ go build -o goose *.go +``` + +``` +$ ./goose sqlite3 ./foo.db status + Applied At Migration + ======================================= + Pending -- 00001_create_users_table.sql + Pending -- 00002_rename_root.go + +$ ./goose sqlite3 ./foo.db up +OK 00001_create_users_table.sql +OK 00002_rename_root.go +goose: no migrations to run. current version: 2 + +$ + Applied At Migration + ======================================= + Mon Jun 19 21:56:00 2017 -- 00001_create_users_table.sql + Mon Jun 19 21:56:00 2017 -- 00002_rename_root.go +``` + +## Best practice: Split migrations into a standalone package + +1. Move [main.go](main.go) into your `cmd/` directory + +2. Rename migration functions to `migrations` pkg + +3. Import `migrations` package from [cmd/main.go](main.go) + + ```go + import ( + _ "github.com/pressly/goose/example/migrations-go" + ) + ``` + + This will cause all `init()` functions to be called within `migrations` pkg, thus registering the migration functions properly. diff --git a/example/migrations/README.md b/example/migrations/README.md new file mode 100644 index 0000000..245c3a2 --- /dev/null +++ b/example/migrations/README.md @@ -0,0 +1,26 @@ +# SQL migrations only + +See [second example](../migrations-go) for Go migrations. + +```bash +$ go get -u github.com/pressly/goose/cmd/goose +``` + +```bash +$ goose sqlite3 ./foo.db status + Applied At Migration + ======================================= + Pending -- 00001_create_users_table.sql + Pending -- 00002_rename_root.sql + +$ goose sqlite3 ./foo.db up +OK 00001_create_users_table.sql +OK 00002_rename_root.sql +goose: no migrations to run. current version: 2 + +$ + Applied At Migration + ======================================= + Mon Jun 19 21:56:00 2017 -- 00001_create_users_table.sql + Mon Jun 19 21:56:00 2017 -- 00002_rename_root.sql +``` From 8daf954f3ae729e7e30e5d206c9f09b18931445d Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Tue, 20 Jun 2017 12:12:51 -0400 Subject: [PATCH 4/5] Improve .travis tests --- .travis.yml | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index fd350fe..8fc94f0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,11 +10,13 @@ install: script: - go test -- go run ./cmd/goose/main.go -dir=example/migrations sqlite3 sql.db up -- go run ./cmd/goose/main.go -dir=example/migrations sqlite3 sql.db version -- go run ./cmd/goose/main.go -dir=example/migrations sqlite3 sql.db down -- go run ./cmd/goose/main.go -dir=example/migrations sqlite3 sql.db status -- go run ./example/migrations-go/cmd/main.go -dir=example/migrations-go sqlite3 go.db up -- go run ./example/migrations-go/cmd/main.go -dir=example/migrations-go sqlite3 go.db version -- go run ./example/migrations-go/cmd/main.go -dir=example/migrations-go sqlite3 go.db down -- go run ./example/migrations-go/cmd/main.go -dir=example/migrations-go sqlite3 go.db status +- go build -i -o goose ./cmd/goose +- ./goose -dir=example/migrations sqlite3 sql.db up +- ./goose -dir=example/migrations sqlite3 sql.db version +- ./goose -dir=example/migrations sqlite3 sql.db down +- ./goose -dir=example/migrations sqlite3 sql.db status +- go build -i -o custom-goose ./example/migrations-go +- ./custom-goose -dir=example/migrations-go sqlite3 go.db up +- ./custom-goose -dir=example/migrations-go sqlite3 go.db version +- ./custom-goose -dir=example/migrations-go sqlite3 go.db down +- ./custom-goose -dir=example/migrations-go sqlite3 go.db status From e66261f33e4364ce2c8e57421c639357cc1256fa Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Tue, 20 Jun 2017 12:17:28 -0400 Subject: [PATCH 5/5] Remove tip from Travis --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8fc94f0..bb9bdd7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ sudo: false language: go go: - 1.8 -- tip install: - go get github.com/golang/dep/cmd/dep