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 87ff4ac
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 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,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;

0 comments on commit 87ff4ac

Please sign in to comment.