Skip to content

create -seq cli command produces wrong migration number #1109

Description

@42LM

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: 10

The /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:

  1. Create an example project with a /migrations folder 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
  1. 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
  2. See error
~/go/src/migratetest
migrate create -dir ./migrations -seq -digits 2 -ext sql foobar
error: duplicate migration version: 10

Expected 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:

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:

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

  1. Guillembonet commented on Jul 2, 2024

    @Guillembonet

    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!

  2. fzlzjerry commented on Sep 9, 2026

    @fzlzjerry

    I reproduced this on current master (9429a3c): files numbered 0 through 11 produce duplicate migration version: 10. Mixed-width names also cause a silent error: with versions 2 and 0010, -digits 3 creates 003 instead of 011.

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions