From: Bill Erickson Date: Wed, 31 Aug 2016 15:00:25 +0000 (-0400) Subject: JBAS-1419 CO history deployment SQL tweaks X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=6f33d441d0b2781c3d8ffd1b8adb05f96b08cc7c;p=working%2FEvergreen.git JBAS-1419 CO history deployment SQL tweaks Signed-off-by: Bill Erickson --- diff --git a/KCLS/sql/schema/deploy/patron-co-history-table.sql b/KCLS/sql/schema/deploy/patron-co-history-table.sql index c364bd9cd6..882c9a409d 100644 --- a/KCLS/sql/schema/deploy/patron-co-history-table.sql +++ b/KCLS/sql/schema/deploy/patron-co-history-table.sql @@ -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); + +*/ + diff --git a/KCLS/sql/schema/revert/aged-circs-api.sql b/KCLS/sql/schema/revert/aged-circs-api.sql index 76945116ac..551bc87204 100644 --- a/KCLS/sql/schema/revert/aged-circs-api.sql +++ b/KCLS/sql/schema/revert/aged-circs-api.sql @@ -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; diff --git a/KCLS/sql/schema/revert/patron-co-history-table.sql b/KCLS/sql/schema/revert/patron-co-history-table.sql index 5a8c969002..821ac3b255 100644 --- a/KCLS/sql/schema/revert/patron-co-history-table.sql +++ b/KCLS/sql/schema/revert/patron-co-history-table.sql @@ -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;