better error messages for duplicate migration versions (close #1148) (#1157)

This commit is contained in:
Aravind Shankar 2018-12-04 10:03:03 +05:30 committed by Shahidh K Muhammed
parent d0effffbf6
commit 56dbc59822
2 changed files with 9 additions and 7 deletions

View File

@ -79,8 +79,9 @@ func (f *File) Open(url string, logger *log.Logger) (source.Driver, error) {
continue // ignore files that we can't parse
}
if !nf.migrations.Append(m) {
return nil, fmt.Errorf("unable to parse file %v", fi.Name())
err = nf.migrations.Append(m)
if err != nil {
return nil, err
}
}
}

View File

@ -1,6 +1,7 @@
package source
import (
"fmt"
"sort"
)
@ -47,9 +48,9 @@ func NewMigrations() *Migrations {
}
}
func (i *Migrations) Append(m *Migration) (ok bool) {
func (i *Migrations) Append(m *Migration) (err error) {
if m == nil {
return false
return fmt.Errorf("migration cannot be nill")
}
if i.migrations[m.Version] == nil {
@ -57,13 +58,13 @@ func (i *Migrations) Append(m *Migration) (ok bool) {
}
// reject duplicate versions
if _, dup := i.migrations[m.Version][m.Direction]; dup {
return false
if migration, dup := i.migrations[m.Version][m.Direction]; dup {
return fmt.Errorf("found duplicate migrations for version %d\n- %s\n- %s", m.Version, m.Raw, migration.Raw)
}
i.migrations[m.Version][m.Direction] = m
i.buildIndex()
return true
return nil
}
func (i *Migrations) buildIndex() {