diff --git a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.6_2024.12.17__workspaceitem_add_item_id_unique_constraint.sql b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.6_2024.12.17__workspaceitem_add_item_id_unique_constraint.sql index d2b7de7dc37..38389bf2d19 100644 --- a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.6_2024.12.17__workspaceitem_add_item_id_unique_constraint.sql +++ b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.6_2024.12.17__workspaceitem_add_item_id_unique_constraint.sql @@ -7,15 +7,15 @@ -- -- In the workspaceitem table, if there are multiple rows referring to the same item ID, keep only the first of them. -WITH dedup AS ( - SELECT item_id, MIN(workspace_item_id) AS workspace_item_id +DELETE FROM workspaceitem WHERE EXISTS ( + SELECT item_id FROM workspaceitem GROUP BY item_id HAVING COUNT(workspace_item_id) > 1 -) -DELETE FROM workspaceitem - USING dedup -WHERE workspaceitem.item_id = dedup.item_id AND workspaceitem.workspace_item_id <> dedup.workspace_item_id; - +) AND workspaceitem.workspace_item_id NOT IN ( + SELECT MIN(workspace_item_id) AS workspace_item_id + FROM workspaceitem + GROUP BY item_id +); -- Identify which rows have duplicates, and compute their replacements. ALTER TABLE workspaceitem ADD CONSTRAINT unique_item_id UNIQUE(item_id);