Add a procedure to delete expired circulation history items
authorscottmk <scottmk@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Tue, 16 Jun 2009 14:44:37 +0000 (14:44 +0000)
committerscottmk <scottmk@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Tue, 16 Jun 2009 14:44:37 +0000 (14:44 +0000)
in container.copy_bucket_item; also add an index so that this
operation doesn't need a full table scan.

git-svn-id: svn://svn.open-ils.org/ILS/trunk@13398 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/sql/Pg/950.data.seed-values.sql
Open-ILS/src/sql/Pg/999.functions.global.sql

index 9bbd9f6..c66bf4d 100644 (file)
@@ -1657,6 +1657,7 @@ SELECT SETVAL('vandelay.authority_attr_definition_id_seq'::TEXT, 100);
 
 INSERT INTO container.copy_bucket_type (code,label) VALUES ('misc', oils_i18n_gettext('misc', 'Miscellaneous', 'ccpbt', 'label'));
 INSERT INTO container.copy_bucket_type (code,label) VALUES ('staff_client', oils_i18n_gettext('staff_client', 'General Staff Client container', 'ccpbt', 'label'));
+INSERT INTO container.copy_bucket_type (code,label) VALUES ( 'circ_history', 'Circulation History' );
 INSERT INTO container.call_number_bucket_type (code,label) VALUES ('misc', oils_i18n_gettext('misc', 'Miscellaneous', 'ccnbt', 'label'));
 INSERT INTO container.biblio_record_entry_bucket_type (code,label) VALUES ('misc', oils_i18n_gettext('misc', 'Miscellaneous', 'cbrebt', 'label'));
 INSERT INTO container.biblio_record_entry_bucket_type (code,label) VALUES ('staff_client', oils_i18n_gettext('staff_client', 'General Staff Client container', 'cbrebt', 'label'));
index 21cd2dc..f824b19 100644 (file)
@@ -210,5 +210,60 @@ COMMENT ON FUNCTION actor.approve_pending_address(INT) IS $$
  */
 $$;
 
-
+CREATE OR REPLACE FUNCTION container.clear_expired_circ_history_items( 
+        ac_usr IN INTEGER
+) RETURNS VOID AS $$
+--
+-- Delete old circulation bucket items for a specified user.
+-- "Old" means older than the interval specified by a
+-- user-level setting, if it is so specified.
+--
+DECLARE
+    threshold TIMESTAMP WITH TIME ZONE;
+BEGIN
+       -- Sanity check
+       IF ac_usr IS NULL THEN
+               RETURN;
+       END IF;
+       -- Determine the threshold date that defines "old".  Subtract the
+       -- interval from the system date, then truncate to midnight.
+       SELECT
+               date_trunc( 
+                       'day',
+                       now() - CAST( translate( value, '"', '' ) AS INTERVAL )
+               )
+       INTO
+               threshold
+       FROM
+               actor.usr_setting
+       WHERE
+               usr = ac_usr
+               AND name = 'patron.max_reading_list_interval';
+       --
+       IF threshold is null THEN
+               -- No interval defined; don't delete anything
+               -- RAISE NOTICE 'No interval defined for user %', ac_usr;
+               return;
+       END IF;
+       --
+       -- RAISE NOTICE 'Date threshold: %', threshold;
+       --
+       -- Threshold found; do the delete
+       delete from container.copy_bucket_item
+       where
+               bucket in
+               (
+                       select
+                               id
+                       from
+                               container.copy_bucket
+                       where
+                               owner = ac_usr
+                               and btype = 'circ_history'
+               )
+               and create_time < threshold;
+       --
+       RETURN;
+END;
+$$ LANGUAGE plpgsql;