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

Merge pull request #47 from ZhengYang/master

Bug Fixes, Error Improvement, Color log
This commit is contained in:
astaxie 2014-08-14 13:51:55 +08:00
commit 8e3d2bb4ff
2 changed files with 20 additions and 4 deletions

View File

@ -109,7 +109,7 @@ func runMigration(cmd *Command, args []string) {
migrateReset(crupath, driverStr, connStr) migrateReset(crupath, driverStr, connStr)
case "refresh": case "refresh":
ColorLog("[INFO] Refreshing all migrations\n") ColorLog("[INFO] Refreshing all migrations\n")
migrateReset(crupath, driverStr, connStr) migrateRefresh(crupath, driverStr, connStr)
default: default:
ColorLog("[ERRO] Command is missing\n") ColorLog("[ERRO] Command is missing\n")
os.Exit(2) os.Exit(2)
@ -152,6 +152,10 @@ func migrate(goal, crupath, driver, connStr string) {
defer db.Close() defer db.Close()
checkForSchemaUpdateTable(db) checkForSchemaUpdateTable(db)
latestName, latestTime := getLatestMigration(db) latestName, latestTime := getLatestMigration(db)
if goal == "rollback" && latestName == "" {
ColorLog("[ERRO] There is nothing to rollback\n")
os.Exit(2)
}
writeMigrationSourceFile(dir, source, driver, connStr, latestTime, latestName, goal) writeMigrationSourceFile(dir, source, driver, connStr, latestTime, latestName, goal)
buildMigrationBinary(dir, binary) buildMigrationBinary(dir, binary)
runMigrationBinary(dir, binary) runMigrationBinary(dir, binary)
@ -238,7 +242,7 @@ func getLatestMigration(db *sql.DB) (file string, createdAt int64) {
// writeMigrationSourceFile create the source file based on MIGRATION_MAIN_TPL // writeMigrationSourceFile create the source file based on MIGRATION_MAIN_TPL
func writeMigrationSourceFile(dir, source, driver, connStr string, latestTime int64, latestName string, task string) { func writeMigrationSourceFile(dir, source, driver, connStr string, latestTime int64, latestName string, task string) {
os.Chdir(dir) changeDir(dir)
if f, err := os.OpenFile(source, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err != nil { if f, err := os.OpenFile(source, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err != nil {
ColorLog("[ERRO] Could not create file: %s\n", err) ColorLog("[ERRO] Could not create file: %s\n", err)
os.Exit(2) os.Exit(2)
@ -258,7 +262,7 @@ func writeMigrationSourceFile(dir, source, driver, connStr string, latestTime in
// buildMigrationBinary changes directory to database/migrations folder and go-build the source // buildMigrationBinary changes directory to database/migrations folder and go-build the source
func buildMigrationBinary(dir, binary string) { func buildMigrationBinary(dir, binary string) {
os.Chdir(dir) changeDir(dir)
cmd := exec.Command("go", "build", "-o", binary) cmd := exec.Command("go", "build", "-o", binary)
if out, err := cmd.CombinedOutput(); err != nil { if out, err := cmd.CombinedOutput(); err != nil {
ColorLog("[ERRO] Could not build migration binary: %s\n", err) ColorLog("[ERRO] Could not build migration binary: %s\n", err)
@ -271,7 +275,7 @@ func buildMigrationBinary(dir, binary string) {
// runMigrationBinary runs the migration program who does the actual work // runMigrationBinary runs the migration program who does the actual work
func runMigrationBinary(dir, binary string) { func runMigrationBinary(dir, binary string) {
os.Chdir(dir) changeDir(dir)
cmd := exec.Command("./" + binary) cmd := exec.Command("./" + binary)
if out, err := cmd.CombinedOutput(); err != nil { if out, err := cmd.CombinedOutput(); err != nil {
formatShellOutput(string(out)) formatShellOutput(string(out))
@ -284,6 +288,15 @@ func runMigrationBinary(dir, binary string) {
} }
} }
// changeDir changes working directory to dir.
// It exits the system when encouter an error
func changeDir(dir string) {
if err := os.Chdir(dir); err != nil {
ColorLog("[ERRO] Could not find migration directory: %s\n", err)
os.Exit(2)
}
}
// removeTempFile removes a file in dir // removeTempFile removes a file in dir
func removeTempFile(dir, file string) { func removeTempFile(dir, file string) {
os.Chdir(dir) os.Chdir(dir)

View File

@ -58,6 +58,7 @@ const (
//NRed = uint8(31) // Normal //NRed = uint8(31) // Normal
EndColor = "\033[0m" EndColor = "\033[0m"
INFO = "INFO"
TRAC = "TRAC" TRAC = "TRAC"
ERRO = "ERRO" ERRO = "ERRO"
WARN = "WARN" WARN = "WARN"
@ -139,6 +140,8 @@ func ColorLogS(format string, a ...interface{}) string {
func getColorLevel(level string) string { func getColorLevel(level string) string {
level = strings.ToUpper(level) level = strings.ToUpper(level)
switch level { switch level {
case INFO:
return fmt.Sprintf("\033[%dm%s\033[0m", Blue, level)
case TRAC: case TRAC:
return fmt.Sprintf("\033[%dm%s\033[0m", Blue, level) return fmt.Sprintf("\033[%dm%s\033[0m", Blue, level)
case ERRO: case ERRO: