Skip to content

Commit

Permalink
Add create-next command to generate sequential numbering of migrations (
Browse files Browse the repository at this point in the history
  • Loading branch information
przemyslaw-dobrowolski-cl committed Mar 29, 2017
1 parent 31bb74f commit 24ad0f7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ Options:
directory with migration files (default ".")
Commands:
up Migrate the DB to the most recent version available
down Roll back the version by 1
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
up Migrate the DB to the most recent version available
down Roll back the version by 1
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-next Creates a blank migration template with next sequential version
```
## create

Expand Down
14 changes: 12 additions & 2 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ import (
)

// 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())
func Create(db *sql.DB, dir, name, migrationType string, sequential bool) error {
var version string
if !sequential {
version = time.Now().Format("20060102150405")
} else {
last, err := LastMigration(dir)
if err != nil {
return err
}
version = fmt.Sprintf("%d", last.Version + 1)
}
path, err := CreateMigration(name, migrationType, dir, version)
if err != nil {
return err
}
Expand Down
14 changes: 13 additions & 1 deletion goose.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,19 @@ func Run(command string, db *sql.DB, dir string, args ...string) error {
if len(args) == 2 {
migrationType = args[1]
}
if err := Create(db, dir, args[0], migrationType); err != nil {
if err := Create(db, dir, args[0], migrationType, false); err != nil {
return err
}
case "create-next":
if len(args) == 0 {
return fmt.Errorf("create must be of form: goose [OPTIONS] DRIVER DBSTRING create-next NAME [go|sql]")
}

migrationType := "go"
if len(args) == 2 {
migrationType = args[1]
}
if err := Create(db, dir, args[0], migrationType, true); err != nil {
return err
}
case "down":
Expand Down
14 changes: 14 additions & 0 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ func LoadMigrationPlugins(dirpath string) error {
return nil
}

// lastVersion returns version of last migration present in dirpath
func LastMigration(dirpath string) (*Migration, error) {
ms, err := CollectMigrations(dirpath, 0, MaxVersion)
if err != nil {
return nil, err
}
m, err := ms.Last()

if err != nil {
return nil, err
}
return m, nil
}

func sortAndConnectMigrations(migrations Migrations) Migrations {
sort.Sort(migrations)

Expand Down
3 changes: 1 addition & 2 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@ func NumericComponent(name string) (int64, error) {
return n, e
}

func CreateMigration(name, migrationType, dir string, t time.Time) (path string, err error) {
func CreateMigration(name, migrationType, dir string, version string) (path string, err error) {

if migrationType != "go" && migrationType != "sql" {
return "", errors.New("migration type must be 'go' or 'sql'")
}

version := t.Format("20060102150405")
filename := fmt.Sprintf("%v_%v.%v", version, name, migrationType)

fpath := filepath.Join(dir, filename)
Expand Down

0 comments on commit 24ad0f7

Please sign in to comment.