explain dirty version error

This commit is contained in:
Matthias Kadenbach 2017-02-19 15:51:12 -08:00
parent 4bb46a98c6
commit e64b7cc233
No known key found for this signature in database
GPG Key ID: DC1F4DC6D31A7031
2 changed files with 19 additions and 12 deletions

View File

@ -29,7 +29,6 @@ var (
ErrNilVersion = fmt.Errorf("no migration") ErrNilVersion = fmt.Errorf("no migration")
ErrLocked = fmt.Errorf("database locked") ErrLocked = fmt.Errorf("database locked")
ErrLockTimeout = fmt.Errorf("timeout: can't acquire database lock") ErrLockTimeout = fmt.Errorf("timeout: can't acquire database lock")
ErrDirty = fmt.Errorf("dirty database")
) )
// ErrShortLimit is an error returned when not enough migrations // ErrShortLimit is an error returned when not enough migrations
@ -43,6 +42,14 @@ func (e ErrShortLimit) Error() string {
return fmt.Sprintf("limit %v short", e.Short) return fmt.Sprintf("limit %v short", e.Short)
} }
type ErrDirty struct {
Version int
}
func (e ErrDirty) Error() string {
return fmt.Sprintf("Dirty database version %v. Fix and force version.", e.Version)
}
type Migrate struct { type Migrate struct {
sourceName string sourceName string
sourceDrv source.Driver sourceDrv source.Driver
@ -210,7 +217,7 @@ func (m *Migrate) Migrate(version uint) error {
} }
if dirty { if dirty {
return m.unlockErr(ErrDirty) return m.unlockErr(ErrDirty{curVersion})
} }
ret := make(chan interface{}, m.PrefetchMigrations) ret := make(chan interface{}, m.PrefetchMigrations)
@ -236,7 +243,7 @@ func (m *Migrate) Steps(n int) error {
} }
if dirty { if dirty {
return m.unlockErr(ErrDirty) return m.unlockErr(ErrDirty{curVersion})
} }
ret := make(chan interface{}, m.PrefetchMigrations) ret := make(chan interface{}, m.PrefetchMigrations)
@ -263,7 +270,7 @@ func (m *Migrate) Up() error {
} }
if dirty { if dirty {
return m.unlockErr(ErrDirty) return m.unlockErr(ErrDirty{curVersion})
} }
ret := make(chan interface{}, m.PrefetchMigrations) ret := make(chan interface{}, m.PrefetchMigrations)
@ -285,7 +292,7 @@ func (m *Migrate) Down() error {
} }
if dirty { if dirty {
return m.unlockErr(ErrDirty) return m.unlockErr(ErrDirty{curVersion})
} }
ret := make(chan interface{}, m.PrefetchMigrations) ret := make(chan interface{}, m.PrefetchMigrations)
@ -317,13 +324,13 @@ func (m *Migrate) Run(migration ...*Migration) error {
return err return err
} }
_, dirty, err := m.databaseDrv.Version() curVersion, dirty, err := m.databaseDrv.Version()
if err != nil { if err != nil {
return m.unlockErr(err) return m.unlockErr(err)
} }
if dirty { if dirty {
return m.unlockErr(ErrDirty) return m.unlockErr(ErrDirty{curVersion})
} }
ret := make(chan interface{}, m.PrefetchMigrations) ret := make(chan interface{}, m.PrefetchMigrations)

View File

@ -289,7 +289,7 @@ func TestMigrateDirty(t *testing.T) {
} }
err := m.Migrate(1) err := m.Migrate(1)
if err != ErrDirty { if _, ok := err.(ErrDirty); !ok {
t.Fatalf("expected ErrDirty, got %v", err) t.Fatalf("expected ErrDirty, got %v", err)
} }
} }
@ -370,7 +370,7 @@ func TestStepsDirty(t *testing.T) {
} }
err := m.Steps(1) err := m.Steps(1)
if err != ErrDirty { if _, ok := err.(ErrDirty); !ok {
t.Fatalf("expected ErrDirty, got %v", err) t.Fatalf("expected ErrDirty, got %v", err)
} }
} }
@ -420,7 +420,7 @@ func TestUpDirty(t *testing.T) {
} }
err := m.Up() err := m.Up()
if err != ErrDirty { if _, ok := err.(ErrDirty); !ok {
t.Fatalf("expected ErrDirty, got %v", err) t.Fatalf("expected ErrDirty, got %v", err)
} }
} }
@ -433,7 +433,7 @@ func TestDownDirty(t *testing.T) {
} }
err := m.Down() err := m.Down()
if err != ErrDirty { if _, ok := err.(ErrDirty); !ok {
t.Fatalf("expected ErrDirty, got %v", err) t.Fatalf("expected ErrDirty, got %v", err)
} }
} }
@ -514,7 +514,7 @@ func TestRunDirty(t *testing.T) {
} }
err = m.Run(migr) err = m.Run(migr)
if err != ErrDirty { if _, ok := err.(ErrDirty); !ok {
t.Fatalf("expected ErrDirty, got %v", err) t.Fatalf("expected ErrDirty, got %v", err)
} }
} }