Skip to content

Commit

Permalink
sweet new model/migration generator
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Aug 18, 2016
1 parent 8619e45 commit 8dd57ef
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 18 deletions.
14 changes: 7 additions & 7 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var schemaMigrations = fizz.Table{
},
}

func MigrationCreate(path, name, ext string) error {
func MigrationCreate(path, name, ext string, up, down []byte) error {
n := time.Now().UTC()
s := n.Format("20060102150405")

Expand All @@ -34,20 +34,20 @@ func MigrationCreate(path, name, ext string) error {
return err
}

up := filepath.Join(path, (fmt.Sprintf("%s_%s.up.%s", s, name, ext)))
err = ioutil.WriteFile(up, []byte(""), 0666)
upf := filepath.Join(path, (fmt.Sprintf("%s_%s.up.%s", s, name, ext)))
err = ioutil.WriteFile(upf, up, 0666)
if err != nil {
return err
}
fmt.Printf("> %s\n", up)
fmt.Printf("> %s\n", upf)

down := filepath.Join(path, (fmt.Sprintf("%s_%s.down.%s", s, name, ext)))
err = ioutil.WriteFile(down, []byte(""), 0666)
downf := filepath.Join(path, (fmt.Sprintf("%s_%s.down.%s", s, name, ext)))
err = ioutil.WriteFile(downf, down, 0666)
if err != nil {
return err
}

fmt.Printf("> %s\n", down)
fmt.Printf("> %s\n", downf)
return err
}

Expand Down
9 changes: 5 additions & 4 deletions soda/cmd/generate/fizz_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
)

var FizzCmd = &cobra.Command{
Use: "fizz [name]",
Short: "Generates Up/Down migrations for your database using fizz.",
Use: "fizz [name]",
Aliases: []string{"migration"},
Short: "Generates Up/Down migrations for your database using fizz.",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("You must supply a name for your migration!")
}
cflag := cmd.Flag("path")
migrationPath := defaults.String(cflag.Value.String(), "./migrates")
return pop.MigrationCreate(migrationPath, args[0], "fizz")
migrationPath := defaults.String(cflag.Value.String(), "./migrations")
return pop.MigrationCreate(migrationPath, args[0], "fizz", nil, nil)
},
}
61 changes: 57 additions & 4 deletions soda/cmd/generate/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ import (
"regexp"
"strings"

"github.com/markbates/going/defaults"
"github.com/markbates/inflect"
"github.com/markbates/pop"
"github.com/spf13/cobra"
)

var skipMigration bool

func init() {
ModelCmd.Flags().BoolVarP(&skipMigration, "skip-migration", "s", false, "Skip creating a new fizz migration for this model.")
}

var nrx = regexp.MustCompile(`^nulls.(.+)`)

type names struct {
Original string
Table string
Expand All @@ -36,6 +46,7 @@ type attribute struct {
Names names
OriginalType string
GoType string
Nullable bool
}

func (a attribute) String() string {
Expand Down Expand Up @@ -72,6 +83,23 @@ func (m model) String() string {
return strings.Join(s, "\n")
}

func (m model) Fizz() string {
s := []string{fmt.Sprintf("create_table(\"%s\", func(t) {", m.Names.Table)}
for _, a := range m.Attributes {
switch a.Names.Original {
case "id", "created_at", "updated_at":
default:
x := fmt.Sprintf("\tt.Column(\"%s\", \"%s\", {})", a.Names.Original, fizzColType(a.OriginalType))
if a.Nullable {
x = strings.Replace(x, "{}", `{"null": true}`, -1)
}
s = append(s, x)
}
}
s = append(s, "})")
return strings.Join(s, "\n")
}

func newModel(name string) model {
id := newName("id")
id.Proper = "ID"
Expand All @@ -98,21 +126,22 @@ var ModelCmd = &cobra.Command{

model := newModel(args[0])

nrx := regexp.MustCompile(`^nulls.(.+)`)
hasNulls := false
for _, def := range args[1:] {
col := strings.Split(def, ":")
if len(col) == 1 {
col = append(col, "string")
}
if !hasNulls && nrx.MatchString(col[1]) {
nullable := nrx.MatchString(col[1])
if !hasNulls && nullable {
hasNulls = true
model.Imports = append(model.Imports, "github.com/markbates/going/nulls")
}
model.Attributes = append(model.Attributes, attribute{
Names: newName(col[0]),
OriginalType: col[1],
GoType: colType(col[1]),
Nullable: nullable,
})
}

Expand All @@ -122,12 +151,12 @@ var ModelCmd = &cobra.Command{
}

fname := filepath.Join("models", model.Names.File+".go")
err = ioutil.WriteFile(fname, []byte(model.String()), 0766)
err = ioutil.WriteFile(fname, []byte(model.String()), 0666)
if err != nil {
return err
}

err = ioutil.WriteFile(filepath.Join("models", model.Names.File+"_test.go"), []byte(`package models_test`), 0766)
err = ioutil.WriteFile(filepath.Join("models", model.Names.File+"_test.go"), []byte(`package models_test`), 0666)
if err != nil {
return err
}
Expand All @@ -147,6 +176,15 @@ var ModelCmd = &cobra.Command{

fmt.Println(string(b))

if !skipMigration {
cflag := cmd.Flag("path")
migrationPath := defaults.String(cflag.Value.String(), "./migrations")
err = pop.MigrationCreate(migrationPath, fmt.Sprintf("create_%s", model.Names.Table), "fizz", []byte(model.Fizz()), []byte(fmt.Sprintf("drop_table(\"%s\")", model.Names.Table)))
if err != nil {
return err
}
}

return nil
},
}
Expand All @@ -162,3 +200,18 @@ func colType(s string) string {
}
return s
}

func fizzColType(s string) string {
if nrx.MatchString(s) {
return fizzColType(strings.Replace(s, "nulls.", "", -1))
}
switch strings.ToLower(s) {
case "int":
return "integer"
case "time":
return "timestamp"
default:
return strings.ToLower(s)
}
return s
}
2 changes: 1 addition & 1 deletion soda/cmd/generate/sql_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ var SQLCmd = &cobra.Command{
}
cflag := cmd.Flag("path")
migrationPath := defaults.String(cflag.Value.String(), "./migrates")
return pop.MigrationCreate(migrationPath, args[0], "sql")
return pop.MigrationCreate(migrationPath, args[0], "sql", nil, nil)
},
}
2 changes: 1 addition & 1 deletion soda/cmd/migrate_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var migrateCreateCmd = &cobra.Command{
return errors.New("You must supply a name for your migration!")
}

return pop.MigrationCreate(migrationPath, args[0], migrationType)
return pop.MigrationCreate(migrationPath, args[0], migrationType, nil, nil)
},
}

Expand Down
2 changes: 1 addition & 1 deletion soda/cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package cmd

const Version = "3.3.0.rc1"
const Version = "3.3.0"

0 comments on commit 8dd57ef

Please sign in to comment.