1
0
mirror of https://github.com/beego/bee.git synced 2024-12-25 04:30:50 +00:00

Adding dir parameter to migrate command

This commit is contained in:
Lukas Bachschwell 2018-11-12 13:09:26 +01:00
parent e3e401cadd
commit 3d977edbb4
3 changed files with 25 additions and 16 deletions

View File

@ -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,29 @@ 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)
}
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)
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 +128,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 +423,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))
} }