From: Thomas Berezansky <tsbere@mvlc.org> Date: Mon, 9 Jan 2012 18:58:07 +0000 (-0500) Subject: Speed up 0663 upgrade script X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=148c06c17527bac8d37dae5f4d1d9b2ea9011405;p=evergreen%2Fmasslnc.git Speed up 0663 upgrade script Move update to the end This ensures most of the rest goes without error first Disable triggers on the table before doing update This speeds the update up significantly as we don't need to re-calc the additional column's new data. Allow new column to be null (at first) with no default This allows the column to be added *without* re-writing every row. We still re-write every row with the update, but at least we aren't doing that *twice* this way. And add the not null/default to new column and re-enable triggers as our last action. Signed-off-by: Thomas Berezansky <tsbere@mvlc.org> Signed-off-by: Jason Stephenson <jstephenson@mvlc.org> --- diff --git a/Open-ILS/src/sql/Pg/upgrade/0663.schema.archive_circ_stat_cats.sql b/Open-ILS/src/sql/Pg/upgrade/0663.schema.archive_circ_stat_cats.sql index 022b2ddd70..192b359814 100644 --- a/Open-ILS/src/sql/Pg/upgrade/0663.schema.archive_circ_stat_cats.sql +++ b/Open-ILS/src/sql/Pg/upgrade/0663.schema.archive_circ_stat_cats.sql @@ -34,10 +34,7 @@ ALTER TABLE asset.stat_cat -- Circulation copy column ALTER TABLE action.circulation - ADD COLUMN copy_location INT NOT NULL DEFAULT 1 REFERENCES asset.copy_location(id) DEFERRABLE INITIALLY DEFERRED; - --- Update action.circulation with real copy_location numbers instead of all "Stacks" -UPDATE action.circulation circ SET copy_location = ac.location FROM asset.copy ac WHERE ac.id = circ.target_copy; + ADD COLUMN copy_location INT NULL REFERENCES asset.copy_location(id) DEFERRABLE INITIALLY DEFERRED; -- Create trigger function to auto-fill the copy_location field CREATE OR REPLACE FUNCTION action.fill_circ_copy_location () RETURNS TRIGGER AS $$ @@ -69,6 +66,9 @@ $$ LANGUAGE PLPGSQL; CREATE TRIGGER fill_circ_copy_location_tgr BEFORE INSERT ON action.circulation FOR EACH ROW EXECUTE PROCEDURE action.fill_circ_copy_location(); CREATE TRIGGER archive_stat_cats_tgr AFTER INSERT ON action.circulation FOR EACH ROW EXECUTE PROCEDURE action.archive_stat_cats(); +-- Ensure all triggers are disabled for speedy updates! +ALTER TABLE action.circulation DISABLE TRIGGER ALL; + -- Update view to use circ's copy_location field instead of the copy's current copy_location field CREATE OR REPLACE VIEW action.all_circulation AS SELECT id,usr_post_code, usr_home_ou, usr_profile, usr_birth_year, copy_call_number, copy_location, @@ -93,4 +93,14 @@ CREATE OR REPLACE VIEW action.all_circulation AS LEFT JOIN actor.usr_address a ON (p.mailing_address = a.id) LEFT JOIN actor.usr_address b ON (p.billing_address = b.id); +-- Update action.circulation with real copy_location numbers instead of all NULL +DO $$BEGIN RAISE WARNING 'We are about to do an update on every row in action.circulation. This may take a while. %', timeofday(); END;$$; +UPDATE action.circulation circ SET copy_location = ac.location FROM asset.copy ac WHERE ac.id = circ.target_copy; + +-- Set not null/default on new column, re-enable triggers +ALTER TABLE action.circulation + ALTER COLUMN copy_location SET NOT NULL, + ALTER COLUMN copy_location SET DEFAULT 1, + ENABLE TRIGGER ALL; + COMMIT;