-
-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #421 from bookbrainz/delete-duplicate-relationships
BB-292 fix(sql): Script to delete duplicate relationships
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,27 @@ | ||
-- This script selects exact duplicate relationships that appear in the same relationshipSet | ||
-- then deletes them from the relevant tables | ||
|
||
CREATE TEMPORARY TABLE IF NOT EXISTS duplicate_relationship_ids | ||
as select distinct id from ( | ||
select | ||
dupe.id | ||
FROM relationship as dupe | ||
join relationship_set__relationship as set1 on set1.relationship_id = dupe.id | ||
join relationship as keep on | ||
dupe.id > keep.id and dupe.type_id = keep.type_id and dupe.source_bbid = keep.source_bbid and dupe.target_bbid = keep.target_bbid | ||
join relationship_set__relationship as set2 on set2.relationship_id = keep.id | ||
-- target only duplicate relationships that appear in the same set | ||
where set1.set_id = set2.set_id | ||
) duplicate_relationships; | ||
|
||
BEGIN TRANSACTION; | ||
|
||
DELETE FROM relationship_set__relationship | ||
USING duplicate_relationship_ids | ||
WHERE relationship_set__relationship.relationship_id = duplicate_relationship_ids.id; | ||
|
||
DELETE FROM relationship | ||
USING duplicate_relationship_ids | ||
where relationship.id = duplicate_relationship_ids.id; | ||
|
||
COMMIT TRANSACTION; |