1
0
mirror of https://github.com/beego/bee.git synced 2025-07-12 04:41:01 +00:00
This commit is contained in:
Sergey Lanzman
2016-07-23 02:05:01 +03:00
parent b022ab3277
commit bc963e0070
12 changed files with 93 additions and 101 deletions

View File

@ -167,23 +167,23 @@ func migrate(goal, crupath, driver, connStr string) {
// checkForSchemaUpdateTable checks the existence of migrations table.
// It checks for the proper table structures and creates the table using MYSQL_MIGRATION_DDL if it does not exist.
func checkForSchemaUpdateTable(db *sql.DB, driver string) {
showTableSql := showMigrationsTableSql(driver)
if rows, err := db.Query(showTableSql); err != nil {
showTableSQL := showMigrationsTableSQL(driver)
if rows, err := db.Query(showTableSQL); err != nil {
ColorLog("[ERRO] Could not show migrations table: %s\n", err)
os.Exit(2)
} else if !rows.Next() {
// no migrations table, create anew
createTableSql := createMigrationsTableSql(driver)
createTableSQL := createMigrationsTableSQL(driver)
ColorLog("[INFO] Creating 'migrations' table...\n")
if _, err := db.Query(createTableSql); err != nil {
if _, err := db.Query(createTableSQL); err != nil {
ColorLog("[ERRO] Could not create migrations table: %s\n", err)
os.Exit(2)
}
}
// checking that migrations table schema are expected
selectTableSql := selectMigrationsTableSql(driver)
if rows, err := db.Query(selectTableSql); err != nil {
selectTableSQL := selectMigrationsTableSQL(driver)
if rows, err := db.Query(selectTableSQL); err != nil {
ColorLog("[ERRO] Could not show columns of migrations table: %s\n", err)
os.Exit(2)
} else {
@ -219,7 +219,7 @@ func checkForSchemaUpdateTable(db *sql.DB, driver string) {
}
}
func showMigrationsTableSql(driver string) string {
func showMigrationsTableSQL(driver string) string {
switch driver {
case "mysql":
return "SHOW TABLES LIKE 'migrations'"
@ -230,18 +230,18 @@ func showMigrationsTableSql(driver string) string {
}
}
func createMigrationsTableSql(driver string) string {
func createMigrationsTableSQL(driver string) string {
switch driver {
case "mysql":
return MYSQL_MIGRATION_DDL
return MYSQLMigrationDDL
case "postgres":
return POSTGRES_MIGRATION_DDL
return POSTGRESMigrationDDL
default:
return MYSQL_MIGRATION_DDL
return MYSQLMigrationDDL
}
}
func selectMigrationsTableSql(driver string) string {
func selectMigrationsTableSQL(driver string) string {
switch driver {
case "mysql":
return "DESC migrations"
@ -290,7 +290,7 @@ func writeMigrationSourceFile(dir, source, driver, connStr string, latestTime in
ColorLog("[ERRO] Could not create file: %s\n", err)
os.Exit(2)
} else {
content := strings.Replace(MIGRATION_MAIN_TPL, "{{DBDriver}}", driver, -1)
content := strings.Replace(MigrationMainTPL, "{{DBDriver}}", driver, -1)
content = strings.Replace(content, "{{ConnStr}}", connStr, -1)
content = strings.Replace(content, "{{LatestTime}}", strconv.FormatInt(latestTime, 10), -1)
content = strings.Replace(content, "{{LatestName}}", latestName, -1)
@ -369,7 +369,7 @@ func formatShellOutput(o string) {
}
const (
MIGRATION_MAIN_TPL = `package main
MigrationMainTPL = `package main
import(
"os"
@ -408,7 +408,7 @@ func main(){
}
`
MYSQL_MIGRATION_DDL = `
MYSQLMigrationDDL = `
CREATE TABLE migrations (
id_migration int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'surrogate key',
name varchar(255) DEFAULT NULL COMMENT 'migration name, unique',
@ -420,7 +420,7 @@ CREATE TABLE migrations (
) ENGINE=InnoDB DEFAULT CHARSET=utf8
`
POSTGRES_MIGRATION_DDL = `
POSTGRESMigrationDDL = `
CREATE TYPE migrations_status AS ENUM('update', 'rollback');
CREATE TABLE migrations (