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

error when migration fold does not exit

This commit is contained in:
ZhengYang 2014-08-14 11:28:01 +08:00
parent 7c5ebae748
commit 7377c05023

View File

@ -238,7 +238,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 +258,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 +271,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 +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 // removeTempFile removes a file in dir
func removeTempFile(dir, file string) { func removeTempFile(dir, file string) {
os.Chdir(dir) os.Chdir(dir)