From 3523b17cdc914ac56d49164136e7d8610244835a Mon Sep 17 00:00:00 2001 From: Josh Fyne Date: Wed, 29 Jun 2016 12:30:47 -0400 Subject: [PATCH 1/6] Remove misleading create documentation --- README.md | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/README.md b/README.md index aaaa334..ff11b50 100644 --- a/README.md +++ b/README.md @@ -43,20 +43,6 @@ Commands: dbversion Print the current version of the database ``` -## create - -Create a new Go migration. - - $ goose create AddSomeColumns - $ goose: created db/migrations/20130106093224_AddSomeColumns.go - -Edit the newly created script to define the behavior of your migration. - -You can also create an SQL migration: - - $ goose create AddSomeColumns sql - $ goose: created db/migrations/20130106093224_AddSomeColumns.sql - ## up Apply all available migrations. @@ -106,7 +92,7 @@ Print the current version of the database: # Migrations -goose supports migrations written in SQL or in Go - see the `goose create` command above for details on how to generate them. +goose supports migrations written in SQL or in Go. ## SQL Migrations From 7b43a0193d5205d02cb51b689967da789f8a85b6 Mon Sep 17 00:00:00 2001 From: Josh Fyne Date: Wed, 29 Jun 2016 14:20:34 -0400 Subject: [PATCH 2/6] Adding create command back in --- cmd/goose/main.go | 9 ++++++++- create.go | 18 ++++++++++++++++++ goose.go | 9 ++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 create.go diff --git a/cmd/goose/main.go b/cmd/goose/main.go index f4b40ba..78ac8ac 100644 --- a/cmd/goose/main.go +++ b/cmd/goose/main.go @@ -57,7 +57,12 @@ func main() { log.Fatalf("-dbstring=%q: %v\n", dbstring, err) } - if err := goose.Run(command, db, *dir); err != nil { + arguments := []string{*dir} + if len(args) > 3 { + arguments = append(arguments, args[3:]...) + } + + if err := goose.Run(command, db, arguments...); err != nil { log.Fatalf("goose run: %v", err) } } @@ -75,6 +80,7 @@ Examples: goose postgres "user=postgres dbname=postgres sslmode=disable" up goose mysql "user:password@/dbname" down goose sqlite3 ./foo.db status + goose postgres "user=postgres dbname=postgres sslmode=disable" create init sql Options: ` @@ -86,5 +92,6 @@ Commands: redo Re-run the latest migration status Dump the migration status for the current DB dbversion Print the current version of the database + create Creates a blank migration template ` ) diff --git a/create.go b/create.go new file mode 100644 index 0000000..3a8b9aa --- /dev/null +++ b/create.go @@ -0,0 +1,18 @@ +package goose + +import ( + "database/sql" + "fmt" + "time" +) + +// Create writes a new blank migration file. +func Create(db *sql.DB, name, migrationType, dir string) error { + path, err := CreateMigration(name, migrationType, dir, time.Now()) + if err != nil { + return err + } + fmt.Println(fmt.Sprintf("Created %s migration at %s", migrationType, path)) + + return nil +} diff --git a/goose.go b/goose.go index 1f2a75b..67b9934 100644 --- a/goose.go +++ b/goose.go @@ -5,7 +5,10 @@ import ( "fmt" ) -func Run(command string, db *sql.DB, dir string) error { +func Run(command string, db *sql.DB, args ...string) error { + dir := args[0] + additional := args[0:] + switch command { case "up": if err := Up(db, dir); err != nil { @@ -15,6 +18,10 @@ func Run(command string, db *sql.DB, dir string) error { if err := UpByOne(db, dir); err != nil { return err } + case "create": + if err := Create(db, additional[0], additional[1], dir); err != nil { + return err + } case "down": if err := Down(db, dir); err != nil { return err From ba86be6db6b3720dec2e8e102e225e4837f68cac Mon Sep 17 00:00:00 2001 From: Josh Fyne Date: Wed, 29 Jun 2016 14:29:16 -0400 Subject: [PATCH 3/6] Readme fix --- README.md | 14 ++++++++++++++ cmd/goose/main.go | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ff11b50..ed1c082 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,21 @@ Commands: redo Re-run the latest migration status Dump the migration status for the current DB dbversion Print the current version of the database + create Creates a blank migration template ``` +## create + +Create a new Go migration. + + $ goose create AddSomeColumns + $ goose: created db/migrations/20130106093224_AddSomeColumns.go + +Edit the newly created script to define the behavior of your migration. + +You can also create an SQL migration: + + $ goose create AddSomeColumns sql + $ goose: created db/migrations/20130106093224_AddSomeColumns.sql ## up diff --git a/cmd/goose/main.go b/cmd/goose/main.go index 78ac8ac..ac186c7 100644 --- a/cmd/goose/main.go +++ b/cmd/goose/main.go @@ -92,6 +92,6 @@ Commands: redo Re-run the latest migration status Dump the migration status for the current DB dbversion Print the current version of the database - create Creates a blank migration template + create Creates a blank migration template ` ) From db38a98723c3b5a91086590c4185c9a222b5cb6e Mon Sep 17 00:00:00 2001 From: Josh Fyne Date: Wed, 29 Jun 2016 14:54:13 -0400 Subject: [PATCH 4/6] dir string before extra args --- cmd/goose/main.go | 4 ++-- goose.go | 7 ++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/cmd/goose/main.go b/cmd/goose/main.go index ac186c7..1560552 100644 --- a/cmd/goose/main.go +++ b/cmd/goose/main.go @@ -57,12 +57,12 @@ func main() { log.Fatalf("-dbstring=%q: %v\n", dbstring, err) } - arguments := []string{*dir} + arguments := []string{} if len(args) > 3 { arguments = append(arguments, args[3:]...) } - if err := goose.Run(command, db, arguments...); err != nil { + if err := goose.Run(command, db, *dir, arguments...); err != nil { log.Fatalf("goose run: %v", err) } } diff --git a/goose.go b/goose.go index 67b9934..6fac520 100644 --- a/goose.go +++ b/goose.go @@ -5,10 +5,7 @@ import ( "fmt" ) -func Run(command string, db *sql.DB, args ...string) error { - dir := args[0] - additional := args[0:] - +func Run(command string, db *sql.DB, dir string, args ...string) error { switch command { case "up": if err := Up(db, dir); err != nil { @@ -19,7 +16,7 @@ func Run(command string, db *sql.DB, args ...string) error { return err } case "create": - if err := Create(db, additional[0], additional[1], dir); err != nil { + if err := Create(db, args[0], args[1], dir); err != nil { return err } case "down": From e4c76971238d65abceed67cd2478e99d79dfd165 Mon Sep 17 00:00:00 2001 From: Josh Fyne Date: Wed, 29 Jun 2016 15:40:13 -0400 Subject: [PATCH 5/6] Re-ordered create arguments, checked for missing create arguments --- create.go | 2 +- goose.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/create.go b/create.go index 3a8b9aa..3ce998f 100644 --- a/create.go +++ b/create.go @@ -7,7 +7,7 @@ import ( ) // Create writes a new blank migration file. -func Create(db *sql.DB, name, migrationType, dir string) error { +func Create(db *sql.DB, dir, name, migrationType string) error { path, err := CreateMigration(name, migrationType, dir, time.Now()) if err != nil { return err diff --git a/goose.go b/goose.go index 6fac520..0240bb6 100644 --- a/goose.go +++ b/goose.go @@ -16,7 +16,15 @@ func Run(command string, db *sql.DB, dir string, args ...string) error { return err } case "create": - if err := Create(db, args[0], args[1], dir); err != nil { + if len(args) == 0 { + return fmt.Errorf("create must be of form: goose [OPTIONS] DRIVER DBSTRING create NAME [go|sql]") + } + + migrationType := "go" + if len(args) == 2 { + migrationType = args[1] + } + if err := Create(db, dir, args[0], migrationType); err != nil { return err } case "down": From 15b7bf5fc7712d90d97d98ab5767aa25ea1913b7 Mon Sep 17 00:00:00 2001 From: Josh Fyne Date: Wed, 29 Jun 2016 17:56:28 -0400 Subject: [PATCH 6/6] Actually works now, re-added go migration template, updated cmd parser --- cmd/goose/main.go | 2 +- migrate.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/cmd/goose/main.go b/cmd/goose/main.go index 1560552..73c121a 100644 --- a/cmd/goose/main.go +++ b/cmd/goose/main.go @@ -25,7 +25,7 @@ func main() { flags.Parse(os.Args[1:]) args := flags.Args() - if len(args) != 3 { + if len(args) < 3 { flags.Usage() return } diff --git a/migrate.go b/migrate.go index c0bb42a..a214a0e 100644 --- a/migrate.go +++ b/migrate.go @@ -393,6 +393,9 @@ func CreateMigration(name, migrationType, dir string, t time.Time) (path string, fpath := filepath.Join(dir, filename) tmpl := sqlMigrationTemplate + if migrationType == "go" { + tmpl = goSqlMigrationTemplate + } path, err = writeTemplateToFile(fpath, tmpl, timestamp) @@ -422,3 +425,24 @@ var sqlMigrationTemplate = template.Must(template.New("goose.sql-migration").Par -- SQL section 'Down' is executed when this migration is rolled back `)) +var goSqlMigrationTemplate = template.Must(template.New("goose.go-migration").Parse(` +package migration + +import ( + "database/sql" + + "github.com/pressly/goose" +) + +func init() { + goose.AddMigration(Up_{{.}}, Down_{{.}}) +} + +func Up_{{.}}(tx *sql.Tx) error { + return nil +} + +func Down_{{.}}(tx *sql.Tx) error { + return nil +} +`))