Skip to content

Commit

Permalink
feat: sync identities with people table
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Sep 4, 2024
1 parent e07c3af commit 809ff8c
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions views/011_users.sql
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
-- Insert identities in people table
CREATE
OR REPLACE FUNCTION insert_identity_to_people () RETURNS TRIGGER AS $$
CREATE OR REPLACE FUNCTION sync_identity_to_people ()
RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'INSERT' THEN
INSERT INTO people (id, name, email)
VALUES (NEW.id, concat(NEW.traits::json->'name'->>'first', ' ', NEW.traits::json->'name'->>'last'), NEW.traits::json->>'email');
ELSIF TG_OP = 'UPDATE' THEN
UPDATE people SET
name = concat(NEW.traits::json->'name'->>'first', ' ', NEW.traits::json->'name'->>'last'),
email = NEW.traits::json->>'email'
WHERE id = NEW.id;
END IF;

RETURN NEW;
RETURN NEW;
END
$$ LANGUAGE plpgsql;

DO $$
BEGIN
IF EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'identities') THEN
CREATE OR REPLACE TRIGGER identity_to_people
AFTER INSERT
ON identities
FOR EACH ROW
EXECUTE PROCEDURE insert_identity_to_people();
END IF;
IF EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'identities') THEN
CREATE OR REPLACE TRIGGER identity_to_people
AFTER INSERT OR UPDATE ON identities
FOR EACH ROW
EXECUTE PROCEDURE sync_identity_to_people();
END IF;
END $$;

CREATE OR REPLACE VIEW
Expand Down

0 comments on commit 809ff8c

Please sign in to comment.