Skip to content

cli: pick next -seq version by numeric max, not lexical last - #1442

Open
shaikn6 wants to merge 1 commit into
golang-migrate:masterfrom
shaikn6:fix/nextseq-numeric-max
Open

shaikn6 wants to merge 1 commit into
golang-migrate:masterfrom
shaikn6:fix/nextseq-numeric-max

Conversation

@shaikn6

@shaikn6 shaikn6 commented Sep 14, 2026

Copy link
Copy Markdown

What

Make migrate create -seq derive the next version from the numerically highest
existing migration instead of the lexically last filename.

Why / evidence

nextSeqVersion (internal/cli/commands.go) read the sequence prefix from
matches[len(matches)-1]. filepath.Glob returns paths in lexical order, so
once a project has both single- and multi-digit versions the last entry is
9_* (it sorts after 10_*, 11_*, ...). The next version was then computed
as 9 + 1 = 10, colliding with the existing 10_* files.

Reproduced with the scenario from #1109: a migrations dir containing 0_ .. 11_
pairs, then migrate create -dir ./migrations -seq -digits 2 -ext sql foobar
=> error: duplicate migration version: 10.

Change

In nextSeqVersion, iterate over every glob match, parse each numeric prefix,
and keep the maximum; next version is max + 1. Per-file malformed-name and
ParseUint errors are unchanged, so behaviour for well-formed input is
identical. One function, ~9 lines.

Verified

  • Added table case Multi digit lexical order
    ([]string{"0_x","10_x","11_x","1_x","2_x","9_x"} -> 000012). Fails on the
    current code (got 000010), passes with the fix.
  • go test ./internal/cli/ -> ok
  • gofmt -l internal/cli/ clean; go vet ./internal/cli/ clean.

Fixes #1109

nextSeqVersion took the sequence prefix from the last element of the
filepath.Glob result, which is sorted lexically. Once a project has both
single- and multi-digit versions (e.g. 9_ and 10_), '9_...' sorts after
'10_...', so 'migrate create -seq' derived the next version from 9 and
produced a duplicate version error.

Scan every match and keep the numeric maximum instead. Malformed-filename
and parse errors are still reported per element, so behaviour is
unchanged for well-formed inputs.

Fixes golang-migrate#1109
@coveralls

coveralls commented Sep 14, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 54.955%. first build — shaikn6:fix/nextseq-numeric-max into golang-migrate:master

@shaikn6 shaikn6 closed this Sep 19, 2026
@shaikn6 shaikn6 reopened this Sep 19, 2026
@shaikn6

shaikn6 commented Oct 6, 2026

Copy link
Copy Markdown
Author

Hi maintainers, the only failing check on this PR is the Go 1.25.x test job. The failures are Docker containers timing out on startup (sqlserver and yugabytedb: "timed out waiting for container to get ready"). Go 1.26.x, lint and coverage all pass. This change doesn't touch those drivers. Could someone with write access re-run the failed job? Thanks!

@shaikn6 shaikn6 closed this Oct 7, 2026
@shaikn6 shaikn6 reopened this Oct 7, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create -seq cli command produces wrong migration number

3 participants