-
-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: create trigger for workspace delete action
- Loading branch information
Showing
3 changed files
with
62 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,49 @@ | ||
-- Create the trigger function | ||
CREATE TABLE IF NOT EXISTS af_workspace_deleted ( | ||
workspace_id uuid PRIMARY KEY NOT NULL, | ||
deleted_at timestamp with time zone DEFAULT now() | ||
); | ||
|
||
GRANT SELECT, INSERT, UPDATE, DELETE ON public.af_workspace_deleted TO supabase_auth_admin; | ||
|
||
-- Workspace delete trigger | ||
CREATE OR REPLACE FUNCTION workspace_deleted_trigger_function() | ||
RETURNS trigger AS | ||
$$ | ||
DECLARE | ||
payload jsonb; | ||
BEGIN | ||
payload := jsonb_build_object( | ||
'workspace_id', OLD.workspace_id, | ||
'owner_id', OLD.owner_uid | ||
'workspace_id', OLD.workspace_id | ||
); | ||
|
||
INSERT INTO public.af_workspace_deleted (workspace_id, deleted_at) | ||
VALUES (OLD.workspace_id, now()) | ||
ON CONFLICT DO NOTHING; | ||
PERFORM pg_notify('af_workspace_deleted', payload::text); | ||
RETURN OLD; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
-- Create the trigger that calls the function after deleting a workspace | ||
CREATE TRIGGER on_workspace_delete | ||
AFTER DELETE ON public.af_workspace | ||
FOR EACH ROW | ||
EXECUTE FUNCTION workspace_deleted_trigger_function(); | ||
EXECUTE FUNCTION workspace_deleted_trigger_function(); | ||
|
||
-- Delete user trigger | ||
CREATE OR REPLACE FUNCTION notify_user_deletion() | ||
RETURNS TRIGGER AS $$ | ||
DECLARE | ||
payload TEXT; | ||
BEGIN | ||
payload := jsonb_build_object( | ||
'user_uuid', OLD.uuid::text | ||
); | ||
|
||
PERFORM pg_notify('af_user_deleted', payload::text); | ||
RETURN OLD; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER user_deletion_trigger | ||
AFTER DELETE ON public.af_user | ||
FOR EACH ROW | ||
EXECUTE FUNCTION notify_user_deletion(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters