Skip to content

Commit

Permalink
Add --exit-code and --quiet flags to status command (#124)
Browse files Browse the repository at this point in the history
Extending the `dbmate status` command with the ability to set an exit code or quiet output, for use in scripts.

Flag names were copied from `git diff` command.
  • Loading branch information
amacneil committed Mar 15, 2020
1 parent 256f92a commit 9d2ec36
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ dbmate drop # drop the database
dbmate migrate # run any pending migrations
dbmate rollback # roll back the most recent migration
dbmate down # alias for rollback
dbmate status # show the status of all migrations
dbmate status # show the status of all migrations (supports --exit-code and --quiet)
dbmate dump # write the database schema.sql file
dbmate wait # wait for the database server to become available
```
Expand Down
30 changes: 28 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ func main() {

app := NewApp()
err := app.Run(os.Args)

if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
os.Exit(2)
}
}

Expand Down Expand Up @@ -105,8 +106,33 @@ func NewApp() *cli.App {
{
Name: "status",
Usage: "List applied and pending migrations",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "exit-code",
Usage: "return 1 if there are pending migrations",
},
cli.BoolFlag{
Name: "quiet",
Usage: "don't output any text (implies --exit-code)",
},
},
Action: action(func(db *dbmate.DB, c *cli.Context) error {
return db.Status()
setExitCode := c.Bool("exit-code")
quiet := c.Bool("quiet")
if quiet {
setExitCode = true
}

pending, err := db.Status(quiet)
if err != nil {
return err
}

if pending > 0 && setExitCode {
return cli.NewExitError("", 1)
}

return nil
}),
},
{
Expand Down
23 changes: 15 additions & 8 deletions pkg/dbmate/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,26 +492,33 @@ func checkMigrationsStatus(db *DB) ([]statusResult, error) {
}

// Status shows the status of all migrations
func (db *DB) Status() error {
func (db *DB) Status(quiet bool) (int, error) {
results, err := checkMigrationsStatus(db)
if err != nil {
return err
return -1, err
}

var totalApplied int
var line string

for _, res := range results {
if res.applied {
fmt.Println("[X]", res.filename)
line = fmt.Sprintf("[X] %s", res.filename)
totalApplied++
} else {
fmt.Println("[ ]", res.filename)
line = fmt.Sprintf("[ ] %s", res.filename)
}
if !quiet {
fmt.Println(line)
}
}

fmt.Println()
fmt.Printf("Applied: %d\n", totalApplied)
fmt.Printf("Pending: %d\n", len(results)-totalApplied)
totalPending := len(results) - totalApplied
if !quiet {
fmt.Println()
fmt.Printf("Applied: %d\n", totalApplied)
fmt.Printf("Pending: %d\n", totalPending)
}

return nil
return totalPending, nil
}

0 comments on commit 9d2ec36

Please sign in to comment.