Purge Circulations: Support last xact_finished use
authorThomas Berezansky <tsbere@mvlc.org>
Mon, 18 Jun 2012 17:00:30 +0000 (13:00 -0400)
committerDan Wells <dbw2@calvin.edu>
Fri, 7 Jun 2013 19:49:45 +0000 (15:49 -0400)
Based on a new (off by default) global flag change between the xact_finish
on the last circ in the chain and the most recent one.

Signed-off-by: Thomas Berezansky <tsbere@mvlc.org>
Signed-off-by: Jason Stephenson <jstephenson@mvlc.org>
Open-ILS/src/sql/Pg/090.schema.action.sql
Open-ILS/src/sql/Pg/950.data.seed-values.sql

index 293aafb..5067242 100644 (file)
@@ -977,6 +977,7 @@ DECLARE
     usr_keep_age    actor.usr_setting%ROWTYPE;
     usr_keep_start  actor.usr_setting%ROWTYPE;
     org_keep_age    INTERVAL;
+    org_use_last    BOOL = false;
     org_keep_count  INT;
 
     keep_age        INTERVAL;
@@ -988,6 +989,8 @@ DECLARE
     purge_position  INT;
     count_purged    INT;
     num_incomplete  INT;
+
+    last_finished   TIMESTAMP WITH TIME ZONE;
 BEGIN
 
     count_purged := 0;
@@ -999,6 +1002,8 @@ BEGIN
         RETURN count_purged; -- Gimme a count to keep, or I keep them all, forever
     END IF;
 
+    SELECT enabled INTO org_use_last FROM config.global_flag WHERE name = 'history.circ.retention_uses_last_finished';
+
     -- First, find copies with more than keep_count non-renewal circs
     FOR target_acp IN
         SELECT  target_copy,
@@ -1023,9 +1028,13 @@ BEGIN
             EXIT WHEN target_acp.total_real_circs - purge_position <= org_keep_count;
 
             SELECT * INTO circ_chain_tail FROM action.circ_chain(circ_chain_head.id) ORDER BY xact_start DESC LIMIT 1;
-            SELECT COUNT(CASE WHEN xact_finish IS NULL THEN 1 ELSE NULL END) INTO num_incomplete FROM action.circ_chain(circ_chain_head.id);
+            SELECT COUNT(CASE WHEN xact_finish IS NULL THEN 1 ELSE NULL END), MAX(xact_finish) INTO num_incomplete, last_finished FROM action.circ_chain(circ_chain_head.id);
             EXIT WHEN circ_chain_tail.xact_finish IS NULL OR num_incomplete > 0;
 
+            IF NOT org_use_last THEN
+                last_finished := circ_chain_tail.xact_finish;
+            END IF;
+
             -- Now get the user settings, if any, to block purging if the user wants to keep more circs
             usr_keep_age.value := NULL;
             SELECT * INTO usr_keep_age FROM actor.usr_setting WHERE usr = circ_chain_head.usr AND name = 'history.circ.retention_age';
@@ -1045,7 +1054,7 @@ BEGIN
                 keep_age := COALESCE( org_keep_age::INTERVAL, '2000 years'::INTERVAL );
             END IF;
 
-            EXIT WHEN AGE(NOW(), circ_chain_tail.xact_finish) < keep_age;
+            EXIT WHEN AGE(NOW(), last_finished) < keep_age;
 
             -- We've passed the purging tests, purge the circ chain starting at the end
             DELETE FROM action.circulation WHERE id = circ_chain_tail.id;
index 43bc29a..6fc8f20 100644 (file)
@@ -9080,6 +9080,16 @@ INSERT INTO config.global_flag (name, label, value, enabled)
         TRUE
     );
 
+INSERT INTO config.global_flag (name, label)
+    VALUES (
+        'history.circ.rentention_uses_last_finished',
+        oils_i18n_gettext(
+            'history.circ.retention_uses_last_finished',
+            'Historical Circulations use most recent xact_finish date instead of last circ''s.',
+            'cgf',
+            'label'
+        )
+    );
 
 INSERT INTO config.usr_setting_type (name,opac_visible,label,description,datatype)
     VALUES (