Skip to content

migrate create with seq does not handle dot dir prefix #238

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

Closed
Etomyutikos opened this issue Jun 18, 2019 · 5 comments
Closed

migrate create with seq does not handle dot dir prefix #238

Etomyutikos opened this issue Jun 18, 2019 · 5 comments

Comments

@Etomyutikos
Copy link

Etomyutikos commented Jun 18, 2019

Describe the Bug
When using the create subcommand with the seq flag, the command fails if dir begins with a ..

Steps to Reproduce
Steps to reproduce the behavior:

  1. Create first migration:
mkdir test
migrate create -ext sql -dir ./test -seq first
  1. Create second migration:
migrate create -ext sql -dir ./test -seq second
  1. See error:
error: strconv.Atoi: parsing "test/000001": invalid syntax

Expected Behavior
migrate create -seq should create a second migration with the proper sequence number when dir begins with a ..

Migrate Version
v4.4.0

Loaded Source Drivers

Source drivers: gitlab, go-bindata, godoc-vfs, gcs, file, s3, github

Loaded Database Drivers

Database drivers: cassandra, crdb-postgres, postgres, stub, clickhouse, cockroach, cockroachdb, mongodb, mysql, postgresql, redshift, spanner, sqlserver

Go Version

go version go1.12.5 darwin/amd64

Stacktrace
Pretty sure it's this line: https://github.com/golang-migrate/migrate/blob/master/internal/cli/commands.go#L31

Additional context
migrate create -seq works just fine when dir does not begin with ..

@dhui
Copy link
Member

dhui commented Jun 19, 2019

Thanks for reporting the issue!

My guess is that filepath.Glob() returns the matches w/o the dot prefix. We should normalize both the dir arg and the matched filepaths before trimming the dir prefix to parse/determine the next migration sequence number.

dhui added a commit to dhui/migrate that referenced this issue Jun 21, 2019
@dhui
Copy link
Member

dhui commented Jun 21, 2019

This should be fixed in master now and will go out with the next release

@toqueteos
Copy link

Unfortunately the fix seems to not be compatible with Windows paths.

$ go version
go1.12.7 windows/amd64
$ migrate -version
4.5.0
$ rm -rf backend/migrations/
$ migrate create -seq -dir ./backend/migrations -ext sql create_accounts_table
$ migrate create -seq -dir ./backend/migrations -ext sql create_bots_table
error: strconv.Atoi: parsing "backend\\migrations\\000001": invalid syntax

@dhui
Copy link
Member

dhui commented Jul 20, 2019

Unfortunately, I don't have access to a windows machine, so won't be able to reproduce or fix the issue for windows. My guess is that the glob match needs to be cleaned before strings.TrimPrefix() is called.

Does using windows path separators work?
e.g.

$ migrate create -seq -dir .\\backend\\migrations -ext sql create_accounts_table
$ migrate create -seq -dir .\\backend\\migrations -ext sql create_bots_table

@toqueteos
Copy link

Nope, it doesn't work (with and without escaping backslashes):

$ migrate create -seq -dir .\\backend\\migrations -ext sql create_accounts_table
$ migrate create -seq -dir .\\backend\\migrations -ext sql create_bots_table
error: strconv.Atoi: parsing "backend\\migrations\\000001": invalid syntax

...

$ migrate create -seq -dir .\backend\migrations -ext sql create_accounts_table
$ migrate create -seq -dir .\backend\migrations -ext sql create_bots_table
error: strconv.Atoi: parsing "backend\\migrations\\000001": invalid syntax

I'll take a look and see if I can get it to work with Windows paths.

ggilmore added a commit to sourcegraph/sourcegraph-public-snapshot that referenced this issue Sep 6, 2019
ggilmore added a commit to sourcegraph/sourcegraph-public-snapshot that referenced this issue Sep 6, 2019
13k added a commit to 13k/migrate that referenced this issue Dec 4, 2019
* Validates migration version on creation, to avoid creation of duplicated versions
* Uses `os.OpenFile` with `O_CREATE|O_EXCL` to create files to avoid file collisions
* Uses `filepath.Join` to concatenate paths, making `cleanPath()` not necessary
* Prints generated filenames
* Fixes golang-migrate#238
* Supersedes golang-migrate#250
@dhui dhui closed this as completed in 8e142df Mar 10, 2020
FPiety0521 pushed a commit to FPiety0521/Golang-SQL that referenced this issue May 24, 2023
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 a pull request may close this issue.

3 participants