Skip to content

Commit 4abca7c

Browse files
committed
Modify create command to parse filename of assigned directory to set version properly for timestamp
1 parent 6aa798c commit 4abca7c

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

core/service.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,18 @@ func (s Service) walker(indexPath map[uint64]*Migration) func(string, os.FileInf
4444
}
4545

4646
name := info.Name()
47-
if s.direction != direction.Get(name) {
48-
return nil
47+
d := direction.Get(name)
48+
49+
if s.direction == direction.Unknown {
50+
// Collect filename of up in case of unknown direction.
51+
if d != direction.Up {
52+
return nil
53+
}
54+
} else {
55+
// Collect filename of the same direction.
56+
if s.direction != d {
57+
return nil
58+
}
4959
}
5060

5161
fullname := filepath.Clean(filepath.Join(wd, path))
@@ -284,6 +294,8 @@ func (s *Service) NextMigration(name string) (up *Migration, down *Migration, er
284294
v := latest.version + 1
285295
up.version, down.version = v, v
286296
}
297+
_, file := filepath.Split(latest.name)
298+
verFormat = version.Format(file)
287299
}
288300

289301
// [ver]_[name]_[direction-suffix][.ext]

internal/version/version_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ func TestFormat(t *testing.T) {
5151
{value: "", expected: "", message: ""},
5252
{value: "foo", expected: "", message: ""},
5353
{value: "foo_bar", expected: "", message: ""},
54+
{value: "20200101150405_foo", expected: "%d", message: ""},
55+
{value: "001_20200101150405_foo", expected: "%03d", message: ""},
56+
{value: "999_20200101150405_foo", expected: "%d", message: ""},
5457
}
5558

5659
for _, c := range candidates {

kamimai.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
// Version represents kamimai's semantic version.
10-
const Version = "v0.4.1"
10+
const Version = "v0.4.2"
1111

1212
// Current returns the current migration version.
1313
func Current(c *core.Config) (uint64, error) {

0 commit comments

Comments
 (0)