mirror of
https://github.com/beego/bee.git
synced 2024-11-22 05:00:54 +00:00
get latest migration from database
This commit is contained in:
parent
6f4c1b3a89
commit
870698f5a4
64
migrate.go
64
migrate.go
@ -83,13 +83,7 @@ func runMigration(cmd *Command, args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkForSchemaUpdateTable(driver string, connStr string) {
|
func checkForSchemaUpdateTable(db *sql.DB) {
|
||||||
db, err := sql.Open(driver, connStr)
|
|
||||||
if err != nil {
|
|
||||||
ColorLog("[ERRO] Could not connect to %s: %s\n", driver, connStr)
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
if rows, err := db.Query("SHOW TABLES LIKE 'migrations'"); err != nil {
|
if rows, err := db.Query("SHOW TABLES LIKE 'migrations'"); err != nil {
|
||||||
ColorLog("[ERRO] Could not show migrations table: %s\n", err)
|
ColorLog("[ERRO] Could not show migrations table: %s\n", err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
@ -138,6 +132,26 @@ func checkForSchemaUpdateTable(driver string, connStr string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getLatestMigration(db *sql.DB) (file string, createdAt string) {
|
||||||
|
sql := "SELECT file, created_at FROM migrations ORDER BY id_migration DESC LIMIT 1"
|
||||||
|
if rows, err := db.Query(sql); err != nil {
|
||||||
|
ColorLog("[ERRO] Could not retrieve migrations: %s\n", err)
|
||||||
|
os.Exit(2)
|
||||||
|
} else {
|
||||||
|
var fileBytes, createdAtBytes []byte
|
||||||
|
if rows.Next() {
|
||||||
|
if err := rows.Scan(&fileBytes, &createdAtBytes); err != nil {
|
||||||
|
ColorLog("[ERRO] Could not read migrations in database: %s\n", err)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
file, createdAt = string(fileBytes), string(createdAtBytes)
|
||||||
|
} else {
|
||||||
|
file, createdAt = "", "0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func createTempMigrationDir(path string) {
|
func createTempMigrationDir(path string) {
|
||||||
if err := os.MkdirAll(path, 0777); err != nil {
|
if err := os.MkdirAll(path, 0777); err != nil {
|
||||||
ColorLog("[ERRO] Could not create path: %s\n", err)
|
ColorLog("[ERRO] Could not create path: %s\n", err)
|
||||||
@ -145,14 +159,16 @@ func createTempMigrationDir(path string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeMigrationSourceFile(filename string, driver string, connStr string) {
|
func writeMigrationSourceFile(filename string, driver string, connStr string, latestTime string, latestName string, task string) {
|
||||||
if f, err := os.OpenFile(filename+".go", os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err != nil {
|
if f, err := os.OpenFile(filename+".go", 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)
|
||||||
} else {
|
} else {
|
||||||
content := strings.Replace(MIGRATION_MAIN_TPL, "{{DBDriver}}", driver, -1)
|
content := strings.Replace(MIGRATION_MAIN_TPL, "{{DBDriver}}", driver, -1)
|
||||||
content = strings.Replace(content, "{{ConnStr}}", connStr, -1)
|
content = strings.Replace(content, "{{ConnStr}}", connStr, -1)
|
||||||
content = strings.Replace(content, "{{CurrTime}}", "123", -1)
|
content = strings.Replace(content, "{{LatestTime}}", latestTime, -1)
|
||||||
|
content = strings.Replace(content, "{{LatestName}}", latestName, -1)
|
||||||
|
content = strings.Replace(content, "{{Task}}", task, -1)
|
||||||
if _, err := f.WriteString(content); err != nil {
|
if _, err := f.WriteString(content); err != nil {
|
||||||
ColorLog("[ERRO] Could not write to file: %s\n", err)
|
ColorLog("[ERRO] Could not write to file: %s\n", err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
@ -188,10 +204,19 @@ func cleanUpMigrationFiles(tmpPath string) {
|
|||||||
|
|
||||||
func migrateUpdate() {
|
func migrateUpdate() {
|
||||||
connStr := "root:@tcp(127.0.0.1:3306)/sgfas?charset=utf8"
|
connStr := "root:@tcp(127.0.0.1:3306)/sgfas?charset=utf8"
|
||||||
checkForSchemaUpdateTable("mysql", connStr)
|
driver := "mysql"
|
||||||
filename := path.Join(TMP_DIR, "super")
|
filename := path.Join(TMP_DIR, "super")
|
||||||
|
// connect to database
|
||||||
|
db, err := sql.Open(driver, connStr)
|
||||||
|
if err != nil {
|
||||||
|
ColorLog("[ERRO] Could not connect to %s: %s\n", driver, connStr)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
checkForSchemaUpdateTable(db)
|
||||||
|
latestTime, latestName := getLatestMigration(db)
|
||||||
createTempMigrationDir(TMP_DIR)
|
createTempMigrationDir(TMP_DIR)
|
||||||
writeMigrationSourceFile(filename, "mysql", connStr)
|
writeMigrationSourceFile(filename, driver, connStr, latestTime, latestName, "upgrade")
|
||||||
buildMigrationBinary(filename)
|
buildMigrationBinary(filename)
|
||||||
runMigrationBinary(filename)
|
runMigrationBinary(filename)
|
||||||
cleanUpMigrationFiles(TMP_DIR)
|
cleanUpMigrationFiles(TMP_DIR)
|
||||||
@ -204,8 +229,6 @@ func migrateReset() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func migrateRefresh() {
|
func migrateRefresh() {
|
||||||
migrateReset()
|
|
||||||
migrateUpdate()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -221,10 +244,17 @@ func init(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main(){
|
func main(){
|
||||||
migration.Upgrade({{CurrTime}})
|
task := {{Task}}
|
||||||
//migration.Rollback()
|
switch task {
|
||||||
//migration.Reset()
|
case "upgrade":
|
||||||
//migration.Refresh()
|
migration.Upgrade({{LatestTime}})
|
||||||
|
case "rollback":
|
||||||
|
migration.Rollback("{{LatestName}}")
|
||||||
|
case "reset":
|
||||||
|
migration.Reset()
|
||||||
|
case "refresh":
|
||||||
|
migration.Refresh()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
`
|
`
|
||||||
|
Loading…
Reference in New Issue
Block a user