1
0
mirror of https://github.com/beego/bee.git synced 2024-12-22 20:20:49 +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]",
Short: "generate code based on application",
Long: `
bee generate scaffold [scaffoldname] [-fields=""]
The generate scaffold command will do a number of things for you
-fields: a list of database fields.
example: bee generate scaffold post -fields="title:string,body:text"
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.
-fields: a list of database fields.
-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=""]
generate RESTFul model based on fields
generate RESTFul model based on fields
bee generate controller [controllerfile]
generate RESTFul controllers
@ -43,7 +45,7 @@ bee generate docs
bee generate test [routerfile]
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
-tables: a list of table names separated by ',', default is empty, indicating all tables
-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")
os.Exit(2)
}
err := loadConfig()
if err != nil {
ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err)
}
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 == "" {
ColorLog("[ERRO] Wrong number of arguments\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]
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":
generateDocs(curpath)
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 'tables'\n", tables)
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":
if len(args) == 2 {
mname := args[1]

View File

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

View File

@ -29,22 +29,22 @@ var cmdMigrate = &Command{
UsageLine: "migrate [Command]",
Short: "run database migrations",
Long: `
bee migrate
bee migrate [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
run all outstanding migrations
-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
bee migrate rollback
bee migrate rollback [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
rollback the last migration operation
-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
bee migrate reset
bee migrate reset [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
rollback all migrations
-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
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
-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