Skip to content

Commit

Permalink
Merge pull request #421 from bookbrainz/delete-duplicate-relationships
Browse files Browse the repository at this point in the history
BB-292 fix(sql): Script to delete duplicate relationships
  • Loading branch information
MonkeyDo committed May 14, 2020
2 parents a8aa31b + 435ce89 commit 32594b5
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions sql/scripts/delete_duplicate_relationships.sql
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;

0 comments on commit 32594b5

Please sign in to comment.