Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hasura): remove old alerts (#339)
- Loading branch information
Showing
3 changed files
with
31 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DROP TRIGGER IF EXISTS delete_alerts on alerts; | ||
DROP FUNCTION IF EXISTS delete_old_alerts; |
20 changes: 20 additions & 0 deletions
20
targets/hasura/migrations/1613499638772_alert_clean/up.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,20 @@ | ||
CREATE OR REPLACE FUNCTION delete_old_alerts() RETURNS trigger AS $body$ | ||
BEGIN | ||
DELETE FROM alerts WHERE created_at < NOW() - TG_ARGV[0]::integer * interval'1 month' AND status IN ('done', 'rejected'); | ||
RETURN NULL; | ||
END; | ||
$body$ | ||
LANGUAGE 'plpgsql'; | ||
|
||
COMMENT ON FUNCTION delete_old_alerts(duration) IS $body$ | ||
Remove alerts that are older that a given duration | ||
|
||
Arguments: | ||
duration: duration in month until alerts are dropped | ||
$body$; | ||
|
||
CREATE TRIGGER delete_alerts AFTER INSERT OR UPDATE ON alerts | ||
FOR EACH STATEMENT EXECUTE PROCEDURE delete_old_alerts(3); | ||
|
||
COMMENT ON TRIGGER "delete_alerts" ON alerts | ||
IS 'trigger to remove alert that older than 3 monthes'; |