Skip to content

Commit

Permalink
Remove database triggers to maintain old notification counts
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Nov 5, 2024
1 parent e146f4a commit fafeb99
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 38 deletions.
5 changes: 4 additions & 1 deletion temba/notifications/migrations/0025_update_triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@

class Migration(migrations.Migration):

dependencies = [("notifications", "0024_notification_data_and_more")]
dependencies = [
("notifications", "0024_notification_data_and_more"),
("orgs", "0156_itemcount"),
]

operations = [migrations.RunSQL(SQL)]
16 changes: 16 additions & 0 deletions temba/notifications/migrations/0027_update_triggers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 5.1.2 on 2024-11-05 18:33

from django.db import migrations

SQL = """
DROP TRIGGER temba_notifications_update_notificationcount ON notifications_notification;
DROP FUNCTION temba_notification_on_change();
DROP FUNCTION temba_insert_notificationcount(INT, INT, INT);
"""


class Migration(migrations.Migration):

dependencies = [("notifications", "0026_backfill_new_counts")]

operations = [migrations.RunSQL(SQL)]
32 changes: 1 addition & 31 deletions temba/sql/current_functions.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Generated by collect_sql on 2024-11-05 15:35 UTC
-- Generated by collect_sql on 2024-11-05 18:36 UTC

----------------------------------------------------------------------
-- Convenience method to call contact_toggle_system_group with a row
Expand Down Expand Up @@ -376,16 +376,6 @@ CREATE OR REPLACE FUNCTION temba_insert_flowpathcount(_flow_id INTEGER, _from_uu
END;
$$ LANGUAGE plpgsql;

----------------------------------------------------------------------
-- Inserts a new notificationcount row with the given values
----------------------------------------------------------------------
CREATE OR REPLACE FUNCTION temba_insert_notificationcount(_org_id INT, _user_id INT, _count INT) RETURNS VOID AS $$
BEGIN
INSERT INTO notifications_notificationcount("org_id", "user_id", "count", "is_squashed")
VALUES(_org_id, _user_id, _count, FALSE);
END;
$$ LANGUAGE plpgsql;

----------------------------------------------------------------------
-- Handles DELETE statements on ivr_call table
----------------------------------------------------------------------
Expand Down Expand Up @@ -605,26 +595,6 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

----------------------------------------------------------------------
-- Trigger procedure to notification counts on notification changes
----------------------------------------------------------------------
CREATE OR REPLACE FUNCTION temba_notification_on_change() RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'INSERT' AND position('U' IN NEW.medium) > 0 AND NOT NEW.is_seen THEN -- new notification inserted
PERFORM temba_insert_notificationcount(NEW.org_id, NEW.user_id, 1);
ELSIF TG_OP = 'UPDATE' AND position('U' IN NEW.medium) > 0 THEN -- existing notification updated
IF OLD.is_seen AND NOT NEW.is_seen THEN -- becoming unseen again
PERFORM temba_insert_notificationcount(NEW.org_id, NEW.user_id, 1);
ELSIF NOT OLD.is_seen AND NEW.is_seen THEN -- becoming seen
PERFORM temba_insert_notificationcount(NEW.org_id, NEW.user_id, -1);
END IF;
ELSIF TG_OP = 'DELETE' AND position('U' IN OLD.medium) > 0 AND NOT OLD.is_seen THEN -- existing notification deleted
PERFORM temba_insert_notificationcount(OLD.org_id, OLD.user_id, -1);
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

----------------------------------------------------------------------
-- Handles DELETE statements on notification table
----------------------------------------------------------------------
Expand Down
7 changes: 1 addition & 6 deletions temba/sql/current_triggers.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Generated by collect_sql on 2024-11-05 15:35 UTC
-- Generated by collect_sql on 2024-11-05 18:36 UTC

CREATE TRIGGER temba_broadcast_on_delete
AFTER DELETE ON msgs_broadcast REFERENCING OLD TABLE AS oldtab
Expand Down Expand Up @@ -98,11 +98,6 @@ CREATE TRIGGER temba_notification_on_update
AFTER UPDATE ON notifications_notification REFERENCING OLD TABLE AS oldtab NEW TABLE AS newtab
FOR EACH STATEMENT EXECUTE PROCEDURE temba_notification_on_update();

CREATE TRIGGER temba_notifications_update_notificationcount
AFTER INSERT OR UPDATE OF is_seen OR DELETE
ON notifications_notification
FOR EACH ROW EXECUTE PROCEDURE temba_notification_on_change();

CREATE TRIGGER temba_ticket_on_change_trg
AFTER INSERT OR UPDATE OR DELETE ON tickets_ticket
FOR EACH ROW EXECUTE PROCEDURE temba_ticket_on_change();
Expand Down

0 comments on commit fafeb99

Please sign in to comment.