Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create -seq cli command produces wrong migration number #1109

Open
42LM opened this issue Jun 17, 2024 · 1 comment
Open

create -seq cli command produces wrong migration number #1109

42LM opened this issue Jun 17, 2024 · 1 comment

Comments

@42LM
Copy link

42LM commented Jun 17, 2024

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.

@Guillembonet
Copy link

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!

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

No branches or pull requests

2 participants