GetPreviousDBVersion(): api cleanup, don't return 'earliest' but do return error

callers of this function shouldn't be concerned with the earliest possible DB version, but we do want a way to return an error if we don't find any valid previous versions. this cleans up the 'down' and 'redo' commands.
This commit is contained in:
Liam Staskawicz 2013-09-30 15:20:30 -07:00
parent ae19207fff
commit e5a160a313
3 changed files with 31 additions and 27 deletions

View File

@ -2,7 +2,6 @@ package main
import ( import (
"bitbucket.org/liamstask/goose/lib/goose" "bitbucket.org/liamstask/goose/lib/goose"
"fmt"
"log" "log"
) )
@ -25,17 +24,9 @@ func downRun(cmd *Command, args ...string) {
log.Fatal(err) log.Fatal(err)
} }
if current == 0 { previous, err := goose.GetPreviousDBVersion(conf.MigrationsDir, current)
fmt.Println("db is empty, can't go down.") if err != nil {
return log.Fatal(err)
}
previous, earliest := goose.GetPreviousDBVersion(conf.MigrationsDir, current)
// if we're at the earliest version, indicate that the
// only available step is to roll back to an empty database
if current == earliest {
previous = 0
} }
goose.RunMigrations(conf, conf.MigrationsDir, previous) goose.RunMigrations(conf, conf.MigrationsDir, previous)

View File

@ -18,19 +18,18 @@ func redoRun(cmd *Command, args ...string) {
log.Fatal(err) log.Fatal(err)
} }
target, err := goose.GetDBVersion(conf) current, err := goose.GetDBVersion(conf)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
_, earliest := goose.GetPreviousDBVersion(conf.MigrationsDir, target) previous, err := goose.GetPreviousDBVersion(conf.MigrationsDir, current)
if err != nil {
downRun(cmd, args...) log.Fatal(err)
if target == 0 {
log.Printf("Updating from %s to %s\n", target, earliest)
target = earliest
} }
goose.RunMigrations(conf, conf.MigrationsDir, target)
goose.RunMigrations(conf, conf.MigrationsDir, previous)
goose.RunMigrations(conf, conf.MigrationsDir, current)
} }
func init() { func init() {

View File

@ -15,7 +15,10 @@ import (
"time" "time"
) )
var ErrTableDoesNotExist = errors.New("table does not exist") var (
ErrTableDoesNotExist = errors.New("table does not exist")
ErrNoPreviousVersion = errors.New("no previous version found")
)
type MigrationRecord struct { type MigrationRecord struct {
VersionId int64 VersionId int64
@ -282,20 +285,20 @@ func GetDBVersion(conf *DBConf) (version int64, err error) {
return version, nil return version, nil
} }
func GetPreviousDBVersion(dirpath string, version int64) (previous, earliest int64) { func GetPreviousDBVersion(dirpath string, version int64) (previous int64, err error) {
previous = -1 previous = -1
earliest = (1 << 63) - 1 sawGivenVersion := false
filepath.Walk(dirpath, func(name string, info os.FileInfo, err error) error { filepath.Walk(dirpath, func(name string, info os.FileInfo, walkerr error) error {
if !info.IsDir() { if !info.IsDir() {
if v, e := NumericComponent(name); e == nil { if v, e := NumericComponent(name); e == nil {
if v > previous && v < version { if v > previous && v < version {
previous = v previous = v
} }
if v < earliest { if v == version {
earliest = v sawGivenVersion = true
} }
} }
} }
@ -303,7 +306,18 @@ func GetPreviousDBVersion(dirpath string, version int64) (previous, earliest int
return nil return nil
}) })
return previous, earliest if previous == -1 {
if sawGivenVersion {
// the given version is (likely) valid but we didn't find
// anything before it.
// 'previous' must reflect that no migrations have been applied.
previous = 0
} else {
err = ErrNoPreviousVersion
}
}
return
} }
// helper to identify the most recent possible version // helper to identify the most recent possible version