mirror of https://github.com/pressly/goose.git
commit
4dec8a31fd
|
@ -41,8 +41,8 @@ Commands:
|
||||||
redo Re-run the latest migration
|
redo Re-run the latest migration
|
||||||
status Dump the migration status for the current DB
|
status Dump the migration status for the current DB
|
||||||
dbversion Print the current version of the database
|
dbversion Print the current version of the database
|
||||||
|
create Creates a blank migration template
|
||||||
```
|
```
|
||||||
|
|
||||||
## create
|
## create
|
||||||
|
|
||||||
Create a new Go migration.
|
Create a new Go migration.
|
||||||
|
@ -106,7 +106,7 @@ Print the current version of the database:
|
||||||
|
|
||||||
# Migrations
|
# 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
|
## SQL Migrations
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ func main() {
|
||||||
flags.Parse(os.Args[1:])
|
flags.Parse(os.Args[1:])
|
||||||
|
|
||||||
args := flags.Args()
|
args := flags.Args()
|
||||||
if len(args) != 3 {
|
if len(args) < 3 {
|
||||||
flags.Usage()
|
flags.Usage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,12 @@ func main() {
|
||||||
log.Fatalf("-dbstring=%q: %v\n", dbstring, err)
|
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)
|
log.Fatalf("goose run: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,6 +80,7 @@ Examples:
|
||||||
goose postgres "user=postgres dbname=postgres sslmode=disable" up
|
goose postgres "user=postgres dbname=postgres sslmode=disable" up
|
||||||
goose mysql "user:password@/dbname" down
|
goose mysql "user:password@/dbname" down
|
||||||
goose sqlite3 ./foo.db status
|
goose sqlite3 ./foo.db status
|
||||||
|
goose postgres "user=postgres dbname=postgres sslmode=disable" create init sql
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
`
|
`
|
||||||
|
@ -86,5 +92,6 @@ Commands:
|
||||||
redo Re-run the latest migration
|
redo Re-run the latest migration
|
||||||
status Dump the migration status for the current DB
|
status Dump the migration status for the current DB
|
||||||
dbversion Print the current version of the database
|
dbversion Print the current version of the database
|
||||||
|
create Creates a blank migration template
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package goose
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Create writes a new blank migration file.
|
||||||
|
func Create(db *sql.DB, dir, name, migrationType 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
|
||||||
|
}
|
14
goose.go
14
goose.go
|
@ -5,7 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run(command string, db *sql.DB, dir string) error {
|
func Run(command string, db *sql.DB, dir string, args ...string) error {
|
||||||
switch command {
|
switch command {
|
||||||
case "up":
|
case "up":
|
||||||
if err := Up(db, dir); err != nil {
|
if err := Up(db, dir); err != nil {
|
||||||
|
@ -15,6 +15,18 @@ func Run(command string, db *sql.DB, dir string) error {
|
||||||
if err := UpByOne(db, dir); err != nil {
|
if err := UpByOne(db, dir); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
case "create":
|
||||||
|
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":
|
case "down":
|
||||||
if err := Down(db, dir); err != nil {
|
if err := Down(db, dir); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
24
migrate.go
24
migrate.go
|
@ -393,6 +393,9 @@ func CreateMigration(name, migrationType, dir string, t time.Time) (path string,
|
||||||
|
|
||||||
fpath := filepath.Join(dir, filename)
|
fpath := filepath.Join(dir, filename)
|
||||||
tmpl := sqlMigrationTemplate
|
tmpl := sqlMigrationTemplate
|
||||||
|
if migrationType == "go" {
|
||||||
|
tmpl = goSqlMigrationTemplate
|
||||||
|
}
|
||||||
|
|
||||||
path, err = writeTemplateToFile(fpath, tmpl, timestamp)
|
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
|
-- 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
|
||||||
|
}
|
||||||
|
`))
|
||||||
|
|
Loading…
Reference in New Issue