]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0993.schema.usr_activity_transient.sql
LP1894131 Sticky catalog holdings org select
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0993.schema.usr_activity_transient.sql
1
2 BEGIN;
3
4 SELECT evergreen.upgrade_deps_block_check('0993', :eg_version);
5
6 ALTER TABLE config.usr_activity_type 
7     ALTER COLUMN transient SET DEFAULT TRUE;
8
9 -- Utility function for removing all activity entries by activity type,
10 -- except for the most recent entry per user.  This is primarily useful
11 -- when cleaning up rows prior to setting the transient flag on an
12 -- activity type to true.  It allows for immediate cleanup of data (e.g.
13 -- for patron privacy) and lets admins control when the data is deleted,
14 -- which could be useful for huge activity tables.
15
16 CREATE OR REPLACE FUNCTION 
17     actor.purge_usr_activity_by_type(act_type INTEGER) 
18     RETURNS VOID AS $$
19 DECLARE
20     cur_usr INTEGER;
21 BEGIN
22     FOR cur_usr IN SELECT DISTINCT(usr) 
23         FROM actor.usr_activity WHERE etype = act_type LOOP
24         DELETE FROM actor.usr_activity WHERE id IN (
25             SELECT id 
26             FROM actor.usr_activity 
27             WHERE usr = cur_usr AND etype = act_type
28             ORDER BY event_time DESC OFFSET 1
29         );
30
31     END LOOP;
32 END $$ LANGUAGE PLPGSQL;
33
34 COMMIT;
35