Skip to content

Commit

Permalink
Adding dir parameter to migrate command
Browse files Browse the repository at this point in the history
  • Loading branch information
s00500 committed Nov 12, 2018
1 parent e3e401c commit 3d977ed
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
38 changes: 23 additions & 15 deletions cmd/commands/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ var CmdMigrate = &commands.Command{

var mDriver utils.DocValue
var mConn utils.DocValue
var mDir utils.DocValue

func init() {
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(&mDir, "dir", "The directory where the migration files are stored")
commands.AvailableCommands = append(commands.AvailableCommands, CmdMigrate)
}

Expand Down Expand Up @@ -94,25 +96,29 @@ func RunMigration(cmd *commands.Command, args []string) int {
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 '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 {
// run all outstanding migrations
beeLogger.Log.Info("Running all outstanding migrations")
MigrateUpdate(currpath, driverStr, connStr)
MigrateUpdate(currpath, driverStr, connStr, dirStr)
} else {
mcmd := args[0]
switch mcmd {
case "rollback":
beeLogger.Log.Info("Rolling back the last migration operation")
MigrateRollback(currpath, driverStr, connStr)
MigrateRollback(currpath, driverStr, connStr, dirStr)
case "reset":
beeLogger.Log.Info("Reseting all migrations")
MigrateReset(currpath, driverStr, connStr)
MigrateReset(currpath, driverStr, connStr, dirStr)
case "refresh":
beeLogger.Log.Info("Refreshing all migrations")
MigrateRefresh(currpath, driverStr, connStr)
MigrateRefresh(currpath, driverStr, connStr, dirStr)
default:
beeLogger.Log.Fatal("Command is missing")
}
Expand All @@ -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
func migrate(goal, currpath, driver, connStr string) {
dir := path.Join(currpath, "database", "migrations")
func migrate(goal, currpath, driver, connStr, dir string) {
if dir == "" {
dir = path.Join(currpath, "database", "migrations")
}
postfix := ""
if runtime.GOOS == "windows" {
postfix = ".exe"
Expand Down Expand Up @@ -415,21 +423,21 @@ CREATE TABLE migrations (
)

// MigrateUpdate does the schema update
func MigrateUpdate(currpath, driver, connStr string) {
migrate("upgrade", currpath, driver, connStr)
func MigrateUpdate(currpath, driver, connStr, dir string) {
migrate("upgrade", currpath, driver, connStr, dir)
}

// MigrateRollback rolls back the latest migration
func MigrateRollback(currpath, driver, connStr string) {
migrate("rollback", currpath, driver, connStr)
func MigrateRollback(currpath, driver, connStr, dir string) {
migrate("rollback", currpath, driver, connStr, dir)
}

// MigrateReset rolls back all migrations
func MigrateReset(currpath, driver, connStr string) {
migrate("reset", currpath, driver, connStr)
func MigrateReset(currpath, driver, connStr, dir string) {
migrate("reset", currpath, driver, connStr, dir)
}

// migrationRefresh rolls back all migrations and start over again
func MigrateRefresh(currpath, driver, connStr string) {
migrate("refresh", currpath, driver, connStr)
func MigrateRefresh(currpath, driver, connStr, dir string) {
migrate("refresh", currpath, driver, connStr, dir)
}
1 change: 1 addition & 0 deletions config/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type bale struct {
type database struct {
Driver string
Conn string
Dir string
}

// LoadConfig loads the bee tool configuration.
Expand Down
2 changes: 1 addition & 1 deletion generate/g_scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func GenerateScaffold(sname, fields, currpath, driver, conn string) {
// Run the migration
beeLogger.Log.Infof("Do you want to migrate the database? [Yes|No] ")
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))
}

0 comments on commit 3d977ed

Please sign in to comment.