1
0
mirror of https://github.com/beego/bee.git synced 2024-11-10 20:10:54 +00:00
bee/migrate.go

435 lines
13 KiB
Go
Raw Normal View History

2014-08-11 03:33:53 +00:00
// Copyright 2013 bee authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package main
2014-08-11 09:23:52 +00:00
import (
"database/sql"
"fmt"
2014-08-11 09:23:52 +00:00
"os"
"os/exec"
"path"
2014-08-13 03:53:55 +00:00
"strconv"
2014-08-11 09:23:52 +00:00
"strings"
2014-08-13 03:53:55 +00:00
"time"
2014-08-11 09:23:52 +00:00
)
2014-08-11 03:33:53 +00:00
var cmdMigrate = &Command{
UsageLine: "migrate [Command]",
Short: "run database migrations",
Long: `
2014-08-14 06:57:57 +00:00
bee migrate [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
2014-08-11 03:33:53 +00:00
run all outstanding migrations
-driver: [mysql | postgres | sqlite] (default: mysql)
2014-08-12 07:48:01 +00:00
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
2014-08-11 03:33:53 +00:00
2014-08-14 06:57:57 +00:00
bee migrate rollback [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
2014-08-11 03:33:53 +00:00
rollback the last migration operation
-driver: [mysql | postgres | sqlite] (default: mysql)
2014-08-12 07:48:01 +00:00
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
2014-08-11 03:33:53 +00:00
2014-08-14 06:57:57 +00:00
bee migrate reset [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
2014-08-11 03:33:53 +00:00
rollback all migrations
-driver: [mysql | postgres | sqlite] (default: mysql)
2014-08-12 07:48:01 +00:00
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
2014-08-11 03:33:53 +00:00
2014-08-14 06:57:57 +00:00
bee migrate refresh [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
2014-08-11 03:33:53 +00:00
rollback all migrations and run them all again
-driver: [mysql | postgres | sqlite] (default: mysql)
2014-08-12 07:48:01 +00:00
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
2014-08-11 03:33:53 +00:00
`,
}
var mDriver docValue
var mConn docValue
2014-08-11 03:33:53 +00:00
func init() {
cmdMigrate.Run = runMigration
cmdMigrate.Flag.Var(&mDriver, "driver", "database driver: mysql, postgres, sqlite, etc.")
cmdMigrate.Flag.Var(&mConn, "conn", "connection string used by the driver to connect to a database instance")
2014-08-11 03:33:53 +00:00
}
2014-08-13 04:35:24 +00:00
// runMigration is the entry point for starting a migration
2014-08-15 09:38:51 +00:00
func runMigration(cmd *Command, args []string) int {
ShowShortVersionBanner()
2014-08-13 03:53:55 +00:00
crupath, _ := os.Getwd()
2014-08-11 03:33:53 +00:00
gopath := os.Getenv("GOPATH")
Debugf("gopath:%s", gopath)
if gopath == "" {
ColorLog("[ERRO] $GOPATH not found\n")
ColorLog("[HINT] Set $GOPATH in your environment vairables\n")
os.Exit(2)
}
// load config
err := loadConfig()
if err != nil {
ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err)
}
// getting command line arguments
if len(args) != 0 {
cmd.Flag.Parse(args[1:])
}
if mDriver == "" {
mDriver = docValue(conf.Database.Driver)
if mDriver == "" {
mDriver = "mysql"
}
}
if mConn == "" {
mConn = docValue(conf.Database.Conn)
if mConn == "" {
mConn = "root:@tcp(127.0.0.1:3306)/test"
}
}
ColorLog("[INFO] Using '%s' as 'driver'\n", mDriver)
ColorLog("[INFO] Using '%s' as 'conn'\n", mConn)
driverStr, connStr := string(mDriver), string(mConn)
2014-08-11 03:33:53 +00:00
if len(args) == 0 {
// run all outstanding migrations
2014-08-11 09:23:52 +00:00
ColorLog("[INFO] Running all outstanding migrations\n")
2014-08-13 03:53:55 +00:00
migrateUpdate(crupath, driverStr, connStr)
2014-08-11 03:33:53 +00:00
} else {
mcmd := args[0]
switch mcmd {
case "rollback":
2014-08-11 09:23:52 +00:00
ColorLog("[INFO] Rolling back the last migration operation\n")
2014-08-13 03:53:55 +00:00
migrateRollback(crupath, driverStr, connStr)
2014-08-11 03:33:53 +00:00
case "reset":
2014-08-11 09:23:52 +00:00
ColorLog("[INFO] Reseting all migrations\n")
2014-08-13 03:53:55 +00:00
migrateReset(crupath, driverStr, connStr)
2014-08-11 03:33:53 +00:00
case "refresh":
2014-08-11 09:23:52 +00:00
ColorLog("[INFO] Refreshing all migrations\n")
migrateRefresh(crupath, driverStr, connStr)
2014-08-11 03:33:53 +00:00
default:
2014-08-11 09:23:52 +00:00
ColorLog("[ERRO] Command is missing\n")
2014-08-11 03:33:53 +00:00
os.Exit(2)
}
2014-08-11 09:23:52 +00:00
}
2014-08-12 07:18:36 +00:00
ColorLog("[SUCC] Migration successful!\n")
2014-08-15 09:38:51 +00:00
return 0
2014-08-11 09:23:52 +00:00
}
2014-08-13 04:35:24 +00:00
// migrateUpdate does the schema update
2014-08-13 03:57:36 +00:00
func migrateUpdate(crupath, driver, connStr string) {
migrate("upgrade", crupath, driver, connStr)
}
2014-08-13 04:35:24 +00:00
// migrateRollback rolls back the latest migration
2014-08-13 03:57:36 +00:00
func migrateRollback(crupath, driver, connStr string) {
migrate("rollback", crupath, driver, connStr)
}
2014-08-13 04:35:24 +00:00
// migrateReset rolls back all migrations
2014-08-13 03:57:36 +00:00
func migrateReset(crupath, driver, connStr string) {
migrate("reset", crupath, driver, connStr)
}
2014-08-13 04:35:24 +00:00
// migrationRefresh rolls back all migrations and start over again
2014-08-13 03:57:36 +00:00
func migrateRefresh(crupath, driver, connStr string) {
migrate("refresh", crupath, driver, connStr)
}
2014-08-13 04:35:24 +00:00
// migrate generates source code, build it, and invoke the binary who does the actual migration
2014-08-13 03:57:36 +00:00
func migrate(goal, crupath, driver, connStr string) {
dir := path.Join(crupath, "database", "migrations")
binary := "m"
source := binary + ".go"
// connect to database
db, err := sql.Open(driver, connStr)
if err != nil {
ColorLog("[ERRO] Could not connect to %s: %s\n", driver, connStr)
2015-05-20 04:41:13 +00:00
ColorLog("[ERRO] Error: %v", err.Error())
2014-08-13 03:57:36 +00:00
os.Exit(2)
}
defer db.Close()
checkForSchemaUpdateTable(db, driver)
latestName, latestTime := getLatestMigration(db, goal)
2014-08-13 03:57:36 +00:00
writeMigrationSourceFile(dir, source, driver, connStr, latestTime, latestName, goal)
buildMigrationBinary(dir, binary)
runMigrationBinary(dir, binary)
removeTempFile(dir, source)
removeTempFile(dir, binary)
}
2014-08-13 04:35:24 +00:00
// 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) {
2016-07-22 23:05:01 +00:00
showTableSQL := showMigrationsTableSQL(driver)
if rows, err := db.Query(showTableSQL); err != nil {
2014-08-11 09:23:52 +00:00
ColorLog("[ERRO] Could not show migrations table: %s\n", err)
os.Exit(2)
} else if !rows.Next() {
// no migrations table, create anew
2016-07-22 23:05:01 +00:00
createTableSQL := createMigrationsTableSQL(driver)
2014-08-11 09:23:52 +00:00
ColorLog("[INFO] Creating 'migrations' table...\n")
2016-07-22 23:05:01 +00:00
if _, err := db.Query(createTableSQL); err != nil {
2014-08-11 09:23:52 +00:00
ColorLog("[ERRO] Could not create migrations table: %s\n", err)
os.Exit(2)
}
}
2014-08-11 09:23:52 +00:00
// checking that migrations table schema are expected
2016-07-22 23:05:01 +00:00
selectTableSQL := selectMigrationsTableSQL(driver)
if rows, err := db.Query(selectTableSQL); err != nil {
2014-08-11 09:23:52 +00:00
ColorLog("[ERRO] Could not show columns of migrations table: %s\n", err)
os.Exit(2)
} else {
for rows.Next() {
var fieldBytes, typeBytes, nullBytes, keyBytes, defaultBytes, extraBytes []byte
if err := rows.Scan(&fieldBytes, &typeBytes, &nullBytes, &keyBytes, &defaultBytes, &extraBytes); err != nil {
ColorLog("[ERRO] Could not read column information: %s\n", err)
os.Exit(2)
}
fieldStr, typeStr, nullStr, keyStr, defaultStr, extraStr :=
string(fieldBytes), string(typeBytes), string(nullBytes), string(keyBytes), string(defaultBytes), string(extraBytes)
if fieldStr == "id_migration" {
if keyStr != "PRI" || extraStr != "auto_increment" {
ColorLog("[ERRO] Column migration.id_migration type mismatch: KEY: %s, EXTRA: %s\n", keyStr, extraStr)
ColorLog("[HINT] Expecting KEY: PRI, EXTRA: auto_increment\n")
os.Exit(2)
}
} else if fieldStr == "name" {
2014-08-11 09:23:52 +00:00
if !strings.HasPrefix(typeStr, "varchar") || nullStr != "YES" {
ColorLog("[ERRO] Column migration.name type mismatch: TYPE: %s, NULL: %s\n", typeStr, nullStr)
2014-08-11 09:23:52 +00:00
ColorLog("[HINT] Expecting TYPE: varchar, NULL: YES\n")
os.Exit(2)
}
} else if fieldStr == "created_at" {
if typeStr != "timestamp" || defaultStr != "CURRENT_TIMESTAMP" {
ColorLog("[ERRO] Column migration.timestamp type mismatch: TYPE: %s, DEFAULT: %s\n", typeStr, defaultStr)
2014-08-11 09:23:52 +00:00
ColorLog("[HINT] Expecting TYPE: timestamp, DEFAULT: CURRENT_TIMESTAMP\n")
os.Exit(2)
}
}
}
}
}
2016-07-22 23:05:01 +00:00
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'"
}
}
2016-07-22 23:05:01 +00:00
func createMigrationsTableSQL(driver string) string {
switch driver {
case "mysql":
2016-07-22 23:05:01 +00:00
return MYSQLMigrationDDL
case "postgres":
2016-07-22 23:05:01 +00:00
return POSTGRESMigrationDDL
default:
2016-07-22 23:05:01 +00:00
return MYSQLMigrationDDL
}
}
2016-07-22 23:05:01 +00:00
func selectMigrationsTableSQL(driver string) string {
switch driver {
case "mysql":
return "DESC migrations"
case "postgres":
2016-03-01 03:08:04 +00:00
return "SELECT * FROM migrations WHERE false ORDER BY id_migration;"
default:
return "DESC migrations"
}
}
2014-08-13 04:35:24 +00:00
// getLatestMigration retrives latest migration with status 'update'
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"
2014-08-12 02:33:25 +00:00
if rows, err := db.Query(sql); err != nil {
ColorLog("[ERRO] Could not retrieve migrations: %s\n", err)
os.Exit(2)
} else {
if rows.Next() {
if err := rows.Scan(&file); err != nil {
2014-08-12 02:33:25 +00:00
ColorLog("[ERRO] Could not read migrations in database: %s\n", err)
os.Exit(2)
}
createdAtStr := file[len(file)-15:]
if t, err := time.Parse("20060102_150405", createdAtStr); err != nil {
2014-08-13 03:53:55 +00:00
ColorLog("[ERRO] Could not parse time: %s\n", err)
os.Exit(2)
} else {
createdAt = t.Unix()
}
2014-08-12 02:33:25 +00:00
} 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)
}
2014-08-13 03:53:55 +00:00
file, createdAt = "", 0
2014-08-12 02:33:25 +00:00
}
}
return
}
2014-08-13 04:35:24 +00:00
// writeMigrationSourceFile create the source file based on MIGRATION_MAIN_TPL
2014-08-13 03:53:55 +00:00
func writeMigrationSourceFile(dir, source, driver, connStr string, latestTime int64, latestName string, task string) {
changeDir(dir)
2014-08-13 03:53:55 +00:00
if f, err := os.OpenFile(source, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err != nil {
2014-08-11 09:23:52 +00:00
ColorLog("[ERRO] Could not create file: %s\n", err)
os.Exit(2)
} else {
2016-07-22 23:05:01 +00:00
content := strings.Replace(MigrationMainTPL, "{{DBDriver}}", driver, -1)
2014-08-11 09:23:52 +00:00
content = strings.Replace(content, "{{ConnStr}}", connStr, -1)
2014-08-13 03:53:55 +00:00
content = strings.Replace(content, "{{LatestTime}}", strconv.FormatInt(latestTime, 10), -1)
2014-08-12 02:33:25 +00:00
content = strings.Replace(content, "{{LatestName}}", latestName, -1)
content = strings.Replace(content, "{{Task}}", task, -1)
2014-08-11 09:23:52 +00:00
if _, err := f.WriteString(content); err != nil {
ColorLog("[ERRO] Could not write to file: %s\n", err)
os.Exit(2)
}
f.Close()
}
}
2014-08-13 04:35:24 +00:00
// buildMigrationBinary changes directory to database/migrations folder and go-build the source
2014-08-13 03:53:55 +00:00
func buildMigrationBinary(dir, binary string) {
changeDir(dir)
2014-08-13 03:53:55 +00:00
cmd := exec.Command("go", "build", "-o", binary)
2014-08-12 08:20:11 +00:00
if out, err := cmd.CombinedOutput(); err != nil {
2014-08-11 09:23:52 +00:00
ColorLog("[ERRO] Could not build migration binary: %s\n", err)
2014-08-12 10:26:34 +00:00
formatShellErrOutput(string(out))
2014-08-13 09:15:32 +00:00
removeTempFile(dir, binary)
removeTempFile(dir, binary+".go")
2014-08-11 09:23:52 +00:00
os.Exit(2)
}
}
2014-08-13 04:35:24 +00:00
// runMigrationBinary runs the migration program who does the actual work
2014-08-13 03:53:55 +00:00
func runMigrationBinary(dir, binary string) {
changeDir(dir)
2014-08-13 03:53:55 +00:00
cmd := exec.Command("./" + binary)
2014-08-11 09:23:52 +00:00
if out, err := cmd.CombinedOutput(); err != nil {
formatShellOutput(string(out))
ColorLog("[ERRO] Could not run migration binary: %s\n", err)
2014-08-13 09:15:32 +00:00
removeTempFile(dir, binary)
removeTempFile(dir, binary+".go")
2014-08-11 09:23:52 +00:00
os.Exit(2)
} else {
2014-08-12 10:26:34 +00:00
formatShellOutput(string(out))
2014-08-11 09:23:52 +00:00
}
}
// changeDir changes working directory to dir.
// It exits the system when encouter an error
func changeDir(dir string) {
if err := os.Chdir(dir); err != nil {
ColorLog("[ERRO] Could not find migration directory: %s\n", err)
os.Exit(2)
}
}
2014-08-13 04:35:24 +00:00
// removeTempFile removes a file in dir
2014-08-13 03:53:55 +00:00
func removeTempFile(dir, file string) {
changeDir(dir)
2014-08-13 03:53:55 +00:00
if err := os.Remove(file); err != nil {
ColorLog("[WARN] Could not remove temporary file: %s\n", err)
2014-08-11 03:33:53 +00:00
}
}
2014-08-13 04:35:24 +00:00
// formatShellErrOutput formats the error shell output
2014-08-12 10:26:34 +00:00
func formatShellErrOutput(o string) {
for _, line := range strings.Split(o, "\n") {
if line != "" {
ColorLog("[ERRO] -| ")
fmt.Println(line)
2014-08-12 10:26:34 +00:00
}
}
}
2014-08-13 04:35:24 +00:00
// formatShellOutput formats the normal shell output
2014-08-12 10:26:34 +00:00
func formatShellOutput(o string) {
for _, line := range strings.Split(o, "\n") {
if line != "" {
ColorLog("[INFO] -| ")
fmt.Println(line)
2014-08-12 10:26:34 +00:00
}
}
}
2014-08-11 09:23:52 +00:00
const (
2016-07-22 23:05:01 +00:00
MigrationMainTPL = `package main
2014-08-11 09:23:52 +00:00
import(
"os"
2014-08-13 08:44:45 +00:00
2014-08-11 09:23:52 +00:00
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/migration"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
2014-08-11 09:23:52 +00:00
)
func init(){
orm.RegisterDataBase("default", "{{DBDriver}}","{{ConnStr}}")
2014-08-11 09:23:52 +00:00
}
func main(){
task := "{{Task}}"
2014-08-12 02:33:25 +00:00
switch task {
case "upgrade":
if err := migration.Upgrade({{LatestTime}}); err != nil {
os.Exit(2)
}
2014-08-12 02:33:25 +00:00
case "rollback":
if err := migration.Rollback("{{LatestName}}"); err != nil {
os.Exit(2)
}
2014-08-12 02:33:25 +00:00
case "reset":
if err := migration.Reset(); err != nil {
os.Exit(2)
}
2014-08-12 02:33:25 +00:00
case "refresh":
if err := migration.Refresh(); err != nil {
os.Exit(2)
}
2014-08-12 02:33:25 +00:00
}
2014-08-11 09:23:52 +00:00
}
`
2016-07-22 23:05:01 +00:00
MYSQLMigrationDDL = `
2014-08-11 09:23:52 +00:00
CREATE TABLE migrations (
id_migration int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'surrogate key',
name varchar(255) DEFAULT NULL COMMENT 'migration name, unique',
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'date migrated or rolled back',
statements longtext COMMENT 'SQL statements for this migration',
rollback_statements longtext COMMENT 'SQL statment for rolling back migration',
status ENUM('update', 'rollback') COMMENT 'update indicates it is a normal migration while rollback means this migration is rolled back',
2014-08-13 08:44:45 +00:00
PRIMARY KEY (id_migration)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
2014-08-11 09:23:52 +00:00
`
2016-07-22 23:05:01 +00:00
POSTGRESMigrationDDL = `
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
)`
2014-08-11 09:23:52 +00:00
)