From 949df006a99d7314c660595aeaed190764899d27 Mon Sep 17 00:00:00 2001 From: Liam Staskawicz Date: Fri, 4 Jan 2013 18:29:12 -1000 Subject: [PATCH] status: add status command to show a summary of applied and pending migrations --- cmd_status.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + 2 files changed, 75 insertions(+) create mode 100644 cmd_status.go diff --git a/cmd_status.go b/cmd_status.go new file mode 100644 index 0000000..0bbf752 --- /dev/null +++ b/cmd_status.go @@ -0,0 +1,74 @@ +package main + +import ( + "database/sql" + "fmt" + "log" + "path" + "time" +) + +var statusCmd = &Command{ + Name: "status", + Usage: "", + Summary: "dump the migration status for the current DB", + Help: `status extended help here...`, +} + +type StatusData struct { + Source string + Status string +} + +func statusRun(cmd *Command, args ...string) { + + conf, err := MakeDBConf() + if err != nil { + log.Fatal(err) + } + + // collect all migrations + min := 0 + max := (1 << 31) - 1 + mm, e := collectMigrations(conf.MigrationsDir, min, max) + if e != nil { + log.Fatal(e) + } + + db, e := sql.Open(conf.Driver, conf.OpenStr) + if e != nil { + log.Fatal("couldn't open DB:", e) + } + defer db.Close() + + fmt.Printf("goose: status for environment '%v'\n", conf.Env) + fmt.Println(" Applied At Migration") + fmt.Println(" =======================================") + for _, v := range mm.Versions { + printMigrationStatus(db, v, path.Base(mm.Migrations[v].Source)) + } +} + +func printMigrationStatus(db *sql.DB, version int, script string) { + var row DBVersion + q := fmt.Sprintf("SELECT tstamp, is_applied FROM goose_db_version WHERE version_id=%d ORDER BY tstamp DESC LIMIT 1", version) + e := db.QueryRow(q).Scan(&row.TStamp, &row.IsApplied) + + if e != nil && e != sql.ErrNoRows { + log.Fatal(e) + } + + var appliedAt string + + if row.IsApplied { + appliedAt = row.TStamp.Format(time.ANSIC) + } else { + appliedAt = "Pending" + } + + fmt.Printf(" %-24s -- %v\n", appliedAt, script) +} + +func init() { + statusCmd.Run = statusRun +} diff --git a/main.go b/main.go index 1e6c545..9e72e7b 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ var commands = []*Command{ upCmd, downCmd, pendingCmd, + statusCmd, } func main() {