GetDBVersion(): report error rather than aborting

This commit is contained in:
Liam Staskawicz 2013-09-30 14:44:09 -07:00
parent 8f7107e30a
commit ae19207fff
3 changed files with 15 additions and 7 deletions

View File

@ -20,7 +20,11 @@ func downRun(cmd *Command, args ...string) {
log.Fatal(err) log.Fatal(err)
} }
current := goose.GetDBVersion(conf) current, err := goose.GetDBVersion(conf)
if err != nil {
log.Fatal(err)
}
if current == 0 { if current == 0 {
fmt.Println("db is empty, can't go down.") fmt.Println("db is empty, can't go down.")
return return

View File

@ -18,7 +18,11 @@ func redoRun(cmd *Command, args ...string) {
log.Fatal(err) log.Fatal(err)
} }
target := goose.GetDBVersion(conf) target, err := goose.GetDBVersion(conf)
if err != nil {
log.Fatal(err)
}
_, earliest := goose.GetPreviousDBVersion(conf.MigrationsDir, target) _, earliest := goose.GetPreviousDBVersion(conf.MigrationsDir, target)
downRun(cmd, args...) downRun(cmd, args...)

View File

@ -266,20 +266,20 @@ func createVersionTable(conf *DBConf, db *sql.DB) error {
// wrapper for EnsureDBVersion for callers that don't already have // wrapper for EnsureDBVersion for callers that don't already have
// their own DB instance // their own DB instance
func GetDBVersion(conf *DBConf) int64 { func GetDBVersion(conf *DBConf) (version int64, err error) {
db, err := sql.Open(conf.Driver.Name, conf.Driver.OpenStr) db, err := sql.Open(conf.Driver.Name, conf.Driver.OpenStr)
if err != nil { if err != nil {
log.Fatal("couldn't open DB:", err) return -1, err
} }
defer db.Close() defer db.Close()
version, err := EnsureDBVersion(conf, db) version, err = EnsureDBVersion(conf, db)
if err != nil { if err != nil {
log.Fatalf("couldn't get DB version: %v", err) return -1, err
} }
return version return version, nil
} }
func GetPreviousDBVersion(dirpath string, version int64) (previous, earliest int64) { func GetPreviousDBVersion(dirpath string, version int64) (previous, earliest int64) {