Fix build metadata race condition #9756
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a race condition in our publish endpoint. When two versions,
1.0.0+foo
and1.0.0+bar
are published concurrently we previously could get into a situation where the "unique version number excluding build metadata" check succeeds for both, and then both versions are added to the database, when only one such version should exist.This PR adds a new
unique
constraint on(crate_id, num_no_build)
to prevent this from happening.We currently have around 650 such cases still in the database though. In those cases the
num_no_build
column is adjusted to actually contain the build metadata too (see column comment).Finally, this PR needs to be merged/deployed in multiple stages. The initial migration adds the column in a nullable way, because the SQL script to backfill the data for the column takes about 30 sec (on my machine), and this shouldn't prevent the API server from booting up because of a running schema migration. It is recommended to merge/deploy each migration in this PR independently!