diff --git a/api/deploy_db/deploy/0028-mf-info.sql b/api/deploy_db/deploy/0028-mf-info.sql index 0c0fb8b..5dac782 100644 --- a/api/deploy_db/deploy/0028-mf-info.sql +++ b/api/deploy_db/deploy/0028-mf-info.sql @@ -71,4 +71,25 @@ create table cliente_tag ( alter table quiz_config add column tag json not null default '[]'; alter table quiz_config alter tag set not null; +CREATE OR REPLACE FUNCTION prevent_duplicate_cliente_tag() +RETURNS TRIGGER AS $$ +BEGIN + INSERT INTO cliente_tag (cliente_id, mf_tag_id, created_on) + VALUES (NEW.cliente_id, NEW.mf_tag_id, NOW()) + ON CONFLICT (cliente_id, mf_tag_id) DO NOTHING; + + RETURN NULL; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER prevent_duplicate_cliente_tag_trigger +BEFORE INSERT ON cliente_tag +FOR EACH ROW +EXECUTE FUNCTION prevent_duplicate_cliente_tag(); + +ALTER TABLE cliente_tag +ADD CONSTRAINT unique_cliente_tag UNIQUE (cliente_id, mf_tag_id); + + + COMMIT;