1
0
mirror of https://github.com/beego/bee.git synced 2024-11-26 11:31:29 +00:00

Merge pull request #50 from ZhengYang/master

Add onsite 'migrate' function for scaffolding feature & minor code refactor
This commit is contained in:
astaxie 2014-08-14 15:21:34 +08:00
commit 78ba508062
3 changed files with 32 additions and 14 deletions

34
g.go
View File

@ -20,13 +20,15 @@ var cmdGenerate = &Command{
UsageLine: "generate [Command]", UsageLine: "generate [Command]",
Short: "generate code based on application", Short: "generate code based on application",
Long: ` Long: `
bee generate scaffold [scaffoldname] [-fields=""] bee generate scaffold [scaffoldname] [-fields=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
The generate scaffold command will do a number of things for you The generate scaffold command will do a number of things for you.
-fields: a list of database fields. -fields: a list of database fields.
example: bee generate scaffold post -fields="title:string,body:text" -driver: [mysql | postgresql | sqlite], the default is mysql
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
example: bee generate scaffold post -fields="title:string,body:text"
bee generate model [modelname] [-fields=""] bee generate model [modelname] [-fields=""]
generate RESTFul model based on fields generate RESTFul model based on fields
bee generate controller [controllerfile] bee generate controller [controllerfile]
generate RESTFul controllers generate RESTFul controllers
@ -43,7 +45,7 @@ bee generate docs
bee generate test [routerfile] bee generate test [routerfile]
generate testcase generate testcase
bee generate appcode [-tables=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test] [-level=3] bee generate appcode [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-level=3]
generate appcode based on an existing database generate appcode based on an existing database
-tables: a list of table names separated by ',', default is empty, indicating all tables -tables: a list of table names separated by ',', default is empty, indicating all tables
-driver: [mysql | postgresql | sqlite], the default is mysql -driver: [mysql | postgresql | sqlite], the default is mysql
@ -90,7 +92,23 @@ func generateCode(cmd *Command, args []string) {
ColorLog("[HINT] Usage: bee generate scaffold [scaffoldname] [-fields=\"\"]\n") ColorLog("[HINT] Usage: bee generate scaffold [scaffoldname] [-fields=\"\"]\n")
os.Exit(2) os.Exit(2)
} }
err := loadConfig()
if err != nil {
ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err)
}
cmd.Flag.Parse(args[2:]) cmd.Flag.Parse(args[2:])
if driver == "" {
driver = docValue(conf.Database.Driver)
if driver == "" {
driver = "mysql"
}
}
if conn == "" {
conn = docValue(conf.Database.Conn)
if conn == "" {
conn = "root:@tcp(127.0.0.1:3306)/test"
}
}
if fields == "" { if fields == "" {
ColorLog("[ERRO] Wrong number of arguments\n") ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: bee generate scaffold [scaffoldname] [-fields=\"title:string,body:text\"]\n") ColorLog("[HINT] Usage: bee generate scaffold [scaffoldname] [-fields=\"title:string,body:text\"]\n")
@ -98,7 +116,7 @@ func generateCode(cmd *Command, args []string) {
} }
sname := args[1] sname := args[1]
ColorLog("[INFO] Using '%s' as scaffold name\n", sname) ColorLog("[INFO] Using '%s' as scaffold name\n", sname)
generateScaffold(sname, fields.String(), curpath) generateScaffold(sname, fields.String(), curpath, driver.String(), conn.String())
case "docs": case "docs":
generateDocs(curpath) generateDocs(curpath)
case "appcode": case "appcode":
@ -127,7 +145,7 @@ func generateCode(cmd *Command, args []string) {
ColorLog("[INFO] Using '%s' as 'conn'\n", conn) ColorLog("[INFO] Using '%s' as 'conn'\n", conn)
ColorLog("[INFO] Using '%s' as 'tables'\n", tables) ColorLog("[INFO] Using '%s' as 'tables'\n", tables)
ColorLog("[INFO] Using '%s' as 'level'\n", level) ColorLog("[INFO] Using '%s' as 'level'\n", level)
generateAppcode(string(driver), string(conn), string(level), string(tables), curpath) generateAppcode(driver.String(), conn.String(), level.String(), tables.String(), curpath)
case "migration": case "migration":
if len(args) == 2 { if len(args) == 2 {
mname := args[1] mname := args[1]

View File

@ -2,7 +2,7 @@ package main
import "strings" import "strings"
func generateScaffold(sname, fields, crupath string) { func generateScaffold(sname, fields, crupath, driver, conn string) {
// generate model // generate model
ColorLog("[INFO] Do you want me to create a %v model? [yes|no]] ", sname) ColorLog("[INFO] Do you want me to create a %v model? [yes|no]] ", sname)
if askForConfirmation() { if askForConfirmation() {
@ -27,7 +27,7 @@ func generateScaffold(sname, fields, crupath string) {
// run migration // run migration
ColorLog("[INFO] Do you want to go ahead and migrate the database? [yes|no]] ") ColorLog("[INFO] Do you want to go ahead and migrate the database? [yes|no]] ")
if askForConfirmation() { if askForConfirmation() {
// @todo migrateUpdate(crupath, driver, conn)
} }
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))
} }

View File

@ -29,22 +29,22 @@ var cmdMigrate = &Command{
UsageLine: "migrate [Command]", UsageLine: "migrate [Command]",
Short: "run database migrations", Short: "run database migrations",
Long: ` Long: `
bee migrate bee migrate [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
run all outstanding migrations run all outstanding migrations
-driver: [mysql | postgresql | sqlite], the default is mysql -driver: [mysql | postgresql | sqlite], the default is mysql
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test -conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
bee migrate rollback bee migrate rollback [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
rollback the last migration operation rollback the last migration operation
-driver: [mysql | postgresql | sqlite], the default is mysql -driver: [mysql | postgresql | sqlite], the default is mysql
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test -conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
bee migrate reset bee migrate reset [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
rollback all migrations rollback all migrations
-driver: [mysql | postgresql | sqlite], the default is mysql -driver: [mysql | postgresql | sqlite], the default is mysql
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test -conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
bee migrate refresh bee migrate refresh [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
rollback all migrations and run them all again rollback all migrations and run them all again
-driver: [mysql | postgresql | sqlite], the default is mysql -driver: [mysql | postgresql | sqlite], the default is mysql
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test -conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test