@@ -11,15 +11,7 @@ import (
1111 "github.com/spf13/cobra"
1212)
1313
14- var (
15- UP_MIGRATION_HELP = "apply all or N up migration steps"
16- DOWN_MIGRATION_HELP = "apply all or N down migration steps"
17- VERSION_MIGRATION_HELP = "migrate the database to a specific version specified by the timestamp"
18- )
19-
20- const FLAG_NOT_SET = ""
21-
22- func NewMigrateApplyCmd (ec * cli.ExecutionContext ) * cobra.Command {
14+ func newMigrateApplyCmd (ec * cli.ExecutionContext ) * cobra.Command {
2315 opts := & migrateApplyOptions {
2416 EC : ec ,
2517 }
@@ -28,15 +20,15 @@ func NewMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
2820 Short : "Apply migrations on the database" ,
2921 SilenceUsage : true ,
3022 RunE : func (cmd * cobra.Command , args []string ) error {
31- return opts .Run ()
23+ return opts .run ()
3224 },
3325 }
3426 f := migrateApplyCmd .Flags ()
3527
36- f .StringVar (& opts .upMigration , "up" , FLAG_NOT_SET , UP_MIGRATION_HELP )
37- f .StringVar (& opts .downMigration , "down" , FLAG_NOT_SET , DOWN_MIGRATION_HELP )
38- f .StringVar (& opts .versionMigration , "version" , FLAG_NOT_SET , VERSION_MIGRATION_HELP )
39- f .StringVar (& opts .typeMigration , "type" , FLAG_NOT_SET , VERSION_MIGRATION_HELP )
28+ f .StringVar (& opts .upMigration , "up" , "" , "apply all or N up migration steps" )
29+ f .StringVar (& opts .downMigration , "down" , "" , "apply all or N down migration steps" )
30+ f .StringVar (& opts .versionMigration , "version" , "" , "migrate the database to a specific version" )
31+ f .StringVar (& opts .migrationType , "type" , "up" , "type of migration (up, down) to be used with version flag" )
4032 return migrateApplyCmd
4133}
4234
@@ -46,11 +38,11 @@ type migrateApplyOptions struct {
4638 upMigration string
4739 downMigration string
4840 versionMigration string
49- typeMigration string
41+ migrationType string
5042}
5143
52- func (o * migrateApplyOptions ) Run () error {
53- migrationType , step , err := getMigrationTypeAndStep (o .upMigration , o .downMigration , o .versionMigration , o .typeMigration )
44+ func (o * migrateApplyOptions ) run () error {
45+ migrationType , step , err := getMigrationTypeAndStep (o .upMigration , o .downMigration , o .versionMigration , o .migrationType )
5446 if err != nil {
5547 return errors .Wrap (err , "error validating flags" )
5648 }
@@ -75,24 +67,25 @@ func (o *migrateApplyOptions) Run() error {
7567 return nil
7668}
7769
78- //Only one flag out of up,down and goto can be set at a time. This function checks whether that is the case and returns an error is not
79- func getMigrationTypeAndStep (upMigration , downMigration , versionMigration , typeMigration string ) (string , int64 , error ) {
70+ // Only one flag out of up, down and version can be set at a time. This function
71+ // checks whether that is the case and returns an error is not
72+ func getMigrationTypeAndStep (upMigration , downMigration , versionMigration , migrationType string ) (string , int64 , error ) {
8073 var flagCount = 0
8174 var stepString = "all"
8275 var migrationName = "up"
83- if upMigration != FLAG_NOT_SET {
76+ if upMigration != "" {
8477 stepString = upMigration
8578 flagCount ++
8679 }
87- if downMigration != FLAG_NOT_SET {
80+ if downMigration != "" {
8881 migrationName = "down"
8982 stepString = downMigration
9083 flagCount ++
9184 }
92- if versionMigration != FLAG_NOT_SET {
85+ if versionMigration != "" {
9386 migrationName = "version"
9487 stepString = versionMigration
95- if typeMigration != FLAG_NOT_SET {
88+ if migrationType == "down" {
9689 stepString = "-" + stepString
9790 }
9891 flagCount ++
0 commit comments