From 87ff4ac0575db4c73378ecd610aad8ffac2ca85b Mon Sep 17 00:00:00 2001 From: Renato Cron Date: Thu, 19 Sep 2024 07:37:18 +0000 Subject: [PATCH] =?UTF-8?q?feat(deploy=5Fdb):=20adiciona=20fun=C3=A7=C3=A3?= =?UTF-8?q?o=20e=20trigger=20para=20prevenir=20duplicatas=20em=20cliente?= =?UTF-8?q?=5Ftag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/deploy_db/deploy/0028-mf-info.sql | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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;