1
0
mirror of https://github.com/beego/bee.git synced 2024-11-26 06:21:34 +00:00

Merge pull request #117 from westonplatter/master

migrate. allow postgres migrations to run.
This commit is contained in:
astaxie 2015-05-21 15:58:25 +08:00
commit fadb5d2af9

View File

@ -153,7 +153,7 @@ func migrate(goal, crupath, driver, connStr string) {
os.Exit(2) os.Exit(2)
} }
defer db.Close() defer db.Close()
checkForSchemaUpdateTable(db) checkForSchemaUpdateTable(db, driver)
latestName, latestTime := getLatestMigration(db, goal) latestName, latestTime := getLatestMigration(db, goal)
writeMigrationSourceFile(dir, source, driver, connStr, latestTime, latestName, goal) writeMigrationSourceFile(dir, source, driver, connStr, latestTime, latestName, goal)
buildMigrationBinary(dir, binary) buildMigrationBinary(dir, binary)
@ -164,20 +164,24 @@ func migrate(goal, crupath, driver, connStr string) {
// checkForSchemaUpdateTable checks the existence of migrations table. // 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. // 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) { func checkForSchemaUpdateTable(db *sql.DB, driver string) {
if rows, err := db.Query("SHOW TABLES LIKE 'migrations'"); err != nil { showTableSql := showMigrationsTableSql(driver)
if rows, err := db.Query(showTableSql); 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)
} else if !rows.Next() { } else if !rows.Next() {
// no migrations table, create anew // no migrations table, create anew
createTableSql := createMigrationsTableSql(driver)
ColorLog("[INFO] Creating 'migrations' table...\n") ColorLog("[INFO] Creating 'migrations' table...\n")
if _, err := db.Query(MYSQL_MIGRATION_DDL); err != nil { if _, err := db.Query(createTableSql); err != nil {
ColorLog("[ERRO] Could not create migrations table: %s\n", err) ColorLog("[ERRO] Could not create migrations table: %s\n", err)
os.Exit(2) os.Exit(2)
} }
} }
// checking that migrations table schema are expected // checking that migrations table schema are expected
if rows, err := db.Query("DESC migrations"); 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) ColorLog("[ERRO] Could not show columns of migrations table: %s\n", err)
os.Exit(2) os.Exit(2)
} else { } else {
@ -213,6 +217,39 @@ func checkForSchemaUpdateTable(db *sql.DB) {
} }
} }
func showMigrationsTableSql(driver string) string {
switch driver {
case "mysql":
return "SHOW TABLES LIKE 'migrations'"
case "postgres":
return "SELECT * FROM pg_catalog.pg_tables WHERE tablename = 'migrations';"
default:
return "SHOW TABLES LIKE 'migrations'"
}
}
func createMigrationsTableSql(driver string) string {
switch driver {
case "mysql":
return MYSQL_MIGRATION_DDL
case "postgres":
return POSTGRES_MIGRATION_DDL
default:
return MYSQL_MIGRATION_DDL
}
}
func selectMigrationsTableSql(driver string) string {
switch driver {
case "mysql":
return "DESC migrations"
case "postgres":
return "SELECT * FROM migrations ORDER BY id_migration;"
default:
return "DESC migrations"
}
}
// getLatestMigration retrives latest migration with status 'update' // getLatestMigration retrives latest migration with status 'update'
func getLatestMigration(db *sql.DB, goal string) (file string, createdAt int64) { func getLatestMigration(db *sql.DB, goal string) (file string, createdAt int64) {
sql := "SELECT name 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"
@ -339,6 +376,7 @@ import(
"github.com/astaxie/beego/migration" "github.com/astaxie/beego/migration"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
) )
func init(){ func init(){
@ -379,4 +417,16 @@ CREATE TABLE migrations (
PRIMARY KEY (id_migration) PRIMARY KEY (id_migration)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ) ENGINE=InnoDB DEFAULT CHARSET=utf8
` `
POSTGRES_MIGRATION_DDL = `
CREATE TYPE migrations_status AS ENUM('update', 'rollback');
CREATE TABLE migrations (
id_migration SERIAL PRIMARY KEY,
name varchar(255) DEFAULT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
statements text,
rollback_statements text,
status migrations_status
)`
) )