mirror of
https://github.com/beego/bee.git
synced 2024-11-01 00:00:53 +00:00
Merge pull request #48 from ZhengYang/master
change created time to be time of execution
This commit is contained in:
commit
5a6ed4d75c
21
migrate.go
21
migrate.go
@ -151,11 +151,7 @@ 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, goal)
|
||||||
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)
|
||||||
@ -215,25 +211,30 @@ func checkForSchemaUpdateTable(db *sql.DB) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getLatestMigration retrives latest migration with status 'update'
|
// getLatestMigration retrives latest migration with status 'update'
|
||||||
func getLatestMigration(db *sql.DB) (file string, createdAt int64) {
|
func getLatestMigration(db *sql.DB, goal string) (file string, createdAt int64) {
|
||||||
sql := "SELECT name, created_at FROM migrations where status = 'update' ORDER BY id_migration DESC LIMIT 1"
|
sql := "SELECT name FROM migrations where status = 'update' ORDER BY id_migration DESC LIMIT 1"
|
||||||
if rows, err := db.Query(sql); err != nil {
|
if rows, err := db.Query(sql); err != nil {
|
||||||
ColorLog("[ERRO] Could not retrieve migrations: %s\n", err)
|
ColorLog("[ERRO] Could not retrieve migrations: %s\n", err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
} else {
|
} else {
|
||||||
var createdAtStr string
|
|
||||||
if rows.Next() {
|
if rows.Next() {
|
||||||
if err := rows.Scan(&file, &createdAtStr); err != nil {
|
if err := rows.Scan(&file); err != nil {
|
||||||
ColorLog("[ERRO] Could not read migrations in database: %s\n", err)
|
ColorLog("[ERRO] Could not read migrations in database: %s\n", err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
if t, err := time.Parse("2006-01-02 15:04:05", createdAtStr); err != nil {
|
createdAtStr := file[len(file)-15:]
|
||||||
|
if t, err := time.Parse("20060102_150405", createdAtStr); err != nil {
|
||||||
ColorLog("[ERRO] Could not parse time: %s\n", err)
|
ColorLog("[ERRO] Could not parse time: %s\n", err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
} else {
|
} else {
|
||||||
createdAt = t.Unix()
|
createdAt = t.Unix()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// migration table has no 'update' record, no point rolling back
|
||||||
|
if goal == "rollback" {
|
||||||
|
ColorLog("[ERRO] There is nothing to rollback\n")
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
file, createdAt = "", 0
|
file, createdAt = "", 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user