Add --exit-code and --quiet flags to status command (#124)

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.
This commit is contained in:
Adrian Macneil 2020-03-15 11:32:48 -07:00 committed by GitHub
parent 256f92ad19
commit 9d2ec369d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 11 deletions

View file

@ -93,7 +93,7 @@ dbmate drop # drop the database
dbmate migrate # run any pending migrations dbmate migrate # run any pending migrations
dbmate rollback # roll back the most recent migration dbmate rollback # roll back the most recent migration
dbmate down # alias for rollback 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 dump # write the database schema.sql file
dbmate wait # wait for the database server to become available dbmate wait # wait for the database server to become available
``` ```

30
main.go
View file

@ -17,9 +17,10 @@ func main() {
app := NewApp() app := NewApp()
err := app.Run(os.Args) err := app.Run(os.Args)
if err != nil { if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err) _, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1) os.Exit(2)
} }
} }
@ -105,8 +106,33 @@ func NewApp() *cli.App {
{ {
Name: "status", Name: "status",
Usage: "List applied and pending migrations", 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 { 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
}), }),
}, },
{ {

View file

@ -492,26 +492,33 @@ func checkMigrationsStatus(db *DB) ([]statusResult, error) {
} }
// Status shows the status of all migrations // Status shows the status of all migrations
func (db *DB) Status() error { func (db *DB) Status(quiet bool) (int, error) {
results, err := checkMigrationsStatus(db) results, err := checkMigrationsStatus(db)
if err != nil { if err != nil {
return err return -1, err
} }
var totalApplied int var totalApplied int
var line string
for _, res := range results { for _, res := range results {
if res.applied { if res.applied {
fmt.Println("[X]", res.filename) line = fmt.Sprintf("[X] %s", res.filename)
totalApplied++ totalApplied++
} else { } else {
fmt.Println("[ ]", res.filename) line = fmt.Sprintf("[ ] %s", res.filename)
}
if !quiet {
fmt.Println(line)
} }
} }
totalPending := len(results) - totalApplied
if !quiet {
fmt.Println() fmt.Println()
fmt.Printf("Applied: %d\n", totalApplied) fmt.Printf("Applied: %d\n", totalApplied)
fmt.Printf("Pending: %d\n", len(results)-totalApplied) fmt.Printf("Pending: %d\n", totalPending)
}
return nil
return totalPending, nil
} }