-
-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate fizz & SQL migrations generators to genny (#432)
* Migrate fizz & SQL migrations generators to genny * Add support for attributes to the fizz cmd * Add support for attributes to the sql command
- Loading branch information
1 parent
5959846
commit fa578ca
Showing
10 changed files
with
360 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cempty | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/gobuffalo/genny" | ||
) | ||
|
||
// New creates a generator to make empty migration files. | ||
func New(opts *Options) (*genny.Generator, error) { | ||
g := genny.New() | ||
|
||
if err := opts.Validate(); err != nil { | ||
return g, err | ||
} | ||
|
||
var f genny.File | ||
if opts.Type == "sql" { | ||
f = genny.NewFileS(filepath.Join(opts.Path, fmt.Sprintf("%s.%s.up.sql", opts.Name, opts.Translator.Name())), "") | ||
g.File(f) | ||
f = genny.NewFileS(filepath.Join(opts.Path, fmt.Sprintf("%s.%s.down.sql", opts.Name, opts.Translator.Name())), "") | ||
g.File(f) | ||
} else { | ||
f = genny.NewFileS(filepath.Join(opts.Path, opts.Name+".up.fizz"), "") | ||
g.File(f) | ||
f = genny.NewFileS(filepath.Join(opts.Path, opts.Name+".down.fizz"), "") | ||
g.File(f) | ||
} | ||
return g, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cempty | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gobuffalo/genny/gentest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_New(t *testing.T) { | ||
r := require.New(t) | ||
|
||
g, err := New(&Options{ | ||
TableName: "widgets", | ||
Name: "create_widgets", | ||
}) | ||
r.NoError(err) | ||
|
||
run := gentest.NewRunner() | ||
run.With(g) | ||
|
||
r.NoError(run.Run()) | ||
|
||
res := run.Results() | ||
|
||
r.Len(res.Commands, 0) | ||
r.Len(res.Files, 2) | ||
|
||
f := res.Files[0] | ||
r.Equal("migrations/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("", f.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package cempty | ||
|
||
type mockTranslator struct{} | ||
|
||
func (mockTranslator) Name() string { | ||
return "test" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cempty | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gobuffalo/flect/name" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var nowFunc = time.Now | ||
|
||
type nameable interface { | ||
Name() string | ||
} | ||
|
||
// Options for the empty migration generator. | ||
type Options struct { | ||
// TableName is the name of the table. | ||
TableName string | ||
// Name is the name of the generated file. | ||
Name string | ||
// Path is the dir path where to generate the migration files. | ||
Path string | ||
// Translator is a Fizz translator to use when asking for SQL migrations. | ||
Translator nameable | ||
// Type is the type of migration to generate (sql or fizz). | ||
Type string | ||
} | ||
|
||
// Validate that options are usuable | ||
func (opts *Options) Validate() error { | ||
if len(opts.TableName) == 0 { | ||
return errors.New("you must set a name for your table") | ||
} | ||
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()) | ||
} | ||
if len(opts.Type) == 0 { | ||
opts.Type = "fizz" | ||
} | ||
if opts.Type != "fizz" && opts.Type != "sql" { | ||
return errors.Errorf("%s migration type is not allowed", opts.Type) | ||
} | ||
if opts.Type == "sql" && opts.Translator == nil { | ||
return errors.New("sql migrations require a fizz translator") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cempty | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Options_Validate(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 }() | ||
|
||
opts := &Options{} | ||
err := opts.Validate() | ||
r.Error(err) | ||
|
||
opts.TableName = "widget" | ||
|
||
err = opts.Validate() | ||
r.NoError(err) | ||
|
||
r.Equal(opts.Name, "20190828074602_create_widgets") | ||
r.Equal("migrations", opts.Path) | ||
|
||
// Custom migration name | ||
opts.Name = "custom_migration" | ||
err = opts.Validate() | ||
r.NoError(err) | ||
|
||
r.Equal(opts.Name, "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", | ||
} | ||
err := opts.Validate() | ||
r.EqualError(err, "sql migrations require a fizz translator") | ||
|
||
opts.Translator = mockTranslator{} | ||
opts.Type = "aaa" | ||
err = opts.Validate() | ||
r.EqualError(err, "aaa migration type is not allowed") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package generate | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/gobuffalo/envy" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_FizzCmd_NoArg(t *testing.T) { | ||
r := require.New(t) | ||
c := FizzCmd | ||
c.SetArgs([]string{}) | ||
|
||
gp, err := envy.MustGet("GOPATH") | ||
r.NoError(err) | ||
cpath := filepath.Join(gp, "src", "github.com", "gobuffalo") | ||
tdir, err := ioutil.TempDir(cpath, "testapp") | ||
r.NoError(err) | ||
defer os.RemoveAll(tdir) | ||
|
||
pwd, err := os.Getwd() | ||
r.NoError(err) | ||
os.Chdir(tdir) | ||
defer os.Chdir(pwd) | ||
|
||
err = c.Execute() | ||
r.EqualError(err, "you must set a name for your table") | ||
} |
Oops, something went wrong.