-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move count increase to a trigger
- Loading branch information
Showing
4 changed files
with
46 additions
and
60 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
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
23 changes: 23 additions & 0 deletions
23
supabase/migrations/20241110101532_update help request totals via trigger.sql
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
CREATE OR REPLACE FUNCTION update_asignees_count() | ||
RETURNS TRIGGER AS $$ | ||
BEGIN | ||
RAISE NOTICE 'Trigger executed on operation: %, help_request_id: %', TG_OP, COALESCE(NEW.help_request_id, OLD.help_request_id); | ||
|
||
IF TG_OP = 'INSERT' THEN | ||
-- Increment assignments_count on assignment add | ||
UPDATE public.help_requests | ||
SET asignees_count = asignees_count + 1 | ||
WHERE id = NEW.help_request_id; | ||
ELSIF TG_OP = 'DELETE' THEN | ||
-- Decrement assignments_count on assignment removal | ||
UPDATE public.help_requests | ||
SET asignees_count = asignees_count - 1 | ||
WHERE id = OLD.help_request_id; | ||
END IF; | ||
RETURN NULL; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER trigger_update_assignments_count | ||
AFTER INSERT OR DELETE ON public.help_request_assignments | ||
FOR EACH ROW EXECUTE FUNCTION update_asignees_count(); |