Skip to content

Commit

Permalink
Add refresh command. Add long help messages for each command.
Browse files Browse the repository at this point in the history
  • Loading branch information
alimoli committed May 22, 2019
1 parent b2f9d91 commit c779089
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
3 changes: 1 addition & 2 deletions command/init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package command

import (
"fmt"
"github.com/limoli/dbshift/lib"
"github.com/limoli/dbshift/util"
)
Expand Down Expand Up @@ -36,7 +35,7 @@ func Init(db lib.IDb) {

importedMigrations, err := db.ImportMigrations(iMigrations)
if err != nil {
fmt.Println(err)
util.Exit(lib.NewError(err.Error()))
} else {
lib.Success("%d migrations have been imported", importedMigrations/2)
}
Expand Down
28 changes: 28 additions & 0 deletions command/refresh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package command

import (
"github.com/limoli/dbshift/lib"
"github.com/limoli/dbshift/util"
)

// Refresh is the command to discover and import new migrations
func Refresh(db lib.IDb) {
migrations, err := lib.ReadMigrationsFromLockFile()
if err != nil {
util.Exit(err)
}

lib.Success("%d migrations have been detected in lock file", len(migrations)/2)

iMigrations := make([]lib.IMigration, 0)
for k := range migrations {
iMigrations = append(iMigrations, &migrations[k])
}

importedMigrations, err := db.ImportMigrations(iMigrations)
if err != nil {
util.Exit(lib.NewError(err.Error()))
} else {
lib.Success("%d migrations have been imported", importedMigrations/2)
}
}
53 changes: 36 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func main() {
}

if len(os.Args) > 1 {
shell.Process(os.Args[1:]...)
err = shell.Process(os.Args[1:]...)
if err != nil {
util.Exit(err)
}
} else {
shell.Run()
}
Expand All @@ -57,29 +60,41 @@ func getDatabase(dbType lib.DatabaseType) (lib.IDb, error) {
func getShellCommands(db lib.IDb) []*ishell.Cmd {
return []*ishell.Cmd{
{
Name: "init",
Help: "init",
Name: "init",
Help: "init",
LongHelp: "Install dbshift using your database.",
Func: func(c *ishell.Context) {
util.ExitIfInitialised(db)
command.Init(db)
},
}, {
Name: "info",
Help: "info",
Name: "refresh",
Help: "refresh",
LongHelp: "It discovers and imports new migrations.",
Func: func(c *ishell.Context) {
util.ExitIfNotInitialised(db)
command.Refresh(db)
},
}, {
Name: "info",
Help: "info",
LongHelp: "It returns information about dbshift.",
Func: func(c *ishell.Context) {
util.ExitIfNotInitialised(db)
command.Info()
},
}, {
Name: "status",
Help: "status",
Name: "status",
Help: "status",
LongHelp: "It returns the current status of database along migrations.",
Func: func(c *ishell.Context) {
util.ExitIfNotInitialised(db)
command.Status(db)
},
}, {
Name: "create",
Help: "create <description>",
Name: "create",
Help: "create <description>",
LongHelp: "It creates a migration with description.",
Func: func(c *ishell.Context) {
util.ExitIfNotInitialised(db)
if len(c.Args) != 1 {
Expand All @@ -89,29 +104,33 @@ func getShellCommands(db lib.IDb) []*ishell.Cmd {
command.Create(db, description)
},
}, {
Name: "migrations-upgrade",
Help: "migrations-upgrade",
Name: "migrations-upgrade",
Help: "migrations-upgrade",
LongHelp: "It returns the list of migrations eligible to upgrade.",
Func: func(c *ishell.Context) {
util.ExitIfNotInitialised(db)
command.Migrations(db, lib.Upgrade)
},
}, {
Name: "migrations-downgrade",
Help: "migrations-downgrade",
Name: "migrations-downgrade",
Help: "migrations-downgrade",
LongHelp: "It returns the list of migrations eligible to downgrade.",
Func: func(c *ishell.Context) {
util.ExitIfNotInitialised(db)
command.Migrations(db, lib.Downgrade)
},
}, {
Name: "upgrade",
Help: "upgrade",
Name: "upgrade",
Help: "upgrade",
LongHelp: "It upgrades all the migrations eligible to upgrade.",
Func: func(c *ishell.Context) {
util.ExitIfNotInitialised(db)
command.Execute(db, lib.Upgrade)
},
}, {
Name: "downgrade",
Help: "downgrade",
Name: "downgrade",
Help: "downgrade",
LongHelp: "It downgrades all the migrations eligible to downgrade.",
Func: func(c *ishell.Context) {
util.ExitIfNotInitialised(db)
command.Execute(db, lib.Downgrade)
Expand Down

0 comments on commit c779089

Please sign in to comment.