-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(migration.sql): add triggers and functions to increment/decremen…
…t project notices count and update hourly occurrences count on occurrence creation and deletion
- Loading branch information
1 parent
400fca7
commit a9bd229
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
prisma/migrations/20230526040245_rebuild_functions/migration.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,48 @@ | ||
-- Drop triggers | ||
DROP TRIGGER IF EXISTS after_notice_create ON notice; | ||
DROP TRIGGER IF EXISTS after_notice_delete ON notice; | ||
DROP TRIGGER IF EXISTS after_occurrence ON occurrence; | ||
|
||
-- Drop functions | ||
DROP FUNCTION IF EXISTS increment_project_notice_count(); | ||
DROP FUNCTION IF EXISTS decrement_project_notice_count(); | ||
DROP FUNCTION IF EXISTS update_hourly_occurrence(); | ||
|
||
CREATE OR REPLACE FUNCTION increment_project_notices_count() RETURNS TRIGGER AS $$ | ||
BEGIN | ||
UPDATE projects | ||
SET notices_count = notices_count + 1 | ||
WHERE id = NEW.project_id; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
CREATE OR REPLACE FUNCTION decrement_project_notices_count() RETURNS TRIGGER AS $$ | ||
BEGIN | ||
UPDATE projects | ||
SET notices_count = notices_count - 1 | ||
WHERE id = OLD.project_id; | ||
RETURN OLD; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER after_notice_create AFTER INSERT ON notices FOR EACH ROW EXECUTE FUNCTION increment_project_notices_count(); | ||
CREATE TRIGGER after_notice_delete AFTER DELETE ON notices FOR EACH ROW EXECUTE FUNCTION decrement_project_notices_count(); | ||
|
||
CREATE OR REPLACE FUNCTION update_hourly_occurrences() RETURNS TRIGGER AS $$ | ||
BEGIN | ||
UPDATE hourly_occurrences AS ho | ||
SET count = count + 1 | ||
WHERE ho.occurrence_id = NEW.id | ||
AND ho.interval_start = date_trunc('hour', NEW.updated_at) | ||
AND ho.interval_end = date_trunc('hour', NEW.updated_at) + interval '1 hour'; | ||
|
||
IF NOT FOUND THEN | ||
INSERT INTO hourly_occurrences (occurrence_id, interval_start, interval_end, count) | ||
VALUES (NEW.id, date_trunc('hour', NEW.updated_at), date_trunc('hour', NEW.updated_at) + interval '1 hour', 1); | ||
END IF; | ||
|
||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER after_occurrence AFTER INSERT OR UPDATE ON occurrences FOR EACH ROW EXECUTE FUNCTION update_hourly_occurrences(); |