Merge pull request #559 from s00500/develop

Adds  "-dir" option to migrate command to set migrations directory
This commit is contained in:
Faissal Elamraoui 2019-01-16 07:47:03 +01:00 committed by GitHub
commit 4b7edb7235
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 20 deletions

View File

@ -38,19 +38,19 @@ var CmdMigrate = &commands.Command{
{{"To run all the migrations:"|bold}} {{"To run all the migrations:"|bold}}
$ bee migrate [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] $ bee migrate [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-dir="path/to/migration"]
{{"To rollback the last migration:"|bold}} {{"To rollback the last migration:"|bold}}
$ bee migrate rollback [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] $ bee migrate rollback [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-dir="path/to/migration"]
{{"To do a reset, which will rollback all the migrations:"|bold}} {{"To do a reset, which will rollback all the migrations:"|bold}}
$ bee migrate reset [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] $ bee migrate reset [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-dir="path/to/migration"]
{{"To update your schema:"|bold}} {{"To update your schema:"|bold}}
$ bee migrate refresh [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] $ bee migrate refresh [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-dir="path/to/migration"]
`, `,
PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() }, PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
Run: RunMigration, Run: RunMigration,
@ -58,10 +58,12 @@ var CmdMigrate = &commands.Command{
var mDriver utils.DocValue var mDriver utils.DocValue
var mConn utils.DocValue var mConn utils.DocValue
var mDir utils.DocValue
func init() { func init() {
CmdMigrate.Flag.Var(&mDriver, "driver", "Database driver. Either mysql, postgres or sqlite.") 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.") CmdMigrate.Flag.Var(&mConn, "conn", "Connection string used by the driver to connect to a database instance.")
CmdMigrate.Flag.Var(&mDir, "dir", "The directory where the migration files are stored")
commands.AvailableCommands = append(commands.AvailableCommands, CmdMigrate) commands.AvailableCommands = append(commands.AvailableCommands, CmdMigrate)
} }
@ -94,25 +96,40 @@ func RunMigration(cmd *commands.Command, args []string) int {
mConn = "root:@tcp(127.0.0.1:3306)/test" mConn = "root:@tcp(127.0.0.1:3306)/test"
} }
} }
if mDir == "" {
mDir = utils.DocValue(config.Conf.Database.Dir)
if mDir == "" {
mDir = utils.DocValue(path.Join(currpath, "database", "migrations"))
}
}
beeLogger.Log.Infof("Using '%s' as 'driver'", mDriver) beeLogger.Log.Infof("Using '%s' as 'driver'", mDriver)
beeLogger.Log.Infof("Using '%s' as 'conn'", mConn) beeLogger.Log.Infof("Using '%s' as 'conn'", mConn)
driverStr, connStr := string(mDriver), string(mConn) beeLogger.Log.Infof("Using '%s' as 'dir'", mDir)
driverStr, connStr, dirStr := string(mDriver), string(mConn), string(mDir)
dirRune := []rune(dirStr)
if dirRune[0] != '/' && dirRune[1] != ':' {
dirStr = path.Join(currpath, dirStr)
}
if len(args) == 0 { if len(args) == 0 {
// run all outstanding migrations // run all outstanding migrations
beeLogger.Log.Info("Running all outstanding migrations") beeLogger.Log.Info("Running all outstanding migrations")
MigrateUpdate(currpath, driverStr, connStr) MigrateUpdate(currpath, driverStr, connStr, dirStr)
} else { } else {
mcmd := args[0] mcmd := args[0]
switch mcmd { switch mcmd {
case "rollback": case "rollback":
beeLogger.Log.Info("Rolling back the last migration operation") beeLogger.Log.Info("Rolling back the last migration operation")
MigrateRollback(currpath, driverStr, connStr) MigrateRollback(currpath, driverStr, connStr, dirStr)
case "reset": case "reset":
beeLogger.Log.Info("Reseting all migrations") beeLogger.Log.Info("Reseting all migrations")
MigrateReset(currpath, driverStr, connStr) MigrateReset(currpath, driverStr, connStr, dirStr)
case "refresh": case "refresh":
beeLogger.Log.Info("Refreshing all migrations") beeLogger.Log.Info("Refreshing all migrations")
MigrateRefresh(currpath, driverStr, connStr) MigrateRefresh(currpath, driverStr, connStr, dirStr)
default: default:
beeLogger.Log.Fatal("Command is missing") beeLogger.Log.Fatal("Command is missing")
} }
@ -122,8 +139,10 @@ func RunMigration(cmd *commands.Command, args []string) int {
} }
// migrate generates source code, build it, and invoke the binary who does the actual migration // migrate generates source code, build it, and invoke the binary who does the actual migration
func migrate(goal, currpath, driver, connStr string) { func migrate(goal, currpath, driver, connStr, dir string) {
dir := path.Join(currpath, "database", "migrations") if dir == "" {
dir = path.Join(currpath, "database", "migrations")
}
postfix := "" postfix := ""
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
postfix = ".exe" postfix = ".exe"
@ -415,21 +434,21 @@ CREATE TABLE migrations (
) )
// MigrateUpdate does the schema update // MigrateUpdate does the schema update
func MigrateUpdate(currpath, driver, connStr string) { func MigrateUpdate(currpath, driver, connStr, dir string) {
migrate("upgrade", currpath, driver, connStr) migrate("upgrade", currpath, driver, connStr, dir)
} }
// MigrateRollback rolls back the latest migration // MigrateRollback rolls back the latest migration
func MigrateRollback(currpath, driver, connStr string) { func MigrateRollback(currpath, driver, connStr, dir string) {
migrate("rollback", currpath, driver, connStr) migrate("rollback", currpath, driver, connStr, dir)
} }
// MigrateReset rolls back all migrations // MigrateReset rolls back all migrations
func MigrateReset(currpath, driver, connStr string) { func MigrateReset(currpath, driver, connStr, dir string) {
migrate("reset", currpath, driver, connStr) migrate("reset", currpath, driver, connStr, dir)
} }
// migrationRefresh rolls back all migrations and start over again // migrationRefresh rolls back all migrations and start over again
func MigrateRefresh(currpath, driver, connStr string) { func MigrateRefresh(currpath, driver, connStr, dir string) {
migrate("refresh", currpath, driver, connStr) migrate("refresh", currpath, driver, connStr, dir)
} }

View File

@ -77,6 +77,7 @@ type bale struct {
type database struct { type database struct {
Driver string Driver string
Conn string Conn string
Dir string
} }
// LoadConfig loads the bee tool configuration. // LoadConfig loads the bee tool configuration.

View File

@ -44,7 +44,7 @@ func GenerateScaffold(sname, fields, currpath, driver, conn string) {
// Run the migration // Run the migration
beeLogger.Log.Infof("Do you want to migrate the database? [Yes|No] ") beeLogger.Log.Infof("Do you want to migrate the database? [Yes|No] ")
if utils.AskForConfirmation() { if utils.AskForConfirmation() {
migrate.MigrateUpdate(currpath, driver, conn) migrate.MigrateUpdate(currpath, driver, conn, "")
} }
beeLogger.Log.Successf("All done! Don't forget to add beego.Router(\"/%s\" ,&controllers.%sController{}) to routers/route.go\n", sname, strings.Title(sname)) beeLogger.Log.Successf("All done! Don't forget to add beego.Router(\"/%s\" ,&controllers.%sController{}) to routers/route.go\n", sname, strings.Title(sname))
} }