unique keywords_i18n constraint plus delete duplicates (#320)

This commit is contained in:
Fredrik Ringqvist
2023-04-26 15:09:43 +02:00
committed by GitHub
parent 4fe3869454
commit 9501af677f
+18
View File
@@ -0,0 +1,18 @@
-- Add unique keyword_i18n locale constraint
-- First we have to delete the duplicates that already exists (keywords 1316-1326; 138 keywords_i18n entries)
with duplicates as (
select duplicate.id duplicate_id, *
from keywords_i18n first
-- inner join the same table where keyword_id, locale_id AND value must all be the same,
-- i.e. only duplicate translations with the same translated value will be included
join keywords_i18n duplicate using (keyword_id, locale_id, value)
where first.id <> duplicate.id
-- only include the last copy, so we dont delete the original
and duplicate.id > first.id
) delete from keywords_i18n where id in (select duplicate_id from duplicates);
-- Now we are allowed to add our constraint
ALTER TABLE keywords_i18n ADD UNIQUE (keyword_id, locale_id);