Skip to content

Commit

Permalink
Fix create empty migration naming (#508)
Browse files Browse the repository at this point in the history
Fixes #484.
  • Loading branch information
stanislas-m authored Dec 16, 2019
1 parent 5798e98 commit 32d161a
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 18 deletions.
10 changes: 7 additions & 3 deletions genny/fizz/cempty/create_empty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cempty

import (
"testing"
"time"

"github.com/gobuffalo/genny/gentest"
"github.com/stretchr/testify/require"
Expand All @@ -10,8 +11,11 @@ import (
func Test_New(t *testing.T) {
r := require.New(t)

t0, _ := time.Parse(time.RFC3339, "2019-08-28T07:46:02Z")
nowFunc = func() time.Time { return t0 }
defer func() { nowFunc = time.Now }()

g, err := New(&Options{
TableName: "widgets",
Name: "create_widgets",
})
r.NoError(err)
Expand All @@ -27,10 +31,10 @@ func Test_New(t *testing.T) {
r.Len(res.Files, 2)

f := res.Files[0]
r.Equal("migrations/create_widgets.down.fizz", f.Name())
r.Equal("migrations/20190828074602_create_widgets.down.fizz", f.Name())
r.Equal("", f.String())

f = res.Files[1]
r.Equal("migrations/create_widgets.up.fizz", f.Name())
r.Equal("migrations/20190828074602_create_widgets.up.fizz", f.Name())
r.Equal("", f.String())
}
14 changes: 7 additions & 7 deletions genny/fizz/cempty/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type nameable interface {
// Options for the empty migration generator.
type Options struct {
// TableName is the name of the table.
// Deprecated: use Name directly since TableName doesn't make sense in this generator.
TableName string
// Name is the name of the generated file.
Name string
Expand All @@ -28,18 +29,17 @@ type Options struct {
Type string
}

// Validate that options are usuable
// Validate that options are usable
func (opts *Options) Validate() error {
if len(opts.TableName) == 0 {
return errors.New("you must set a name for your table")
if len(opts.Name) == 0 {
return errors.New("you must set a name for your migration")
}
if len(opts.Path) == 0 {
opts.Path = "migrations"
}
if len(opts.Name) == 0 {
timestamp := nowFunc().UTC().Format("20060102150405")
opts.Name = fmt.Sprintf("%s_create_%s", timestamp, name.New(opts.TableName).Tableize())
}
timestamp := nowFunc().UTC().Format("20060102150405")
opts.Name = fmt.Sprintf("%s_%s", timestamp, name.New(opts.Name).Underscore())

if len(opts.Type) == 0 {
opts.Type = "fizz"
}
Expand Down
8 changes: 4 additions & 4 deletions genny/fizz/cempty/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Test_Options_Validate(t *testing.T) {
err := opts.Validate()
r.Error(err)

opts.TableName = "widget"
opts.Name = "create_widgets"

err = opts.Validate()
r.NoError(err)
Expand All @@ -31,16 +31,16 @@ func Test_Options_Validate(t *testing.T) {
err = opts.Validate()
r.NoError(err)

r.Equal(opts.Name, "custom_migration")
r.Equal(opts.Name, "20190828074602_custom_migration")
r.Equal("migrations", opts.Path)
}

func Test_Options_Validate_Errors(t *testing.T) {
r := require.New(t)

opts := &Options{
TableName: "widget",
Type: "sql",
Name: "create_widget",
Type: "sql",
}
err := opts.Validate()
r.EqualError(err, "sql migrations require a fizz translator")
Expand Down
2 changes: 1 addition & 1 deletion genny/fizz/ctable/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Options struct {
ForceDefaultID bool `json:"force_default_id"`
}

// Validate that options are usuable
// Validate that options are usable
func (opts *Options) Validate() error {
if len(opts.TableName) == 0 {
return errors.New("you must set a name for your table")
Expand Down
2 changes: 1 addition & 1 deletion genny/model/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Options struct {
ForceDefaultTimestamps bool `json:"force_default_timestamps"`
}

// Validate that options are usuable
// Validate that options are usable
func (opts *Options) Validate() error {
if len(opts.Name) == 0 {
return errors.New("you must set a name for your model")
Expand Down
2 changes: 1 addition & 1 deletion soda/cmd/generate/fizz_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ func Test_FizzCmd_NoArg(t *testing.T) {
defer os.Chdir(pwd)

err = c.Execute()
r.EqualError(err, "you must set a name for your table")
r.EqualError(err, "you must set a name for your migration")
}
2 changes: 1 addition & 1 deletion soda/cmd/generate/sql_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var SQLCmd = &cobra.Command{

if len(atts) == 0 {
g, err := cempty.New(&cempty.Options{
TableName: name,
Name: name,
Path: path,
Type: "sql",
Translator: translator,
Expand Down

0 comments on commit 32d161a

Please sign in to comment.