Skip to content

Commit

Permalink
feat(deploy_db): adiciona função e trigger para prevenir duplicatas e…
Browse files Browse the repository at this point in the history
…m cliente_tag
  • Loading branch information
renatocron committed Sep 19, 2024
1 parent d2f3ad7 commit 559a6ec
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions api/deploy_db/deploy/0028-mf-info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,26 @@ 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
-- Se já existe uma tag igual para o cliente, não permitir a inserção
IF EXISTS (SELECT 1 FROM cliente_tag WHERE cliente_id = NEW.cliente_id AND mf_tag_id = NEW.mf_tag_id) THEN
RETURN NULL;
END IF;

RETURN NEW;
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;

0 comments on commit 559a6ec

Please sign in to comment.