Skip to content

fix: bump and retry goose create on duplicate migration versions - #1118

Open
cestercian wants to merge 1 commit into
pressly:mainfrom
cestercian:cursor/fix-create-duplicate-version-retry-42f5
Open

fix: bump and retry goose create on duplicate migration versions#1118
cestercian wants to merge 1 commit into
pressly:mainfrom
cestercian:cursor/fix-create-duplicate-version-retry-42f5

Conversation

@cestercian

Copy link
Copy Markdown

Bug

goose create can write two migrations with different names but the same version.

  • Timestamp mode: two creates in the same second share 20060102150405, so later up panics in Migrations.Less (migrate.go) with a duplicate version. The provider path already returns a proper error.
  • Sequential mode (-s): scan for last.Version+1 then create is TOCTOU. Concurrent creates collide; create itself can panic when the dir scan hits the duplicates.
  • fix: return clear error when migration file already exists #1104 made create atomic with O_EXCL for the filename, but that does not protect version uniqueness across different names.

Fix

Option 2 from #1116:

  • After the atomic O_EXCL create, detect when another file already owns that version.
  • Bump the version (timestamp +1s, or sequential max+1) and retry O_EXCL.
  • Exact same filename still returns os.ErrExist (keeps fix: return clear error when migration file already exists #1104).
  • Sequential create no longer goes through collectMigrationsFS, so existing duplicate versions cannot panic create.
  • timestampFormat is unchanged (no sub-second versions).
  • Legacy CollectMigrations now returns an error for duplicate versions instead of panicking in Migrations.Less, matching provider_collect.go.

Tests

  • Same-second timestamp collision (including later-name-first).
  • Sequential collision / retry, existing duplicates, and concurrent creates.
  • TestCreateDuplicateFile still expects os.ErrExist.

Fixes #1116

Drafted with Cursor; human-reviewed before opening.

Detect version collisions during create and retry the O_EXCL write
with a bumped version so different names cannot share a timestamp
or sequential version. Exact filename collisions still return
os.ErrExist. CollectMigrations now errors on duplicates instead of
panicking in Migrations.Less.

Fixes pressly#1116
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.

goose create can produce duplicate migration versions

1 participant