Skip to content

Commit

Permalink
feat: table activity trigger for permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Sep 30, 2024
1 parent 6e08301 commit fb8e0e5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
9 changes: 1 addition & 8 deletions views/020_teams_triggers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
CREATE OR REPLACE FUNCTION handle_team_updates()
RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'DELETE' THEN
PERFORM pg_notify('table_activity', TG_TABLE_NAME || ' ' || OLD.id);
RETURN NULL;
END IF;

IF OLD.deleted_at IS NULL AND NEW.deleted_at IS NOT NULL THEN
DELETE FROM team_components WHERE team_id = OLD.id;
END IF;
Expand All @@ -15,12 +10,10 @@ BEGIN
UPDATE notifications SET error = NULL WHERE team_id = NEW.id;
END IF;

PERFORM pg_notify('table_activity', TG_TABLE_NAME || ' ' || NEW.id);

RETURN NULL;
END
$$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER team_updates
AFTER UPDATE OR DELETE ON teams
AFTER UPDATE ON teams
FOR EACH ROW EXECUTE PROCEDURE handle_team_updates();
19 changes: 0 additions & 19 deletions views/021_notification.sql
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
-- Notify on any updates/deletes on the notifications table
CREATE OR REPLACE FUNCTION handle_notifications_updates_deletes()
RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'DELETE' THEN
PERFORM pg_notify('table_activity', TG_TABLE_NAME || ' ' || OLD.id);
ELSE
PERFORM pg_notify('table_activity', TG_TABLE_NAME || ' ' || NEW.id);
END IF;

RETURN NULL;
END
$$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER handle_notifications_updates_deletes_trigger
AFTER UPDATE OR DELETE ON notifications
FOR EACH ROW
EXECUTE PROCEDURE handle_notifications_updates_deletes();

-- Handle before updates for notifications
CREATE OR REPLACE FUNCTION reset_notification_error_before_update()
RETURNS TRIGGER AS $$
Expand Down
34 changes: 34 additions & 0 deletions views/033_table_activity.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- Cleanup previous trigger and function
DROP TRIGGER IF EXISTS handle_notifications_updates_deletes_trigger ON notifications;

DROP FUNCTION IF EXISTS handle_notifications_updates_deletes;

-- Notify on any updates/deletes on these tables
CREATE OR REPLACE FUNCTION notify_table_updates_and_deletes()
RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'DELETE' THEN
PERFORM pg_notify('table_activity', TG_TABLE_NAME || ' ' || OLD.id);
ELSE
PERFORM pg_notify('table_activity', TG_TABLE_NAME || ' ' || NEW.id);
END IF;

RETURN NULL;
END
$$ LANGUAGE plpgsql;

DO $$
DECLARE
table_name TEXT;
BEGIN
FOR table_name IN SELECT unnest(ARRAY['notifications', 'permissions', 'teams'])
LOOP
EXECUTE format('
CREATE OR REPLACE TRIGGER notify_updates_and_deletes
AFTER UPDATE OR DELETE ON %I
FOR EACH ROW
EXECUTE PROCEDURE notify_table_updates_and_deletes()',
table_name
);
END LOOP;
END $$;

0 comments on commit fb8e0e5

Please sign in to comment.