Files
next-tjwater-server/resources/sql/020_prune_non_actionable_notifications.sql
T

52 lines
1.7 KiB
PL/PgSQL

-- Remove historical task lifecycle notices that do not require user action.
BEGIN;
CREATE TEMP TABLE notification_cleanup_projects ON COMMIT DROP AS
SELECT
project_id,
count(*)::bigint AS removed_count
FROM workbench.operational_task_notifications
WHERE document->>'kind' IN ('task_created', 'report_updated', 'task_completed')
GROUP BY project_id;
DELETE FROM workbench.operational_task_notifications
WHERE document->>'kind' IN ('task_created', 'report_updated', 'task_completed');
WITH inserted_events AS (
INSERT INTO workbench.domain_event_log (
project_id,
event_id,
topic,
aggregate_type,
aggregate_id,
aggregate_revision,
metadata
)
SELECT
project_id,
(
substr(md5(project_id::text || ':notification-routing-v1'), 1, 8)
|| '-' || substr(md5(project_id::text || ':notification-routing-v1'), 9, 4)
|| '-5' || substr(md5(project_id::text || ':notification-routing-v1'), 14, 3)
|| '-a' || substr(md5(project_id::text || ':notification-routing-v1'), 18, 3)
|| '-' || substr(md5(project_id::text || ':notification-routing-v1'), 21, 12)
)::uuid,
'operational.notification.changed',
'operational_notification_batch',
project_id::text,
removed_count,
jsonb_build_object(
'change', 'batch_pruned',
'reason', 'non_actionable_notification_cleanup',
'removed_count', removed_count
)
FROM notification_cleanup_projects
ON CONFLICT (project_id, event_id) DO NOTHING
RETURNING project_id, event_seq
)
SELECT pg_notify('workbench_events', project_id::text || ':' || event_seq::text)
FROM inserted_events;
COMMIT;