mirror of
https://github.com/beego/bee.git
synced 2024-11-26 01:11:30 +00:00
refactor function signatures
This commit is contained in:
parent
c9b12183f8
commit
399e3ffefc
89
migrate.go
89
migrate.go
@ -19,7 +19,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmdMigrate = &Command{
|
var cmdMigrate = &Command{
|
||||||
@ -58,6 +60,8 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runMigration(cmd *Command, args []string) {
|
func runMigration(cmd *Command, args []string) {
|
||||||
|
crupath, _ := os.Getwd()
|
||||||
|
|
||||||
gopath := os.Getenv("GOPATH")
|
gopath := os.Getenv("GOPATH")
|
||||||
Debugf("gopath:%s", gopath)
|
Debugf("gopath:%s", gopath)
|
||||||
if gopath == "" {
|
if gopath == "" {
|
||||||
@ -81,19 +85,19 @@ func runMigration(cmd *Command, args []string) {
|
|||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
// run all outstanding migrations
|
// run all outstanding migrations
|
||||||
ColorLog("[INFO] Running all outstanding migrations\n")
|
ColorLog("[INFO] Running all outstanding migrations\n")
|
||||||
migrateUpdate(driverStr, connStr)
|
migrateUpdate(crupath, driverStr, connStr)
|
||||||
} else {
|
} else {
|
||||||
mcmd := args[0]
|
mcmd := args[0]
|
||||||
switch mcmd {
|
switch mcmd {
|
||||||
case "rollback":
|
case "rollback":
|
||||||
ColorLog("[INFO] Rolling back the last migration operation\n")
|
ColorLog("[INFO] Rolling back the last migration operation\n")
|
||||||
migrateRollback(driverStr, connStr)
|
migrateRollback(crupath, driverStr, connStr)
|
||||||
case "reset":
|
case "reset":
|
||||||
ColorLog("[INFO] Reseting all migrations\n")
|
ColorLog("[INFO] Reseting all migrations\n")
|
||||||
migrateReset(driverStr, connStr)
|
migrateReset(crupath, driverStr, connStr)
|
||||||
case "refresh":
|
case "refresh":
|
||||||
ColorLog("[INFO] Refreshing all migrations\n")
|
ColorLog("[INFO] Refreshing all migrations\n")
|
||||||
migrateReset(driverStr, connStr)
|
migrateReset(crupath, driverStr, connStr)
|
||||||
default:
|
default:
|
||||||
ColorLog("[ERRO] Command is missing\n")
|
ColorLog("[ERRO] Command is missing\n")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
@ -151,41 +155,47 @@ func checkForSchemaUpdateTable(db *sql.DB) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getLatestMigration(db *sql.DB) (file string, createdAt string) {
|
func getLatestMigration(db *sql.DB) (file string, createdAt int64) {
|
||||||
sql := "SELECT name, created_at FROM migrations where status = 'update' ORDER BY id_migration DESC LIMIT 1"
|
sql := "SELECT name, created_at 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 fileBytes, createdAtBytes []byte
|
var createdAtStr string
|
||||||
if rows.Next() {
|
if rows.Next() {
|
||||||
if err := rows.Scan(&fileBytes, &createdAtBytes); err != nil {
|
if err := rows.Scan(&file, &createdAtStr); 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)
|
||||||
}
|
}
|
||||||
file, createdAt = string(fileBytes), string(createdAtBytes)
|
if t, err := time.Parse("2006-01-02 15:04:05", createdAtStr); err != nil {
|
||||||
|
ColorLog("[ERRO] Could not parse time: %s\n", err)
|
||||||
|
os.Exit(2)
|
||||||
} else {
|
} else {
|
||||||
file, createdAt = "", "0"
|
createdAt = t.Unix()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
file, createdAt = "", 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTempMigrationDir(path string) {
|
func createTempMigrationDir(dir string) {
|
||||||
if err := os.MkdirAll(path, 0777); err != nil {
|
if err := os.MkdirAll(dir, 0777); err != nil {
|
||||||
ColorLog("[ERRO] Could not create path: %s\n", err)
|
ColorLog("[ERRO] Could not create directory: %s\n", err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeMigrationSourceFile(filename string, driver string, connStr string, latestTime string, latestName string, task string) {
|
func writeMigrationSourceFile(dir, source, driver, connStr string, latestTime int64, latestName string, task string) {
|
||||||
if f, err := os.OpenFile(filename+".go", os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err != nil {
|
os.Chdir(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)
|
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, "{{LatestTime}}", latestTime, -1)
|
content = strings.Replace(content, "{{LatestTime}}", strconv.FormatInt(latestTime, 10), -1)
|
||||||
content = strings.Replace(content, "{{LatestName}}", latestName, -1)
|
content = strings.Replace(content, "{{LatestName}}", latestName, -1)
|
||||||
content = strings.Replace(content, "{{Task}}", task, -1)
|
content = strings.Replace(content, "{{Task}}", task, -1)
|
||||||
if _, err := f.WriteString(content); err != nil {
|
if _, err := f.WriteString(content); err != nil {
|
||||||
@ -196,9 +206,9 @@ func writeMigrationSourceFile(filename string, driver string, connStr string, la
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildMigrationBinary(filename string) {
|
func buildMigrationBinary(dir, binary string) {
|
||||||
os.Chdir(path.Join("database", "migrations"))
|
os.Chdir(dir)
|
||||||
cmd := exec.Command("go", "build", "-o", filename)
|
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)
|
||||||
formatShellErrOutput(string(out))
|
formatShellErrOutput(string(out))
|
||||||
@ -206,8 +216,9 @@ func buildMigrationBinary(filename string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runMigrationBinary(filename string) {
|
func runMigrationBinary(dir, binary string) {
|
||||||
cmd := exec.Command("./" + filename)
|
os.Chdir(dir)
|
||||||
|
cmd := exec.Command("./" + binary)
|
||||||
if out, err := cmd.CombinedOutput(); err != nil {
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
formatShellOutput(string(out))
|
formatShellOutput(string(out))
|
||||||
ColorLog("[ERRO] Could not run migration binary: %s\n", err)
|
ColorLog("[ERRO] Could not run migration binary: %s\n", err)
|
||||||
@ -217,31 +228,34 @@ func runMigrationBinary(filename string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeMigrationBinary(path string) {
|
func removeTempFile(dir, file string) {
|
||||||
if err := os.Remove(path); err != nil {
|
os.Chdir(dir)
|
||||||
ColorLog("[ERRO] Could not remove migration binary: %s\n", err)
|
if err := os.Remove(file); err != nil {
|
||||||
|
ColorLog("[ERRO] Could not remove temporary migration files: %s\n", err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func migrateUpdate(driver, connStr string) {
|
func migrateUpdate(crupath, driver, connStr string) {
|
||||||
migrate("upgrade", driver, connStr)
|
migrate("upgrade", crupath, driver, connStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func migrateRollback(driver, connStr string) {
|
func migrateRollback(crupath, driver, connStr string) {
|
||||||
migrate("rollback", driver, connStr)
|
migrate("rollback", crupath, driver, connStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func migrateReset(driver, connStr string) {
|
func migrateReset(crupath, driver, connStr string) {
|
||||||
migrate("reset", driver, connStr)
|
migrate("reset", crupath, driver, connStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func migrateRefresh(driver, connStr string) {
|
func migrateRefresh(crupath, driver, connStr string) {
|
||||||
migrate("refresh", driver, connStr)
|
migrate("refresh", crupath, driver, connStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func migrate(goal, driver, connStr string) {
|
func migrate(goal, crupath, driver, connStr string) {
|
||||||
filepath := path.Join("database", "migrations", "migrate")
|
dir := path.Join(crupath, "database", "migrations")
|
||||||
|
binary := "m"
|
||||||
|
source := binary + ".go"
|
||||||
// connect to database
|
// connect to database
|
||||||
db, err := sql.Open(driver, connStr)
|
db, err := sql.Open(driver, connStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -251,10 +265,11 @@ func migrate(goal, driver, connStr string) {
|
|||||||
defer db.Close()
|
defer db.Close()
|
||||||
checkForSchemaUpdateTable(db)
|
checkForSchemaUpdateTable(db)
|
||||||
latestName, latestTime := getLatestMigration(db)
|
latestName, latestTime := getLatestMigration(db)
|
||||||
writeMigrationSourceFile(filepath, driver, connStr, latestTime, latestName, goal)
|
writeMigrationSourceFile(dir, source, driver, connStr, latestTime, latestName, goal)
|
||||||
buildMigrationBinary(filepath)
|
buildMigrationBinary(dir, binary)
|
||||||
runMigrationBinary(filepath)
|
runMigrationBinary(dir, binary)
|
||||||
removeMigrationBinary(filepath)
|
removeTempFile(dir, source)
|
||||||
|
removeTempFile(dir, binary)
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatShellErrOutput(o string) {
|
func formatShellErrOutput(o string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user