2017-03-12 23:18:38 +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.
|
2017-03-06 23:58:53 +00:00
|
|
|
package migrate
|
2014-08-11 03:33:53 +00:00
|
|
|
|
2014-08-11 09:23:52 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
2016-11-13 14:14:48 +00:00
|
|
|
"runtime"
|
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"
|
2017-03-06 23:58:53 +00:00
|
|
|
|
2020-12-16 05:20:41 +00:00
|
|
|
"github.com/beego/bee/v2/cmd/commands"
|
|
|
|
"github.com/beego/bee/v2/cmd/commands/version"
|
|
|
|
"github.com/beego/bee/v2/config"
|
|
|
|
"github.com/beego/bee/v2/utils"
|
2017-03-06 23:58:53 +00:00
|
|
|
|
2020-12-16 05:20:41 +00:00
|
|
|
beeLogger "github.com/beego/bee/v2/logger"
|
2014-08-11 09:23:52 +00:00
|
|
|
)
|
2014-08-11 03:33:53 +00:00
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
var CmdMigrate = &commands.Command{
|
2014-08-11 03:33:53 +00:00
|
|
|
UsageLine: "migrate [Command]",
|
2016-12-03 10:54:41 +00:00
|
|
|
Short: "Runs database migrations",
|
|
|
|
Long: `The command 'migrate' allows you to run database migrations to keep it up-to-date.
|
|
|
|
|
|
|
|
▶ {{"To run all the migrations:"|bold}}
|
|
|
|
|
2019-01-15 17:37:20 +00:00
|
|
|
$ bee migrate [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-dir="path/to/migration"]
|
2016-12-03 10:54:41 +00:00
|
|
|
|
|
|
|
▶ {{"To rollback the last migration:"|bold}}
|
|
|
|
|
2019-01-15 17:37:20 +00:00
|
|
|
$ bee migrate rollback [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-dir="path/to/migration"]
|
2016-12-03 10:54:41 +00:00
|
|
|
|
|
|
|
▶ {{"To do a reset, which will rollback all the migrations:"|bold}}
|
|
|
|
|
2019-01-15 17:37:20 +00:00
|
|
|
$ bee migrate reset [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-dir="path/to/migration"]
|
2016-12-03 10:54:41 +00:00
|
|
|
|
|
|
|
▶ {{"To update your schema:"|bold}}
|
|
|
|
|
2019-01-15 17:37:20 +00:00
|
|
|
$ bee migrate refresh [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-dir="path/to/migration"]
|
2014-08-11 03:33:53 +00:00
|
|
|
`,
|
2017-03-15 14:18:33 +00:00
|
|
|
PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
|
|
|
|
Run: RunMigration,
|
2014-08-11 03:33:53 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
var mDriver utils.DocValue
|
|
|
|
var mConn utils.DocValue
|
2018-11-12 12:09:26 +00:00
|
|
|
var mDir utils.DocValue
|
2014-08-12 07:45:37 +00:00
|
|
|
|
2014-08-11 03:33:53 +00:00
|
|
|
func init() {
|
2017-03-06 23:58:53 +00:00
|
|
|
CmdMigrate.Flag.Var(&mDriver, "driver", "Database driver. Either mysql, postgres or sqlite.")
|
|
|
|
CmdMigrate.Flag.Var(&mConn, "conn", "Connection string used by the driver to connect to a database instance.")
|
2018-11-12 12:09:26 +00:00
|
|
|
CmdMigrate.Flag.Var(&mDir, "dir", "The directory where the migration files are stored")
|
2017-03-06 23:58:53 +00:00
|
|
|
commands.AvailableCommands = append(commands.AvailableCommands, CmdMigrate)
|
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
|
2017-03-06 23:58:53 +00:00
|
|
|
func RunMigration(cmd *commands.Command, args []string) int {
|
2016-08-01 09:42:16 +00:00
|
|
|
currpath, _ := os.Getwd()
|
2014-08-13 03:53:55 +00:00
|
|
|
|
2016-11-13 14:14:48 +00:00
|
|
|
// Getting command line arguments
|
2014-08-12 07:45:37 +00:00
|
|
|
if len(args) != 0 {
|
|
|
|
cmd.Flag.Parse(args[1:])
|
|
|
|
}
|
|
|
|
if mDriver == "" {
|
2017-03-06 23:58:53 +00:00
|
|
|
mDriver = utils.DocValue(config.Conf.Database.Driver)
|
2014-08-13 06:34:12 +00:00
|
|
|
if mDriver == "" {
|
|
|
|
mDriver = "mysql"
|
|
|
|
}
|
2014-08-12 07:45:37 +00:00
|
|
|
}
|
|
|
|
if mConn == "" {
|
2017-03-06 23:58:53 +00:00
|
|
|
mConn = utils.DocValue(config.Conf.Database.Conn)
|
2014-08-13 06:34:12 +00:00
|
|
|
if mConn == "" {
|
|
|
|
mConn = "root:@tcp(127.0.0.1:3306)/test"
|
|
|
|
}
|
2014-08-12 07:45:37 +00:00
|
|
|
}
|
2018-11-12 12:09:26 +00:00
|
|
|
if mDir == "" {
|
|
|
|
mDir = utils.DocValue(config.Conf.Database.Dir)
|
2018-11-12 12:58:15 +00:00
|
|
|
if mDir == "" {
|
|
|
|
mDir = utils.DocValue(path.Join(currpath, "database", "migrations"))
|
|
|
|
}
|
2018-11-12 12:09:26 +00:00
|
|
|
}
|
2018-11-12 12:58:15 +00:00
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Infof("Using '%s' as 'driver'", mDriver)
|
2019-03-08 06:14:52 +00:00
|
|
|
//Log sensitive connection information only when DEBUG is set to true.
|
|
|
|
beeLogger.Log.Debugf("Conn: %s", utils.FILE(), utils.LINE(), mConn)
|
2018-11-12 12:09:26 +00:00
|
|
|
beeLogger.Log.Infof("Using '%s' as 'dir'", mDir)
|
|
|
|
driverStr, connStr, dirStr := string(mDriver), string(mConn), string(mDir)
|
2018-11-12 12:58:15 +00:00
|
|
|
|
|
|
|
dirRune := []rune(dirStr)
|
|
|
|
|
|
|
|
if dirRune[0] != '/' && dirRune[1] != ':' {
|
|
|
|
dirStr = path.Join(currpath, dirStr)
|
|
|
|
}
|
|
|
|
|
2014-08-11 03:33:53 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
// run all outstanding migrations
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Info("Running all outstanding migrations")
|
2018-11-12 12:09:26 +00:00
|
|
|
MigrateUpdate(currpath, driverStr, connStr, dirStr)
|
2014-08-11 03:33:53 +00:00
|
|
|
} else {
|
|
|
|
mcmd := args[0]
|
|
|
|
switch mcmd {
|
|
|
|
case "rollback":
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Info("Rolling back the last migration operation")
|
2018-11-12 12:09:26 +00:00
|
|
|
MigrateRollback(currpath, driverStr, connStr, dirStr)
|
2014-08-11 03:33:53 +00:00
|
|
|
case "reset":
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Info("Reseting all migrations")
|
2018-11-12 12:09:26 +00:00
|
|
|
MigrateReset(currpath, driverStr, connStr, dirStr)
|
2014-08-11 03:33:53 +00:00
|
|
|
case "refresh":
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Info("Refreshing all migrations")
|
2018-11-12 12:09:26 +00:00
|
|
|
MigrateRefresh(currpath, driverStr, connStr, dirStr)
|
2014-08-11 03:33:53 +00:00
|
|
|
default:
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatal("Command is missing")
|
2014-08-11 03:33:53 +00:00
|
|
|
}
|
2014-08-11 09:23:52 +00:00
|
|
|
}
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Success("Migration successful!")
|
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
|
|
|
// migrate generates source code, build it, and invoke the binary who does the actual migration
|
2018-11-12 12:09:26 +00:00
|
|
|
func migrate(goal, currpath, driver, connStr, dir string) {
|
|
|
|
if dir == "" {
|
|
|
|
dir = path.Join(currpath, "database", "migrations")
|
|
|
|
}
|
2016-09-15 19:40:03 +00:00
|
|
|
postfix := ""
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
postfix = ".exe"
|
|
|
|
}
|
|
|
|
binary := "m" + postfix
|
2014-08-13 03:57:36 +00:00
|
|
|
source := binary + ".go"
|
2016-11-13 14:14:48 +00:00
|
|
|
|
|
|
|
// Connect to database
|
2014-08-13 03:57:36 +00:00
|
|
|
db, err := sql.Open(driver, connStr)
|
|
|
|
if err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Could not connect to database using '%s': %s", connStr, err)
|
2014-08-13 03:57:36 +00:00
|
|
|
}
|
|
|
|
defer db.Close()
|
2016-11-13 14:14:48 +00:00
|
|
|
|
2015-05-21 06:18:04 +00:00
|
|
|
checkForSchemaUpdateTable(db, driver)
|
2014-08-14 05:46:02 +00:00
|
|
|
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.
|
2015-05-21 06:18:04 +00:00
|
|
|
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 {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Could not show migrations table: %s", err)
|
2014-08-11 09:23:52 +00:00
|
|
|
} else if !rows.Next() {
|
2016-11-13 14:14:48 +00:00
|
|
|
// No migrations table, create new ones
|
2016-07-22 23:05:01 +00:00
|
|
|
createTableSQL := createMigrationsTableSQL(driver)
|
2016-11-13 14:14:48 +00:00
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Infof("Creating 'migrations' table...")
|
2016-11-13 14:14:48 +00:00
|
|
|
|
2016-07-22 23:05:01 +00:00
|
|
|
if _, err := db.Query(createTableSQL); err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Could not create migrations table: %s", err)
|
2014-08-11 09:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-21 06:18:04 +00:00
|
|
|
|
2016-11-13 14:14:48 +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 {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Could not show columns of migrations table: %s", err)
|
2014-08-11 09:23:52 +00:00
|
|
|
} else {
|
|
|
|
for rows.Next() {
|
|
|
|
var fieldBytes, typeBytes, nullBytes, keyBytes, defaultBytes, extraBytes []byte
|
|
|
|
if err := rows.Scan(&fieldBytes, &typeBytes, &nullBytes, &keyBytes, &defaultBytes, &extraBytes); err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Could not read column information: %s", err)
|
2014-08-11 09:23:52 +00:00
|
|
|
}
|
|
|
|
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" {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Hint("Expecting KEY: PRI, EXTRA: auto_increment")
|
|
|
|
beeLogger.Log.Fatalf("Column migration.id_migration type mismatch: KEY: %s, EXTRA: %s", keyStr, extraStr)
|
2014-08-11 09:23:52 +00:00
|
|
|
}
|
2014-08-12 07:16:29 +00:00
|
|
|
} else if fieldStr == "name" {
|
2014-08-11 09:23:52 +00:00
|
|
|
if !strings.HasPrefix(typeStr, "varchar") || nullStr != "YES" {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Hint("Expecting TYPE: varchar, NULL: YES")
|
|
|
|
beeLogger.Log.Fatalf("Column migration.name type mismatch: TYPE: %s, NULL: %s", typeStr, nullStr)
|
2014-08-11 09:23:52 +00:00
|
|
|
}
|
|
|
|
} else if fieldStr == "created_at" {
|
2020-06-28 06:51:15 +00:00
|
|
|
if typeStr != "timestamp" || (!strings.EqualFold(defaultStr, "CURRENT_TIMESTAMP") && !strings.EqualFold(defaultStr, "CURRENT_TIMESTAMP()")) {
|
|
|
|
beeLogger.Log.Hint("Expecting TYPE: timestamp, DEFAULT: CURRENT_TIMESTAMP || CURRENT_TIMESTAMP()")
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Column migration.timestamp type mismatch: TYPE: %s, DEFAULT: %s", typeStr, defaultStr)
|
2014-08-11 09:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 06:24:22 +00:00
|
|
|
func driverImportStatement(driver string) string {
|
|
|
|
switch driver {
|
|
|
|
case "mysql":
|
|
|
|
return "github.com/go-sql-driver/mysql"
|
|
|
|
case "postgres":
|
|
|
|
return "github.com/lib/pq"
|
|
|
|
default:
|
|
|
|
return "github.com/go-sql-driver/mysql"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 23:05:01 +00:00
|
|
|
func showMigrationsTableSQL(driver string) string {
|
2015-05-21 06:18:04 +00:00
|
|
|
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 {
|
2015-05-21 06:18:04 +00:00
|
|
|
switch driver {
|
|
|
|
case "mysql":
|
2016-07-22 23:05:01 +00:00
|
|
|
return MYSQLMigrationDDL
|
2015-05-21 06:18:04 +00:00
|
|
|
case "postgres":
|
2016-07-22 23:05:01 +00:00
|
|
|
return POSTGRESMigrationDDL
|
2015-05-21 06:18:04 +00:00
|
|
|
default:
|
2016-07-22 23:05:01 +00:00
|
|
|
return MYSQLMigrationDDL
|
2015-05-21 06:18:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 23:05:01 +00:00
|
|
|
func selectMigrationsTableSQL(driver string) string {
|
2015-05-21 06:18:04 +00:00
|
|
|
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;"
|
2015-05-21 06:18:04 +00:00
|
|
|
default:
|
|
|
|
return "DESC migrations"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 04:35:24 +00:00
|
|
|
// getLatestMigration retrives latest migration with status 'update'
|
2014-08-14 05:46:02 +00:00
|
|
|
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 {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Could not retrieve migrations: %s", err)
|
2014-08-12 02:33:25 +00:00
|
|
|
} else {
|
|
|
|
if rows.Next() {
|
2014-08-14 05:46:02 +00:00
|
|
|
if err := rows.Scan(&file); err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Could not read migrations in database: %s", err)
|
2014-08-12 02:33:25 +00:00
|
|
|
}
|
2014-08-14 05:46:02 +00:00
|
|
|
createdAtStr := file[len(file)-15:]
|
|
|
|
if t, err := time.Parse("20060102_150405", createdAtStr); err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Could not parse time: %s", err)
|
2014-08-13 03:53:55 +00:00
|
|
|
} else {
|
|
|
|
createdAt = t.Unix()
|
|
|
|
}
|
2014-08-12 02:33:25 +00:00
|
|
|
} else {
|
2014-08-14 05:46:02 +00:00
|
|
|
// migration table has no 'update' record, no point rolling back
|
|
|
|
if goal == "rollback" {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatal("There is nothing to rollback")
|
2014-08-14 05:46:02 +00:00
|
|
|
}
|
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) {
|
2014-08-14 03:28:01 +00:00
|
|
|
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 {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Could not create file: %s", err)
|
2014-08-11 09:23:52 +00:00
|
|
|
} else {
|
2016-07-22 23:05:01 +00:00
|
|
|
content := strings.Replace(MigrationMainTPL, "{{DBDriver}}", driver, -1)
|
2017-09-16 06:24:22 +00:00
|
|
|
content = strings.Replace(content, "{{DriverRepo}}", driverImportStatement(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 {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Could not write to file: %s", err)
|
2014-08-11 09:23:52 +00:00
|
|
|
}
|
2017-03-06 23:58:53 +00:00
|
|
|
utils.CloseFile(f)
|
2014-08-11 09:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-08-14 03:28:01 +00:00
|
|
|
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 {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Errorf("Could not build migration binary: %s", 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) {
|
2014-08-14 03:28:01 +00:00
|
|
|
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 {
|
2014-08-13 02:16:55 +00:00
|
|
|
formatShellOutput(string(out))
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Errorf("Could not run migration binary: %s", 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 03:28:01 +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 {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Could not find migration directory: %s", err)
|
2014-08-14 03:28:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-08-14 06:25:02 +00:00
|
|
|
changeDir(dir)
|
2014-08-13 03:53:55 +00:00
|
|
|
if err := os.Remove(file); err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Warnf("Could not remove temporary file: %s", 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 != "" {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Errorf("|> %s", 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 != "" {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Infof("|> %s", line)
|
2014-08-12 10:26:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-11 09:23:52 +00:00
|
|
|
const (
|
2016-11-13 14:14:48 +00:00
|
|
|
// MigrationMainTPL migration main template
|
2016-07-22 23:05:01 +00:00
|
|
|
MigrationMainTPL = `package main
|
2014-08-11 09:23:52 +00:00
|
|
|
|
|
|
|
import(
|
2014-08-13 02:16:55 +00:00
|
|
|
"os"
|
2014-08-13 08:44:45 +00:00
|
|
|
|
2020-12-14 05:08:47 +00:00
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
|
|
"github.com/beego/beego/v2/client/orm/migration"
|
2014-08-12 07:16:29 +00:00
|
|
|
|
2017-09-16 06:24:22 +00:00
|
|
|
_ "{{DriverRepo}}"
|
2014-08-11 09:23:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init(){
|
2014-08-12 07:16:29 +00:00
|
|
|
orm.RegisterDataBase("default", "{{DBDriver}}","{{ConnStr}}")
|
2014-08-11 09:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main(){
|
2014-08-12 07:16:29 +00:00
|
|
|
task := "{{Task}}"
|
2014-08-12 02:33:25 +00:00
|
|
|
switch task {
|
|
|
|
case "upgrade":
|
2014-08-13 02:16:55 +00:00
|
|
|
if err := migration.Upgrade({{LatestTime}}); err != nil {
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2014-08-12 02:33:25 +00:00
|
|
|
case "rollback":
|
2014-08-13 02:16:55 +00:00
|
|
|
if err := migration.Rollback("{{LatestName}}"); err != nil {
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2014-08-12 02:33:25 +00:00
|
|
|
case "reset":
|
2014-08-13 02:16:55 +00:00
|
|
|
if err := migration.Reset(); err != nil {
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2014-08-12 02:33:25 +00:00
|
|
|
case "refresh":
|
2014-08-13 02:16:55 +00:00
|
|
|
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-11-13 14:14:48 +00:00
|
|
|
// MYSQLMigrationDDL MySQL migration SQL
|
2016-07-22 23:05:01 +00:00
|
|
|
MYSQLMigrationDDL = `
|
2014-08-11 09:23:52 +00:00
|
|
|
CREATE TABLE migrations (
|
2014-08-12 07:16:29 +00:00
|
|
|
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',
|
2014-08-12 07:45:37 +00:00
|
|
|
rollback_statements longtext COMMENT 'SQL statment for rolling back migration',
|
2014-08-12 07:16:29 +00:00
|
|
|
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)
|
2014-12-18 16:19:34 +00:00
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8
|
2014-08-11 09:23:52 +00:00
|
|
|
`
|
2016-11-13 14:14:48 +00:00
|
|
|
// POSTGRESMigrationDDL Postgres migration SQL
|
2016-07-22 23:05:01 +00:00
|
|
|
POSTGRESMigrationDDL = `
|
2015-05-21 06:18:04 +00:00
|
|
|
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
|
|
|
)
|
2017-03-06 23:58:53 +00:00
|
|
|
|
|
|
|
// MigrateUpdate does the schema update
|
2018-11-12 12:09:26 +00:00
|
|
|
func MigrateUpdate(currpath, driver, connStr, dir string) {
|
|
|
|
migrate("upgrade", currpath, driver, connStr, dir)
|
2017-03-06 23:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MigrateRollback rolls back the latest migration
|
2018-11-12 12:09:26 +00:00
|
|
|
func MigrateRollback(currpath, driver, connStr, dir string) {
|
|
|
|
migrate("rollback", currpath, driver, connStr, dir)
|
2017-03-06 23:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MigrateReset rolls back all migrations
|
2018-11-12 12:09:26 +00:00
|
|
|
func MigrateReset(currpath, driver, connStr, dir string) {
|
|
|
|
migrate("reset", currpath, driver, connStr, dir)
|
2017-03-06 23:58:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 03:17:56 +00:00
|
|
|
// MigrateRefresh rolls back all migrations and start over again
|
2018-11-12 12:09:26 +00:00
|
|
|
func MigrateRefresh(currpath, driver, connStr, dir string) {
|
|
|
|
migrate("refresh", currpath, driver, connStr, dir)
|
2017-03-06 23:58:53 +00:00
|
|
|
}
|