JBAS-1419 CO history deployment SQL tweaks
authorBill Erickson <berickxx@gmail.com>
Wed, 31 Aug 2016 15:00:25 +0000 (11:00 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/sql/schema/deploy/patron-co-history-table.sql
KCLS/sql/schema/revert/aged-circs-api.sql
KCLS/sql/schema/revert/patron-co-history-table.sql

index c364bd9..882c9a4 100644 (file)
@@ -22,11 +22,6 @@ CREATE TABLE action.usr_circ_history (
                  ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED
 );
 
-CREATE TRIGGER action_usr_circ_history_target_copy_trig
-    AFTER INSERT OR UPDATE ON action.usr_circ_history
-    FOR EACH ROW EXECUTE PROCEDURE evergreen.fake_fkey_tgr('target_copy');
-
-CREATE INDEX action_usr_circ_history_usr_idx ON action.usr_circ_history (usr);
 
 CREATE OR REPLACE FUNCTION action.maintain_usr_circ_history() 
     RETURNS TRIGGER AS $FUNK$
@@ -100,6 +95,7 @@ BEGIN
 END;                                                                           
 $FUNK$ LANGUAGE PLPGSQL; 
 
+-- Create TRIGGER after migration
 CREATE TRIGGER maintain_usr_circ_history_tgr 
     AFTER INSERT OR UPDATE ON action.circulation 
     FOR EACH ROW EXECUTE PROCEDURE action.maintain_usr_circ_history();
@@ -203,27 +199,33 @@ $$;
 -- no modifications are required.
 
 -- Create circ history rows for existing circ history data.
-DO $FUNK$
+CREATE OR REPLACE FUNCTION action.migrate_usr_circ_history
+    (mod_factor INT, mod_slot INT) RETURNS VOID AS $FUNK$
 DECLARE
     cur_usr   INTEGER;
     cur_circ  action.circulation%ROWTYPE;
     last_circ action.circulation%ROWTYPE;
-    counter   INTEGER DEFAULT 1;
+    circ_counter  INTEGER DEFAULT 1;
+    usr_counter  INTEGER DEFAULT 1;
 BEGIN
 
     RAISE NOTICE 
         'Migrating circ history for % users.  This might take a while...',
         (SELECT COUNT(DISTINCT(au.id)) FROM actor.usr au
             JOIN actor.usr_setting aus ON (aus.usr = au.id)
-            WHERE NOT au.deleted AND 
-                aus.name ~ '^history.circ.retention_');
+            WHERE 
+                NOT au.deleted 
+                AND aus.name ~ '^history.circ.retention_'
+                AND (au.id % mod_factor) = mod_slot);
 
     FOR cur_usr IN 
         SELECT DISTINCT(au.id)
             FROM actor.usr au 
             JOIN actor.usr_setting aus ON (aus.usr = au.id)
-            WHERE NOT au.deleted AND 
-                aus.name ~ '^history.circ.retention_' LOOP
+            WHERE 
+                NOT au.deleted 
+                AND aus.name ~ '^history.circ.retention_' 
+                AND (au.id % mod_factor) = mod_slot LOOP
 
         FOR cur_circ IN SELECT * FROM action.usr_visible_circs(cur_usr) LOOP
 
@@ -255,19 +257,25 @@ BEGIN
             );
 
             -- useful for alleviating administrator anxiety.
-            IF counter % 10000 = 0 THEN
-                RAISE NOTICE 'Migrated history for % total users', counter;
+            IF circ_counter % 10000 = 0 THEN
+                RAISE NOTICE 'Migrated history for % total circs', circ_counter;
             END IF;
 
-            counter := counter + 1;
+            circ_counter := circ_counter + 1;
 
         END LOOP;
+
+         -- useful for alleviating administrator anxiety.
+        IF usr_counter % 1000 = 0 THEN
+            RAISE NOTICE 'Migrated history for % total users', usr_counter;
+        END IF;
+
+        usr_counter := usr_counter + 1;
+
     END LOOP;
 
-END $FUNK$;
+END $FUNK$ LANGUAGE PLPGSQL;
 
-DROP FUNCTION IF EXISTS action.usr_visible_circs (INTEGER);
-DROP FUNCTION IF EXISTS action.usr_visible_circ_copies (INTEGER);
 
 CREATE OR REPLACE FUNCTION action.purge_circulations_custom () RETURNS INT AS $func$
 DECLARE
@@ -769,3 +777,20 @@ END;
 $$ LANGUAGE plpgsql;
 
 COMMIT;
+
+-- APPLY THESE POST-MIGRATION
+
+/*
+
+CREATE TRIGGER action_usr_circ_history_target_copy_trig
+   AFTER INSERT OR UPDATE ON action.usr_circ_history
+  FOR EACH ROW EXECUTE PROCEDURE evergreen.fake_fkey_tgr('target_copy');
+
+
+DROP FUNCTION IF EXISTS action.usr_visible_circs (INTEGER);
+DROP FUNCTION IF EXISTS action.usr_visible_circ_copies (INTEGER);
+
+CREATE INDEX action_usr_circ_history_usr_idx ON action.usr_circ_history (usr);
+
+*/
+
index 7694511..551bc87 100644 (file)
@@ -53,4 +53,6 @@ UNION ALL
    LEFT JOIN actor.usr_address b ON p.billing_address = b.id;
 
 
+DELETE FROM config.upgrade_log WHERE version = '0998';  
+
 COMMIT;
index 5a8c969..821ac3b 100644 (file)
@@ -632,4 +632,8 @@ DROP TRIGGER maintain_usr_circ_history_tgr ON action.circulation;
 DROP FUNCTION action.maintain_usr_circ_history();
 DROP TABLE action.usr_circ_history;
 
+DROP FUNCTION action.migrate_usr_circ_history(INT, INT);
+
+DELETE FROM config.upgrade_log WHERE version = '0960';
+
 COMMIT;