diff --git a/core/service.go b/core/service.go index 73b54cf..ac06522 100644 --- a/core/service.go +++ b/core/service.go @@ -44,8 +44,18 @@ func (s Service) walker(indexPath map[uint64]*Migration) func(string, os.FileInf } name := info.Name() - if s.direction != direction.Get(name) { - return nil + d := direction.Get(name) + + if s.direction == direction.Unknown { + // Collect filename of up in case of unknown direction. + if d != direction.Up { + return nil + } + } else { + // Collect filename of the same direction. + if s.direction != d { + return nil + } } fullname := filepath.Clean(filepath.Join(wd, path)) @@ -284,6 +294,8 @@ func (s *Service) NextMigration(name string) (up *Migration, down *Migration, er v := latest.version + 1 up.version, down.version = v, v } + _, file := filepath.Split(latest.name) + verFormat = version.Format(file) } // [ver]_[name]_[direction-suffix][.ext] diff --git a/internal/version/version_test.go b/internal/version/version_test.go index f99cd00..b2a53f4 100644 --- a/internal/version/version_test.go +++ b/internal/version/version_test.go @@ -51,6 +51,9 @@ func TestFormat(t *testing.T) { {value: "", expected: "", message: ""}, {value: "foo", expected: "", message: ""}, {value: "foo_bar", expected: "", message: ""}, + {value: "20200101150405_foo", expected: "%d", message: ""}, + {value: "001_20200101150405_foo", expected: "%03d", message: ""}, + {value: "999_20200101150405_foo", expected: "%d", message: ""}, } for _, c := range candidates { diff --git a/kamimai.go b/kamimai.go index 6cea8d4..fb8471b 100644 --- a/kamimai.go +++ b/kamimai.go @@ -7,7 +7,7 @@ import ( ) // Version represents kamimai's semantic version. -const Version = "v0.4.1" +const Version = "v0.4.2" // Current returns the current migration version. func Current(c *core.Config) (uint64, error) {