diff --git a/src/catalog/backends/pgconfig/src/main/resources/db/pgconfig/migration/postgresql/V1_8__ServiceInfoSettingsInfoConstraints.sql b/src/catalog/backends/pgconfig/src/main/resources/db/pgconfig/migration/postgresql/V1_8__ServiceInfoSettingsInfoConstraints.sql index e5ce1b9c3..b2e2f6984 100644 --- a/src/catalog/backends/pgconfig/src/main/resources/db/pgconfig/migration/postgresql/V1_8__ServiceInfoSettingsInfoConstraints.sql +++ b/src/catalog/backends/pgconfig/src/main/resources/db/pgconfig/migration/postgresql/V1_8__ServiceInfoSettingsInfoConstraints.sql @@ -10,21 +10,21 @@ * - serviceinfo("@type", workspace) */ - -ALTER TABLE settingsinfo - ADD CONSTRAINT fk_settingsinfo_workspace - FOREIGN KEY (workspace) - REFERENCES workspaceinfo(id) - ON DELETE CASCADE; - -CREATE UNIQUE INDEX settingsinfo_workspace_key - ON settingsinfo (workspace) - NULLS NOT DISTINCT; +/* + * Delete possible duplicate settingsinfo by workspace before enforcing uniqueness. + */ +DELETE FROM settingsinfo WHERE id IN( SELECT id FROM ( + SELECT id, + ROW_NUMBER() OVER(PARTITION BY workspace ORDER BY id ASC) AS duprow + FROM settingsinfo +) dups +WHERE +dups.duprow > 1); /* * Delete possible duplicate serviceinfos by type and workspace before enforcing uniqueness. */ -DELETE FROM serviceinfo WHERE id IN( select id FROM ( +DELETE FROM serviceinfo WHERE id IN( SELECT id FROM ( SELECT id, ROW_NUMBER() OVER(PARTITION BY "@type", workspace ORDER BY id ASC) AS duprow FROM serviceinfo @@ -32,6 +32,16 @@ DELETE FROM serviceinfo WHERE id IN( select id FROM ( WHERE dups.duprow > 1); +ALTER TABLE settingsinfo + ADD CONSTRAINT fk_settingsinfo_workspace + FOREIGN KEY (workspace) + REFERENCES workspaceinfo(id) + ON DELETE CASCADE; + +CREATE UNIQUE INDEX settingsinfo_workspace_key + ON settingsinfo (workspace) + NULLS NOT DISTINCT; + ALTER TABLE serviceinfo ADD CONSTRAINT fk_serviceinfo_workspace FOREIGN KEY (workspace)