Repository navigation
create -seq cli command produces wrong migration number #1109
Description
First of all thanks for making this great library <3.
Describe the Bug
The create cli command produces the wrong migration number.
Command used:
migrate create -dir ./migrations -seq -digits 2 -ext sql foobar
error: duplicate migration version: 10The /migrations folder contains the following files:
0_.down.sql
0_.up.sql
1_.down.sql
1_.up.sql
2_.down.sql
2_.up.sql
3_.down.sql
3_.up.sql
4_.down.sql
4_.up.sql
5_.down.sql
5_.up.sql
6_.down.sql
6_.up.sql
7_.down.sql
7_.up.sql
8_.down.sql
8_.up.sql
9_.down.sql
9_.up.sql
10_.down.sql
10_.up.sql
11_.down.sql
11_.up.sql
Steps to Reproduce
Steps to reproduce the behavior:
- Create an example project with a
/migrationsfolder somewhat like the following:
.
└── migrations
├── 0_.down.sql
├── 0_.up.sql
├── 1_.down.sql
├── 1_.up.sql
├── 2_.down.sql
├── 2_.up.sql
├── 3_.down.sql
├── 3_.up.sql
├── 4_.down.sql
├── 4_.up.sql
├── 5_.down.sql
├── 5_.up.sql
├── 6_.down.sql
├── 6_.up.sql
├── 7_.down.sql
├── 7_.up.sql
├── 8_.down.sql
├── 8_.up.sql
├── 9_.down.sql
├── 9_.up.sql
├── 10_.down.sql
├── 10_.up.sql
├── 11_.down.sql
└── 11_.up.sql
- Run the migrate create cli command with the option to generate sequential up/down migrations with N digits:
migrate create -dir ./migrations -seq -digits 2 -ext sql foobar - See error
~/go/src/migratetest
migrate create -dir ./migrations -seq -digits 2 -ext sql foobar
error: duplicate migration version: 10Expected Behavior
The migrate create cli command does not raise an error, instead it creates the files 12_foobar.up.sql and 12_foobar.down.sql.
Migrate Version
v4.17.1
Loaded Source Drivers
(Should not matter)
bitbucket, godoc-vfs, s3, gcs, file, github, github-ee, gitlab, go-bindata
Loaded Database Drivers
(Should not matter)
Database drivers: pgx4, yugabytedb, firebird, mongodb+srv, stub, cockroach, crdb-postgres, pgx, redshift, yugabyte, cassandra, pgx5, cockroachdb, postgresql, mongodb, sqlserver, spanner, mysql, ysql, firebirdsql, rqlite, clickhouse, postgres, neo4j
Go Version
go version go1.22.2 darwin/arm64
Stacktrace
Additional context
The matches obtained in the create cli Command in line 87:
migrate/internal/cli/commands.go
Line 87 in 2477f63
| matches, err := filepath.Glob(filepath.Join(dir, "*"+ext)) |
results in the slice:
[]string{
"migrations/0_.down.sql",
"migrations/0_.up.sql",
"migrations/10_.down.sql",
"migrations/10_.up.sql",
"migrations/11_.down.sql",
"migrations/11_.up.sql",
"migrations/1_.down.sql",
"migrations/1_.up.sql",
"migrations/2_.down.sql",
"migrations/2_.up.sql",
"migrations/3_.down.sql",
"migrations/3_.up.sql",
"migrations/4_.down.sql",
"migrations/4_.up.sql",
"migrations/5_.down.sql",
"migrations/5_.up.sql",
"migrations/6_.down.sql",
"migrations/6_.up.sql",
"migrations/7_.down.sql",
"migrations/7_.up.sql",
"migrations/8_.down.sql",
"migrations/8_.up.sql",
"migrations/9_.down.sql",
"migrations/9_.up.sql",
}When nextSeqVersion is used to obtain the next version number it takes the last element of the slice in line 31:
migrate/internal/cli/commands.go
Line 31 in 2477f63
| filename := matches[len(matches)-1] |
and this results in the new version number being determined as 10, which in this case is not correct.
PS: Everyone have a great week.
Activity
Guillembonet commentedon Jul 2, 2024
I think this happens because not all file have the same amount of digits, but this still works when applying migrations so I think it should be fixed. Also suffering the same here!
fzlzjerry commentedon Sep 9, 2026
I reproduced this on current master (
9429a3c): files numbered 0 through 11 produceduplicate migration version: 10. Mixed-width names also cause a silent error: with versions2and0010,-digits 3creates003instead of011.I'd like to work on this. I'll select the highest numeric version across the matching filenames, preserve padding and filename validation, and add helper and CLI regressions for mixed widths, digit limits and numeric overflow.