mirror of
https://github.com/beego/bee.git
synced 2024-11-22 15:10:54 +00:00
refactor generate migration
This commit is contained in:
parent
f8447477d9
commit
3627eb2ddc
8
g.go
8
g.go
@ -170,11 +170,9 @@ func generateCode(cmd *Command, args []string) int {
|
|||||||
upsql := ""
|
upsql := ""
|
||||||
downsql := ""
|
downsql := ""
|
||||||
if fields != "" {
|
if fields != "" {
|
||||||
upsql = `m.SQL("CREATE TABLE ` + mname + "(" + generateSQLFromFields(fields.String()) + `)");`
|
dbMigrator := newDBDriver()
|
||||||
downsql = `m.SQL("DROP TABLE ` + "`" + mname + "`" + `")`
|
upsql = dbMigrator.generateCreateUp(mname)
|
||||||
if driver == "postgres" {
|
downsql = dbMigrator.generateCreateDown(mname)
|
||||||
downsql = strings.Replace(downsql, "`", "", -1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
generateMigration(mname, upsql, downsql, currpath)
|
generateMigration(mname, upsql, downsql, currpath)
|
||||||
case "controller":
|
case "controller":
|
||||||
|
157
g_migration.go
157
g_migration.go
@ -25,15 +25,168 @@ import (
|
|||||||
const (
|
const (
|
||||||
MPath = "migrations"
|
MPath = "migrations"
|
||||||
MDateFormat = "20060102_150405"
|
MDateFormat = "20060102_150405"
|
||||||
|
DBPath = "database"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type DBDriver interface {
|
||||||
|
generateCreateUp(tableName string) string
|
||||||
|
generateCreateDown(tableName string) string
|
||||||
|
}
|
||||||
|
|
||||||
|
type mysqlDriver struct{}
|
||||||
|
|
||||||
|
func (m mysqlDriver) generateCreateUp(tableName string) string {
|
||||||
|
upsql := `m.SQL("CREATE TABLE ` + tableName + "(" + m.generateSQLFromFields(fields.String()) + `)");`
|
||||||
|
return upsql
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m mysqlDriver) generateCreateDown(tableName string) string {
|
||||||
|
downsql := `m.SQL("DROP TABLE ` + "`" + tableName + "`" + `")`
|
||||||
|
return downsql
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m mysqlDriver) generateSQLFromFields(fields string) string {
|
||||||
|
sql, tags := "", ""
|
||||||
|
fds := strings.Split(fields, ",")
|
||||||
|
for i, v := range fds {
|
||||||
|
kv := strings.SplitN(v, ":", 2)
|
||||||
|
if len(kv) != 2 {
|
||||||
|
ColorLog("[ERRO] Fields format is wrong. Should be: key:type,key:type " + v + "\n")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
typ, tag := m.getSQLType(kv[1])
|
||||||
|
if typ == "" {
|
||||||
|
ColorLog("[ERRO] Fields format is wrong. Should be: key:type,key:type " + v + "\n")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if i == 0 && strings.ToLower(kv[0]) != "id" {
|
||||||
|
sql += "`id` int(11) NOT NULL AUTO_INCREMENT,"
|
||||||
|
tags = tags + "PRIMARY KEY (`id`),"
|
||||||
|
}
|
||||||
|
sql += "`" + snakeString(kv[0]) + "` " + typ + ","
|
||||||
|
if tag != "" {
|
||||||
|
tags = tags + fmt.Sprintf(tag, "`"+snakeString(kv[0])+"`") + ","
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sql = strings.TrimRight(sql+tags, ",")
|
||||||
|
return sql
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m mysqlDriver) getSQLType(ktype string) (tp, tag string) {
|
||||||
|
kv := strings.SplitN(ktype, ":", 2)
|
||||||
|
switch kv[0] {
|
||||||
|
case "string":
|
||||||
|
if len(kv) == 2 {
|
||||||
|
return "varchar(" + kv[1] + ") NOT NULL", ""
|
||||||
|
}
|
||||||
|
return "varchar(128) NOT NULL", ""
|
||||||
|
case "text":
|
||||||
|
return "longtext NOT NULL", ""
|
||||||
|
case "auto":
|
||||||
|
return "int(11) NOT NULL AUTO_INCREMENT", ""
|
||||||
|
case "pk":
|
||||||
|
return "int(11) NOT NULL", "PRIMARY KEY (%s)"
|
||||||
|
case "datetime":
|
||||||
|
return "datetime NOT NULL", ""
|
||||||
|
case "int", "int8", "int16", "int32", "int64":
|
||||||
|
fallthrough
|
||||||
|
case "uint", "uint8", "uint16", "uint32", "uint64":
|
||||||
|
return "int(11) DEFAULT NULL", ""
|
||||||
|
case "bool":
|
||||||
|
return "tinyint(1) NOT NULL", ""
|
||||||
|
case "float32", "float64":
|
||||||
|
return "float NOT NULL", ""
|
||||||
|
case "float":
|
||||||
|
return "float NOT NULL", ""
|
||||||
|
}
|
||||||
|
return "", ""
|
||||||
|
}
|
||||||
|
|
||||||
|
type postgresqlDriver struct{}
|
||||||
|
|
||||||
|
func (m postgresqlDriver) generateCreateUp(tableName string) string {
|
||||||
|
upsql := `m.SQL("CREATE TABLE ` + tableName + "(" + m.generateSQLFromFields(fields.String()) + `)");`
|
||||||
|
return upsql
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m postgresqlDriver) generateCreateDown(tableName string) string {
|
||||||
|
downsql := `m.SQL("DROP TABLE ` + tableName + `")`
|
||||||
|
return downsql
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m postgresqlDriver) generateSQLFromFields(fields string) string {
|
||||||
|
sql, tags := "", ""
|
||||||
|
fds := strings.Split(fields, ",")
|
||||||
|
for i, v := range fds {
|
||||||
|
kv := strings.SplitN(v, ":", 2)
|
||||||
|
if len(kv) != 2 {
|
||||||
|
ColorLog("[ERRO] Fields format is wrong. Should be: key:type,key:type " + v + "\n")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
typ, tag := m.getSQLType(kv[1])
|
||||||
|
if typ == "" {
|
||||||
|
ColorLog("[ERRO] Fields format is wrong. Should be: key:type,key:type " + v + "\n")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if i == 0 && strings.ToLower(kv[0]) != "id" {
|
||||||
|
sql += "id interger serial primary key,"
|
||||||
|
}
|
||||||
|
sql += snakeString(kv[0]) + " " + typ + ","
|
||||||
|
if tag != "" {
|
||||||
|
tags = tags + fmt.Sprintf(tag, snakeString(kv[0])) + ","
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if tags != "" {
|
||||||
|
sql = strings.TrimRight(sql+" "+tags, ",")
|
||||||
|
} else {
|
||||||
|
sql = strings.TrimRight(sql, ",")
|
||||||
|
}
|
||||||
|
return sql
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m postgresqlDriver) getSQLType(ktype string) (tp, tag string) {
|
||||||
|
kv := strings.SplitN(ktype, ":", 2)
|
||||||
|
switch kv[0] {
|
||||||
|
case "string":
|
||||||
|
if len(kv) == 2 {
|
||||||
|
return "char(" + kv[1] + ") NOT NULL", ""
|
||||||
|
}
|
||||||
|
return "TEXT NOT NULL", ""
|
||||||
|
case "text":
|
||||||
|
return "TEXT NOT NULL", ""
|
||||||
|
case "auto", "pk":
|
||||||
|
return "serial primary key", ""
|
||||||
|
case "datetime":
|
||||||
|
return "TIMESTAMP WITHOUT TIME ZONE NOT NULL", ""
|
||||||
|
case "int", "int8", "int16", "int32", "int64":
|
||||||
|
fallthrough
|
||||||
|
case "uint", "uint8", "uint16", "uint32", "uint64":
|
||||||
|
return "integer DEFAULT NULL", ""
|
||||||
|
case "bool":
|
||||||
|
return "boolean NOT NULL", ""
|
||||||
|
case "float32", "float64", "float":
|
||||||
|
return "numeric NOT NULL", ""
|
||||||
|
}
|
||||||
|
return "", ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func newDBDriver() DBDriver {
|
||||||
|
switch driver {
|
||||||
|
case "mysql":
|
||||||
|
return mysqlDriver{}
|
||||||
|
case "postgres":
|
||||||
|
return postgresqlDriver{}
|
||||||
|
default:
|
||||||
|
panic("driver not supported")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// generateMigration generates migration file template for database schema update.
|
// generateMigration generates migration file template for database schema update.
|
||||||
// The generated file template consists of an up() method for updating schema and
|
// The generated file template consists of an up() method for updating schema and
|
||||||
// a down() method for reverting the update.
|
// a down() method for reverting the update.
|
||||||
func generateMigration(mname, upsql, downsql, curpath string) {
|
func generateMigration(mname, upsql, downsql, curpath string) {
|
||||||
w := NewColorWriter(os.Stdout)
|
w := NewColorWriter(os.Stdout)
|
||||||
|
migrationFilePath := path.Join(curpath, DBPath, MPath)
|
||||||
migrationFilePath := path.Join(curpath, "database", MPath)
|
|
||||||
if _, err := os.Stat(migrationFilePath); os.IsNotExist(err) {
|
if _, err := os.Stat(migrationFilePath); os.IsNotExist(err) {
|
||||||
// create migrations directory
|
// create migrations directory
|
||||||
if err := os.MkdirAll(migrationFilePath, 0777); err != nil {
|
if err := os.MkdirAll(migrationFilePath, 0777); err != nil {
|
||||||
|
122
g_scaffold.go
122
g_scaffold.go
@ -1,9 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "strings"
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func generateScaffold(sname, fields, currpath, driver, conn string) {
|
func generateScaffold(sname, fields, currpath, driver, conn string) {
|
||||||
ColorLog("[INFO] Do you want to create a '%v' model? [Yes|No] ", sname)
|
ColorLog("[INFO] Do you want to create a '%v' model? [Yes|No] ", sname)
|
||||||
@ -31,11 +28,13 @@ func generateScaffold(sname, fields, currpath, driver, conn string) {
|
|||||||
upsql := ""
|
upsql := ""
|
||||||
downsql := ""
|
downsql := ""
|
||||||
if fields != "" {
|
if fields != "" {
|
||||||
upsql = `m.SQL("CREATE TABLE ` + sname + "(" + generateSQLFromFields(fields) + `)");`
|
dbMigrator := newDBDriver()
|
||||||
downsql = `m.SQL("DROP TABLE ` + "`" + sname + "`" + `")`
|
upsql = dbMigrator.generateCreateUp(sname)
|
||||||
if driver == "" {
|
downsql = dbMigrator.generateCreateDown(sname)
|
||||||
downsql = strings.Replace(downsql, "`", "", -1)
|
//todo remove
|
||||||
}
|
//if driver == "" {
|
||||||
|
// downsql = strings.Replace(downsql, "`", "", -1)
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
generateMigration(sname, upsql, downsql, currpath)
|
generateMigration(sname, upsql, downsql, currpath)
|
||||||
}
|
}
|
||||||
@ -47,108 +46,3 @@ func generateScaffold(sname, fields, currpath, driver, conn string) {
|
|||||||
}
|
}
|
||||||
ColorLog("[INFO] All done! Don't forget to add beego.Router(\"/%v\" ,&controllers.%vController{}) to routers/route.go\n", sname, strings.Title(sname))
|
ColorLog("[INFO] All done! Don't forget to add beego.Router(\"/%v\" ,&controllers.%vController{}) to routers/route.go\n", sname, strings.Title(sname))
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateSQLFromFields(fields string) string {
|
|
||||||
sql := ""
|
|
||||||
tags := ""
|
|
||||||
fds := strings.Split(fields, ",")
|
|
||||||
for i, v := range fds {
|
|
||||||
kv := strings.SplitN(v, ":", 2)
|
|
||||||
if len(kv) != 2 {
|
|
||||||
ColorLog("[ERRO] Fields format is wrong. Should be: key:type,key:type " + v + "\n")
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
typ, tag := "", ""
|
|
||||||
switch driver {
|
|
||||||
case "mysql":
|
|
||||||
typ, tag = getSQLTypeMysql(kv[1])
|
|
||||||
case "postgres":
|
|
||||||
typ, tag = getSQLTypePostgresql(kv[1])
|
|
||||||
default:
|
|
||||||
typ, tag = getSQLTypeMysql(kv[1])
|
|
||||||
}
|
|
||||||
if typ == "" {
|
|
||||||
ColorLog("[ERRO] Fields format is wrong. Should be: key:type,key:type " + v + "\n")
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
if i == 0 && strings.ToLower(kv[0]) != "id" {
|
|
||||||
switch driver {
|
|
||||||
case "mysql":
|
|
||||||
sql = sql + "`id` int(11) NOT NULL AUTO_INCREMENT,"
|
|
||||||
tags = tags + "PRIMARY KEY (`id`),"
|
|
||||||
case "postgres":
|
|
||||||
sql = sql + "id interger serial primary key,"
|
|
||||||
default:
|
|
||||||
sql = sql + "`id` int(11) NOT NULL AUTO_INCREMENT,"
|
|
||||||
tags = tags + "PRIMARY KEY (`id`),"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sql = sql + "`" + snakeString(kv[0]) + "` " + typ + ","
|
|
||||||
if tag != "" {
|
|
||||||
tags = tags + fmt.Sprintf(tag, "`"+snakeString(kv[0])+"`") + ","
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if driver == "postgres" {
|
|
||||||
sql = strings.Replace(sql, "`", "", -1)
|
|
||||||
tags = strings.Replace(tags, "`", "", -1)
|
|
||||||
}
|
|
||||||
sql = strings.TrimRight(sql+tags, ",")
|
|
||||||
return sql
|
|
||||||
}
|
|
||||||
|
|
||||||
func getSQLTypeMysql(ktype string) (tp, tag string) {
|
|
||||||
kv := strings.SplitN(ktype, ":", 2)
|
|
||||||
switch kv[0] {
|
|
||||||
case "string":
|
|
||||||
if len(kv) == 2 {
|
|
||||||
return "varchar(" + kv[1] + ") NOT NULL", ""
|
|
||||||
}
|
|
||||||
return "varchar(128) NOT NULL", ""
|
|
||||||
case "text":
|
|
||||||
return "longtext NOT NULL", ""
|
|
||||||
case "auto":
|
|
||||||
return "int(11) NOT NULL AUTO_INCREMENT", ""
|
|
||||||
case "pk":
|
|
||||||
return "int(11) NOT NULL", "PRIMARY KEY (%s)"
|
|
||||||
case "datetime":
|
|
||||||
return "datetime NOT NULL", ""
|
|
||||||
case "int", "int8", "int16", "int32", "int64":
|
|
||||||
fallthrough
|
|
||||||
case "uint", "uint8", "uint16", "uint32", "uint64":
|
|
||||||
return "int(11) DEFAULT NULL", ""
|
|
||||||
case "bool":
|
|
||||||
return "tinyint(1) NOT NULL", ""
|
|
||||||
case "float32", "float64":
|
|
||||||
return "float NOT NULL", ""
|
|
||||||
case "float":
|
|
||||||
return "float NOT NULL", ""
|
|
||||||
}
|
|
||||||
return "", ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func getSQLTypePostgresql(ktype string) (tp, tag string) {
|
|
||||||
kv := strings.SplitN(ktype, ":", 2)
|
|
||||||
switch kv[0] {
|
|
||||||
case "string":
|
|
||||||
if len(kv) == 2 {
|
|
||||||
return "char(" + kv[1] + ") NOT NULL", ""
|
|
||||||
}
|
|
||||||
return "TEXT NOT NULL", ""
|
|
||||||
case "text":
|
|
||||||
return "TEXT NOT NULL", ""
|
|
||||||
case "auto", "pk":
|
|
||||||
return "serial primary key", ""
|
|
||||||
case "datetime":
|
|
||||||
return "TIMESTAMP WITHOUT TIME ZONE NOT NULL", ""
|
|
||||||
case "int", "int8", "int16", "int32", "int64":
|
|
||||||
fallthrough
|
|
||||||
case "uint", "uint8", "uint16", "uint32", "uint64":
|
|
||||||
return "integer DEFAULT NULL", ""
|
|
||||||
case "bool":
|
|
||||||
return "boolean NOT NULL", ""
|
|
||||||
case "float32", "float64", "float":
|
|
||||||
return "numeric NOT NULL", ""
|
|
||||||
}
|
|
||||||
return "", ""
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user