From 7377c050235a101e0e9a4820346e7e5ec4dfe95f Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Thu, 14 Aug 2014 11:28:01 +0800 Subject: [PATCH 1/4] error when migration fold does not exit --- migrate.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/migrate.go b/migrate.go index 34ce205..d4b24d2 100644 --- a/migrate.go +++ b/migrate.go @@ -238,7 +238,7 @@ func getLatestMigration(db *sql.DB) (file string, createdAt int64) { // writeMigrationSourceFile create the source file based on MIGRATION_MAIN_TPL 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 { ColorLog("[ERRO] Could not create file: %s\n", err) os.Exit(2) @@ -258,7 +258,7 @@ func writeMigrationSourceFile(dir, source, driver, connStr string, latestTime in // buildMigrationBinary changes directory to database/migrations folder and go-build the source func buildMigrationBinary(dir, binary string) { - os.Chdir(dir) + changeDir(dir) cmd := exec.Command("go", "build", "-o", binary) if out, err := cmd.CombinedOutput(); err != nil { ColorLog("[ERRO] Could not build migration binary: %s\n", err) @@ -271,7 +271,7 @@ func buildMigrationBinary(dir, binary string) { // runMigrationBinary runs the migration program who does the actual work func runMigrationBinary(dir, binary string) { - os.Chdir(dir) + changeDir(dir) cmd := exec.Command("./" + binary) if out, err := cmd.CombinedOutput(); err != nil { formatShellOutput(string(out)) @@ -284,6 +284,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 func removeTempFile(dir, file string) { os.Chdir(dir) From 104b35e062c4d18f727e896bec07d12046583a41 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Thu, 14 Aug 2014 12:10:03 +0800 Subject: [PATCH 2/4] bugfix: call refresh function when refreshing --- migrate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrate.go b/migrate.go index d4b24d2..c65fae7 100644 --- a/migrate.go +++ b/migrate.go @@ -109,7 +109,7 @@ func runMigration(cmd *Command, args []string) { migrateReset(crupath, driverStr, connStr) case "refresh": ColorLog("[INFO] Refreshing all migrations\n") - migrateReset(crupath, driverStr, connStr) + migrateRefresh(crupath, driverStr, connStr) default: ColorLog("[ERRO] Command is missing\n") os.Exit(2) From 923a02f99ba268f79b82fdd7bc109e47c91e42d1 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Thu, 14 Aug 2014 12:20:49 +0800 Subject: [PATCH 3/4] colorlog for [INFO] --- util.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/util.go b/util.go index 183f058..0e24d08 100644 --- a/util.go +++ b/util.go @@ -58,6 +58,7 @@ const ( //NRed = uint8(31) // Normal EndColor = "\033[0m" + INFO = "INFO" TRAC = "TRAC" ERRO = "ERRO" WARN = "WARN" @@ -139,6 +140,8 @@ func ColorLogS(format string, a ...interface{}) string { func getColorLevel(level string) string { level = strings.ToUpper(level) switch level { + case INFO: + return fmt.Sprintf("\033[%dm%s\033[0m", Blue, level) case TRAC: return fmt.Sprintf("\033[%dm%s\033[0m", Blue, level) case ERRO: From ef8e6750a0b792937377fe97dcb56d737d90a039 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Thu, 14 Aug 2014 13:10:42 +0800 Subject: [PATCH 4/4] halt when there is nothing to rollback --- migrate.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/migrate.go b/migrate.go index c65fae7..afe4257 100644 --- a/migrate.go +++ b/migrate.go @@ -152,6 +152,10 @@ func migrate(goal, crupath, driver, connStr string) { defer db.Close() checkForSchemaUpdateTable(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) buildMigrationBinary(dir, binary) runMigrationBinary(dir, binary)