Skip to content

Commit

Permalink
UPDATE: creating ddl migration spec is now possible
Browse files Browse the repository at this point in the history
SUMMARY: The DDL migration can now be generated by adding a `-ddl` and a
proper "alter" or "create" as argument value.

Migration generation had been modified to accommodate the complementary
code for ddl generation

Signed-off-by: Gnanakeethan Balasubramaniam <[email protected]>
  • Loading branch information
gnanakeethan committed Jul 13, 2017
1 parent 34faa8f commit ea63bf2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 32 deletions.
3 changes: 2 additions & 1 deletion cmd/commands/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/beego/bee/config"
"github.com/beego/bee/generate"
"github.com/beego/bee/generate/swaggergen"
beeLogger "github.com/beego/bee/logger"
"github.com/beego/bee/logger"
"github.com/beego/bee/utils"
)

Expand Down Expand Up @@ -71,6 +71,7 @@ func init() {
CmdGenerate.Flag.Var(&generate.SQLConn, "conn", "Connection string used by the SQLDriver to connect to a database instance.")
CmdGenerate.Flag.Var(&generate.Level, "level", "Either 1, 2 or 3. i.e. 1=models; 2=models and controllers; 3=models, controllers and routers.")
CmdGenerate.Flag.Var(&generate.Fields, "fields", "List of table Fields.")
CmdGenerate.Flag.Var(&generate.DDL, "ddl", "Generate DDL Migration")
commands.AvailableCommands = append(commands.AvailableCommands, CmdGenerate)
}

Expand Down
1 change: 1 addition & 0 deletions generate/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ var SQLConn utils.DocValue
var Level utils.DocValue
var Tables utils.DocValue
var Fields utils.DocValue
var DDL utils.DocValue
105 changes: 74 additions & 31 deletions generate/g_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"strings"
"time"

beeLogger "github.com/beego/bee/logger"
"github.com/beego/bee/logger"
"github.com/beego/bee/logger/colors"
"github.com/beego/bee/utils"
)
Expand Down Expand Up @@ -203,11 +203,31 @@ func GenerateMigration(mname, upsql, downsql, curpath string) {
fpath := path.Join(migrationFilePath, fmt.Sprintf("%s_%s.go", today, mname))
if f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil {
defer utils.CloseFile(f)
content := strings.Replace(MigrationTPL, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1)
content = strings.Replace(content, "{{CurrTime}}", today, -1)
content = strings.Replace(content, "{{UpSQL}}", upsql, -1)
content = strings.Replace(content, "{{DownSQL}}", downsql, -1)
f.WriteString(content)
ddlSpec := ""
spec := ""
up := ""
down := ""
if DDL != "" {
ddlSpec = "m.ddlSpec()"
switch strings.Title(DDL.String()) {
case "Create":
spec = strings.Replace(DDLSpecCreate, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1)
break
case "Alter":
spec = strings.Replace(DDLSpecAlter, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1)
}
spec = strings.Replace(spec, "{{tableName}}", mname, -1)
} else {
up = strings.Replace(MigrationUp, "{{UpSQL}}", upsql, -1)
up = strings.Replace(up, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1)
down = strings.Replace(MigrationDown, "{{DownSQL}}", downsql, -1)
down = strings.Replace(down, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1)
}

header := strings.Replace(MigrationHeader, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1)
header = strings.Replace(header, "{{ddlSpec}}", ddlSpec, -1)
header = strings.Replace(header, "{{CurrTime}}", today, -1)
f.WriteString(header + spec + up + down)
// Run 'gofmt' on the generated source code
utils.FormatSourceCode(fpath)
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", fpath, "\x1b[0m")
Expand All @@ -216,33 +236,56 @@ func GenerateMigration(mname, upsql, downsql, curpath string) {
}
}

const MigrationTPL = `package main
const (
MigrationHeader = `package main
import (
"github.com/astaxie/beego/migration"
)
import (
"github.com/astaxie/beego/migration"
)
// DO NOT MODIFY
type {{StructName}} struct {
migration.Migration
}
// DO NOT MODIFY
type {{StructName}} struct {
migration.Migration
}
// DO NOT MODIFY
func init() {
m := &{{StructName}}{}
m.Created = "{{CurrTime}}"
{{ddlSpec}}
migration.Register("{{StructName}}", m)
}
`

// DO NOT MODIFY
func init() {
m := &{{StructName}}{}
m.Created = "{{CurrTime}}"
migration.Register("{{StructName}}", m)
}
DDLSpecCreate = `
/*
refer beego/migration/doc.go
*/
func(m *{{StructName}}) ddlSpec(){
m.CreateTable("{{tableName}}", "InnoDB", "utf8")
m.PriCol("id").SetAuto(true).SetNullable(false).SetDataType("INT(10)").SetUnsigned(true)
// Run the migrations
func (m *{{StructName}}) Up() {
// use m.SQL("CREATE TABLE ...") to make schema update
{{UpSQL}}
}
}
`
DDLSpecAlter = `
/*
refer beego/migration/doc.go
*/
func(m *{{StructName}}) ddlSpec(){
m.AlterTable("{{tableName}}")
// Reverse the migrations
func (m *{{StructName}}) Down() {
// use m.SQL("DROP TABLE ...") to reverse schema update
{{DownSQL}}
}
`
}
`
MigrationUp = `
// Run the migrations
func (m *{{StructName}}) Up() {
// use m.SQL("CREATE TABLE ...") to make schema update
{{UpSQL}}
}`
MigrationDown = `
// Reverse the migrations
func (m *{{StructName}}) Down() {
// use m.SQL("DROP TABLE ...") to reverse schema update
{{DownSQL}}
}
`
)

0 comments on commit ea63bf2

Please sign in to comment.