Skip to content

Commit

Permalink
feat(hasura): remove old alerts (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelB committed Feb 23, 2021
1 parent 7ecf74b commit ac649ad
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README-dev.md
Expand Up @@ -155,6 +155,15 @@ select audit.audit_table('documents',

Pour voir la [configuration du trigger](targets/hasura/migrations/1613474820206_audit_trigger/up.sql)

## Suppression des données anciennes

Les données de certaines tables sont nettoyées automatiquement.

Pour l'instant seulement 2 triggers sont en place:

- nettoyage de la table `alerts` (alertes traitées conservés pour 3mois)
- nettoyage de la table `audit.logged_action` (actions conservées pour 3mois)

## How to ?

### How to retrieve CDTN data from production ?
Expand Down
2 changes: 2 additions & 0 deletions targets/hasura/migrations/1613499638772_alert_clean/down.sql
@@ -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 targets/hasura/migrations/1613499638772_alert_clean/up.sql
@@ -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';

0 comments on commit ac649ad

Please sign in to comment.