mirror of
https://github.com/beego/bee.git
synced 2024-11-25 20:10:55 +00:00
change created time to be time of execution
This commit is contained in:
parent
ef8e6750a0
commit
ea3b10f2dc
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