Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions internal/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,27 @@ func nextSeqVersion(matches []string, seqDigits int) (string, error) {

nextSeq := uint64(1)

if len(matches) > 0 {
filename := matches[len(matches)-1]
// filepath.Glob returns matches in lexical order, so the highest sequence
// number is not necessarily last (e.g. "9_" sorts after "10_"). Scan every
// match and use the numeric maximum.
for _, filename := range matches {
matchSeqStr := filepath.Base(filename)
idx := strings.Index(matchSeqStr, "_")

if idx < 1 { // Using 1 instead of 0 since there should be at least 1 digit
return "", fmt.Errorf("malformed migration filename: %s", filename)
}

var err error
matchSeqStr = matchSeqStr[0:idx]
nextSeq, err = strconv.ParseUint(matchSeqStr, 10, 64)
matchSeq, err := strconv.ParseUint(matchSeqStr, 10, 64)

if err != nil {
return "", err
}

nextSeq++
if matchSeq >= nextSeq {
nextSeq = matchSeq + 1
}
}

version := fmt.Sprintf("%0[2]*[1]d", nextSeq, seqDigits)
Expand Down
1 change: 1 addition & 0 deletions internal/cli/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (s *CreateCmdSuite) TestNextSeqVersion() {
{"Single digit no int", []string{"bad_bad"}, 1, "", errors.New(`strconv.ParseUint: parsing "bad": invalid syntax`)},
{"Single digit negative seq", []string{"-5_test"}, 1, "", errors.New(`strconv.ParseUint: parsing "-5": invalid syntax`)},
{"Single digit increment", []string{"3_test", "4_test"}, 1, "5", nil},
{"Multi digit lexical order", []string{"0_x", "10_x", "11_x", "1_x", "2_x", "9_x"}, 6, "000012", nil},
{"Single digit overflow", []string{"9_test"}, 1, "", errors.New("next sequence number 10 too large, at most 1 digits are allowed")},
{"Zero-pad initialize", []string{}, 6, "000001", nil},
{"Zero-pad malformed", []string{"bad"}, 6, "", errors.New("malformed migration filename: bad")},
Expand Down
Loading