1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 05:00:54 +00:00

generate migration file

This commit is contained in:
ZhengYang 2014-08-07 13:52:28 +08:00
parent b1ae637139
commit 2556579ad0
2 changed files with 82 additions and 7 deletions

6
g.go
View File

@ -90,9 +90,9 @@ func generateCode(cmd *Command, args []string) {
generateModel(string(driver), string(conn), string(level), curpath) generateModel(string(driver), string(conn), string(level), curpath)
case "migration": case "migration":
if len(args) == 2 { if len(args) == 2 {
filename := args[1] mname := args[1]
ColorLog("[INFO] Using '%s' as migration file name\n", filename) ColorLog("[INFO] Using '%s' as migration name\n", mname)
generateMigration(filename, curpath) generateMigration(mname, curpath)
} else { } else {
ColorLog("[ERRO] Wrong number of arguments\n") ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: bee generate migration [filename]\n") ColorLog("[HINT] Usage: bee generate migration [filename]\n")

View File

@ -14,9 +14,84 @@
package main package main
import "fmt" import (
"fmt"
"os"
"os/exec"
"path"
"strings"
"time"
)
func generateMigration(filename string, curpath string) { const (
fmt.Println("filename:", filename) M_PATH = "migrations"
fmt.Println("curpath:", curpath) M_DATE_FORMAT = "2006-01-02"
)
// generateMigration generates migration file template for database schema update.
// The generated file template consists of an up() method for updating schema and
// a down() method for reverting the update.
func generateMigration(mname string, curpath string) {
migrationFilePath := path.Join(curpath, M_PATH)
if _, err := os.Stat(migrationFilePath); os.IsNotExist(err) {
// create migrations directory
if err := os.Mkdir(migrationFilePath, 0777); err != nil {
ColorLog("[ERRO] Could not create migration directory: %s\n", err)
os.Exit(2)
}
}
// create file
today := time.Now().Format(M_DATE_FORMAT)
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 f.Close()
content := strings.Replace(MIGRATION_TPL, "{{StructName}}", camelCase(mname), -1)
content = strings.Replace(content, "{{DateFormat}}", M_DATE_FORMAT, -1)
content = strings.Replace(content, "{{CurrTime}}", today, -1)
f.WriteString(content)
// gofmt generated source code
formatSourceCode(fpath)
ColorLog("[INFO] Migration file generated: %s\n", fpath)
} else {
// error creating file
ColorLog("[ERRO] Could not create migration file: %s\n", err)
os.Exit(2)
}
} }
// formatSourceCode formats the source code using gofmt
func formatSourceCode(fpath string) {
cmd := exec.Command("gofmt", "-w", fpath)
cmd.Run()
}
const MIGRATION_TPL = `
package main
import (
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/migration"
)
func init() {
m := &{{StructName}}{}
m.Created = time.Parse("{{DateFormat}}", "{{CurrTime}}")
migration.Register(m)
}
type {{StructName}} struct {
migration.Migration
}
// Run the migrations
func (m *{{StructName}}) up() {
// use m.Sql("create table ...") to make schema update
}
// Reverse the migrations
func (m *{{StructName}}) down() {
// use m.Sql("drop table ...") to reverse schema update
}
`