LP#1570909 User activity purge function
authorBill Erickson <berickxx@gmail.com>
Fri, 15 Apr 2016 20:26:57 +0000 (16:26 -0400)
committerChris Sharp <csharp@georgialibraries.org>
Tue, 16 Aug 2016 21:41:11 +0000 (17:41 -0400)
Utility function for removing all activity entries by activity type,
except for the most recent entry per user.  This is primarily useful
when cleaning up rows prior to setting the transient flag on an
activity type to true.  It allows for immediate cleanup of data (e.g.
for patron privacy) and lets admins control when the data is deleted,
which could be useful for huge activity tables.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Chris Sharp <csharp@georgialibraries.org>
Open-ILS/src/sql/Pg/002.schema.config.sql
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.usr_activity_transient.sql

index de67b41..a995284 100644 (file)
@@ -1010,6 +1010,26 @@ CREATE TABLE config.usr_activity_type (
     CONSTRAINT  one_of_wwh CHECK (COALESCE(ewho,ewhat,ehow) IS NOT NULL)
 );
 
+-- Remove all activity entries by activity type, 
+-- except the most recent entry per user. 
+CREATE OR REPLACE FUNCTION 
+    actor.purge_usr_activity_by_type(act_type INTEGER) 
+    RETURNS VOID AS $$
+DECLARE
+    cur_usr INTEGER;
+BEGIN
+    FOR cur_usr IN SELECT DISTINCT(usr) 
+        FROM actor.usr_activity WHERE etype = act_type LOOP
+        DELETE FROM actor.usr_activity WHERE id IN (
+            SELECT id 
+            FROM actor.usr_activity 
+            WHERE usr = cur_usr AND etype = act_type
+            ORDER BY event_time DESC OFFSET 1
+        );
+
+    END LOOP;
+END $$ LANGUAGE PLPGSQL;
+
 CREATE UNIQUE INDEX unique_wwh ON config.usr_activity_type 
     (COALESCE(ewho,''), COALESCE (ewhat,''), COALESCE(ehow,''));
 
index 02d4a8b..fb70a87 100644 (file)
@@ -6,5 +6,30 @@ SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
 ALTER TABLE config.usr_activity_type 
     ALTER COLUMN transient SET DEFAULT TRUE;
 
+-- Utility function for removing all activity entries by activity type,
+-- except for the most recent entry per user.  This is primarily useful
+-- when cleaning up rows prior to setting the transient flag on an
+-- activity type to true.  It allows for immediate cleanup of data (e.g.
+-- for patron privacy) and lets admins control when the data is deleted,
+-- which could be useful for huge activity tables.
+
+CREATE OR REPLACE FUNCTION 
+    actor.purge_usr_activity_by_type(act_type INTEGER) 
+    RETURNS VOID AS $$
+DECLARE
+    cur_usr INTEGER;
+BEGIN
+    FOR cur_usr IN SELECT DISTINCT(usr) 
+        FROM actor.usr_activity WHERE etype = act_type LOOP
+        DELETE FROM actor.usr_activity WHERE id IN (
+            SELECT id 
+            FROM actor.usr_activity 
+            WHERE usr = cur_usr AND etype = act_type
+            ORDER BY event_time DESC OFFSET 1
+        );
+
+    END LOOP;
+END $$ LANGUAGE PLPGSQL;
+
 COMMIT;