-
Notifications
You must be signed in to change notification settings - Fork 0
fix: prevent duplicate labels at database level #34
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4b48d57
Fix non-functional UI elements and label deduplication
796c16c
Fix duplicate labels at root cause: add unique constraint and dedup m…
d792dfa
fix: cast UUID to text for MIN() in dedup migration
4f8d6c9
fix: handle duplicate newId constraint in createMapping
012cfb0
fix: update test to expect label deduplication behavior
c012b16
fix: update board control tests to open filter bar first
33a4c53
fix: update palette test to open filter bar before interacting
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
packages/server/prisma/migrations/20260515094021_merge_duplicate_labels/migration.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| -- Step 1: Move issue associations from duplicate labels to the keeper (earliest id per name) | ||
| INSERT INTO "_IssueToIssueLabel" ("A", "B") | ||
| SELECT DISTINCT dup_assoc."A", keepers.keeper_id | ||
| FROM "_IssueToIssueLabel" dup_assoc | ||
| JOIN "IssueLabel" dup ON dup.id = dup_assoc."B" | ||
| JOIN ( | ||
| SELECT name, (MIN(id::text))::uuid AS keeper_id | ||
| FROM "IssueLabel" | ||
| GROUP BY name | ||
| HAVING COUNT(*) > 1 | ||
| ) keepers ON keepers.name = dup.name AND dup.id != keepers.keeper_id | ||
| WHERE NOT EXISTS ( | ||
| SELECT 1 FROM "_IssueToIssueLabel" existing | ||
| WHERE existing."A" = dup_assoc."A" AND existing."B" = keepers.keeper_id | ||
| ); | ||
|
|
||
| -- Step 2: Remove associations pointing to duplicate (non-keeper) labels | ||
| DELETE FROM "_IssueToIssueLabel" | ||
| WHERE "B" IN ( | ||
| SELECT dup.id | ||
| FROM "IssueLabel" dup | ||
| JOIN ( | ||
| SELECT name, (MIN(id::text))::uuid AS keeper_id | ||
| FROM "IssueLabel" | ||
| GROUP BY name | ||
| HAVING COUNT(*) > 1 | ||
| ) keepers ON keepers.name = dup.name AND dup.id != keepers.keeper_id | ||
| ); | ||
|
|
||
| -- Step 3: Delete duplicate labels (keep the earliest id per name) | ||
| DELETE FROM "IssueLabel" | ||
| WHERE id IN ( | ||
| SELECT dup.id | ||
| FROM "IssueLabel" dup | ||
| JOIN ( | ||
| SELECT name, (MIN(id::text))::uuid AS keeper_id | ||
| FROM "IssueLabel" | ||
| GROUP BY name | ||
| HAVING COUNT(*) > 1 | ||
| ) keepers ON keepers.name = dup.name AND dup.id != keepers.keeper_id | ||
| ); | ||
|
|
||
| -- Step 4: Add unique constraint on name | ||
| ALTER TABLE "IssueLabel" ADD CONSTRAINT "IssueLabel_name_key" UNIQUE ("name"); | ||
|
|
||
| -- Step 5: Drop the now-redundant index (unique constraint already creates one) | ||
| DROP INDEX IF EXISTS "IssueLabel_name_idx"; |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,13 +132,21 @@ async function createMapping( | |
| newId: string, | ||
| entityType: string, | ||
| ): Promise<void> { | ||
| await prisma.legacyLinearMapping.upsert({ | ||
| where: { | ||
| oldId_entityType: { oldId, entityType }, | ||
| }, | ||
| create: { oldId, newId, entityType }, | ||
| update: {}, | ||
| }); | ||
| try { | ||
| await prisma.legacyLinearMapping.upsert({ | ||
| where: { | ||
| oldId_entityType: { oldId, entityType }, | ||
| }, | ||
| create: { oldId, newId, entityType }, | ||
| update: {}, | ||
| }); | ||
| } catch (error: unknown) { | ||
| const prismaError = error as { code?: string }; | ||
| if (prismaError.code === 'P2002') { | ||
| return; | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| function recordSkippedImport( | ||
|
|
@@ -282,10 +290,16 @@ async function importLabels( | |
| } | ||
|
|
||
| const created = await prisma.$transaction(async (transaction) => { | ||
| const nextLabel = await transaction.issueLabel.create({ | ||
| data: { name: label.name }, | ||
| let nextLabel = await transaction.issueLabel.findFirst({ | ||
| where: { name: label.name }, | ||
| }); | ||
|
|
||
| if (!nextLabel) { | ||
| nextLabel = await transaction.issueLabel.create({ | ||
| data: { name: label.name }, | ||
| }); | ||
| } | ||
|
Comment on lines
+293
to
+301
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of manually checking for existence and then creating, you can use Prisma's const nextLabel = await transaction.issueLabel.upsert({
where: { name: label.name },
update: {},
create: { name: label.name },
});
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| await createMapping(transaction, label.id, nextLabel.id, 'label'); | ||
| return nextLabel; | ||
| }); | ||
|
|
||
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
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
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a significant discrepancy between the implementation and the PR description. The description states that a unique constraint
@@unique([name, teamId])was added, but the code only adds@uniqueto thenamefield.In a multi-tenant system like this (where
IssueandWorkflowStateare scoped to ateamId), labels are typically also scoped to a team. If labels are intended to be per-team, theIssueLabelmodel is missing ateamIdfield and the corresponding relation. If labels are truly global, then the PR description is incorrect and should be updated to reflect that uniqueness is enforced globally by name.