fix: prevent duplicate preview deployments on labeled PRs#4552
Open
Wouter8 wants to merge 1 commit into
Open
Conversation
Opening a PR with a label fires `opened` + `labeled` together; both created a preview for the same PR after the dedup constraint was dropped (a212d42) and `labeled` became a trigger (e554adc). Restore the unique (applicationId, pullRequestId) constraint (de-duping rows first), make createPreviewDeployment idempotent via onConflictDoNothing, and stop label events redeploying existing previews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Opening a PR that has a label produces duplicate preview deployments — a regression of #1204 (issue #1198).
GitHub fires two
pull_requestwebhooks when a labeled PR is opened:openedandlabeled. Two later changes made both create a preview for the same PR:e554adc3added"labeled"toshouldCreateDeployment, so label events can create previews.a212d4249dropped theUNIQUE(applicationId, pullRequestId)constraint onpreview_deployments.createPreviewDeploymentdoes a non-atomic check-then-insert. With no constraint and no lock, the concurrentopened+labeledwebhooks both pass the existence check and both insert, so two previews are created per PR.Fix
UNIQUE(applicationId, pullRequestId)constraint. The migration de-duplicates existing rows (keeping the oldest per PR/app) before adding it, so it applies cleanly on installs that already have duplicates.createPreviewDeploymentidempotent: insert first withonConflictDoNothing; the winner does the side effects (comment, domain), the loser returns the existing preview. The "add the label to an open PR later" flow keeps working.labeled/unlabeled— a label change is not a code change (extracted intoshouldDeployPreviewDeployment+ unit tests).Verification
ON CONFLICTpath verified against Postgres 16: duplicates collapse to the oldest row, the constraint is added, the racy second insert returns no row, and new PRs still insert.pnpm typecheckpasses for@dokploy/serveranddokploy.biome checkclean;vitestgreen including new tests.