From: Bill Erickson Date: Fri, 30 Mar 2018 15:11:40 +0000 (-0400) Subject: JBAS-2007 Vandelay import/overlay updates edit date X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=6e103fbbdfd25ab4e03b31c90db2981c605c3693;p=working%2FEvergreen.git JBAS-2007 Vandelay import/overlay updates edit date Configure all merge profiles, except for match-only merge, to result in an update of the bib record editor (when available) and edit date values. Recover KCLS customization that caused edit_date to be updated regardless of whether an editor value is supplied. Signed-off-by: Bill Erickson --- diff --git a/KCLS/sql/schema/deploy/vand-import-edit-date-fix.sql b/KCLS/sql/schema/deploy/vand-import-edit-date-fix.sql new file mode 100644 index 0000000000..f4f89a13a4 --- /dev/null +++ b/KCLS/sql/schema/deploy/vand-import-edit-date-fix.sql @@ -0,0 +1,88 @@ +-- Deploy kcls-evergreen:vand-import-edit-date-fix to pg +-- requires: hold-queue-pos-materialized + +BEGIN; + +-- All merge profiles result in bib editor/edit-date updates, except +-- match-only merge. +UPDATE vandelay.merge_profile + SET update_bib_source = TRUE WHERE id NOT IN (112); + +CREATE OR REPLACE FUNCTION vandelay.overlay_bib_record + ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$ +DECLARE + editor_string TEXT; + editor_id INT; + v_marc TEXT; + v_bib_source INT; + update_fields TEXT[]; + update_query TEXT; + update_bib BOOL; +BEGIN + + SELECT q.marc, q.bib_source INTO v_marc, v_bib_source + FROM vandelay.queued_bib_record q + JOIN vandelay.bib_match m ON (m.queued_record = q.id AND q.id = import_id) + LIMIT 1; + + IF v_marc IS NULL THEN + -- RAISE NOTICE 'no marc for vandelay or bib record'; + RETURN FALSE; + END IF; + + IF vandelay.template_overlay_bib_record( v_marc, eg_id, merge_profile_id) THEN + UPDATE vandelay.queued_bib_record + SET imported_as = eg_id, + import_time = NOW() + WHERE id = import_id; + + SELECT q.update_bib_source INTO update_bib FROM vandelay.merge_profile q where q.id = merge_profile_id; + + IF update_bib THEN + editor_string := (oils_xpath('//*[@tag="905"]/*[@code="u"]/text()',v_marc))[1]; + + IF editor_string IS NOT NULL AND editor_string <> '' THEN + SELECT usr INTO editor_id FROM actor.card WHERE barcode = editor_string; + + IF editor_id IS NULL THEN + SELECT id INTO editor_id FROM actor.usr WHERE usrname = editor_string; + END IF; + + IF editor_id IS NOT NULL THEN + --only update the edit date if we have a valid editor + -- KCLS always wants edit_date to be updated + -- update_fields := ARRAY_APPEND(update_fields, 'editor = ' || editor_id || ', edit_date = NOW()'); + update_fields := ARRAY_APPEND(update_fields, 'editor = ' || editor_id); + -- /KCLS + END IF; + + END IF; + + -- KCLS always wants edit_date to be updated + update_fields := ARRAY_APPEND(update_fields, 'edit_date = NOW()'); + -- /KCLS + + IF v_bib_source IS NOT NULL THEN + update_fields := ARRAY_APPEND(update_fields, 'source = ' || v_bib_source); + END IF; + + IF ARRAY_LENGTH(update_fields, 1) > 0 THEN + update_query := 'UPDATE biblio.record_entry SET ' || ARRAY_TO_STRING(update_fields, ',') || ' WHERE id = ' || eg_id || ';'; + --RAISE NOTICE 'query: %', update_query; + EXECUTE update_query; + END IF; + END IF; + + RETURN TRUE; + END IF; + + -- RAISE NOTICE 'update of biblio.record_entry failed'; + + RETURN FALSE; + +END; +$$ LANGUAGE PLPGSQL; + + + +COMMIT; diff --git a/KCLS/sql/schema/revert/vand-import-edit-date-fix.sql b/KCLS/sql/schema/revert/vand-import-edit-date-fix.sql new file mode 100644 index 0000000000..036b6a8ae0 --- /dev/null +++ b/KCLS/sql/schema/revert/vand-import-edit-date-fix.sql @@ -0,0 +1,76 @@ +-- Revert kcls-evergreen:vand-import-edit-date-fix from pg + +BEGIN; + +-- revert to original 2.12 state. +UPDATE vandelay.merge_profile SET update_bib_source = FALSE; + +CREATE OR REPLACE FUNCTION vandelay.overlay_bib_record + ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$ +DECLARE + editor_string TEXT; + editor_id INT; + v_marc TEXT; + v_bib_source INT; + update_fields TEXT[]; + update_query TEXT; + update_bib BOOL; +BEGIN + + SELECT q.marc, q.bib_source INTO v_marc, v_bib_source + FROM vandelay.queued_bib_record q + JOIN vandelay.bib_match m ON (m.queued_record = q.id AND q.id = import_id) + LIMIT 1; + + IF v_marc IS NULL THEN + -- RAISE NOTICE 'no marc for vandelay or bib record'; + RETURN FALSE; + END IF; + + IF vandelay.template_overlay_bib_record( v_marc, eg_id, merge_profile_id) THEN + UPDATE vandelay.queued_bib_record + SET imported_as = eg_id, + import_time = NOW() + WHERE id = import_id; + + SELECT q.update_bib_source INTO update_bib FROM vandelay.merge_profile q where q.id = merge_profile_id; + + IF update_bib THEN + editor_string := (oils_xpath('//*[@tag="905"]/*[@code="u"]/text()',v_marc))[1]; + + IF editor_string IS NOT NULL AND editor_string <> '' THEN + SELECT usr INTO editor_id FROM actor.card WHERE barcode = editor_string; + + IF editor_id IS NULL THEN + SELECT id INTO editor_id FROM actor.usr WHERE usrname = editor_string; + END IF; + + IF editor_id IS NOT NULL THEN + --only update the edit date if we have a valid editor + update_fields := ARRAY_APPEND(update_fields, 'editor = ' || editor_id || ', edit_date = NOW()'); + END IF; + END IF; + + IF v_bib_source IS NOT NULL THEN + update_fields := ARRAY_APPEND(update_fields, 'source = ' || v_bib_source); + END IF; + + IF ARRAY_LENGTH(update_fields, 1) > 0 THEN + update_query := 'UPDATE biblio.record_entry SET ' || ARRAY_TO_STRING(update_fields, ',') || ' WHERE id = ' || eg_id || ';'; + --RAISE NOTICE 'query: %', update_query; + EXECUTE update_query; + END IF; + END IF; + + RETURN TRUE; + END IF; + + -- RAISE NOTICE 'update of biblio.record_entry failed'; + + RETURN FALSE; + +END; +$$ LANGUAGE PLPGSQL; + + +COMMIT; diff --git a/KCLS/sql/schema/sqitch.plan b/KCLS/sql/schema/sqitch.plan index 60e8de9b58..cc09f9fc59 100644 --- a/KCLS/sql/schema/sqitch.plan +++ b/KCLS/sql/schema/sqitch.plan @@ -64,3 +64,4 @@ outreach-si-profile [at-purge-interval-data] 2018-02-06T15:18:44Z Bill Erickson, 2.10-to-2.12-upgrade [at-purge-interval-data] 2018-01-29T21:03:38Z Bill Erickson,,, # KCLS 2.10 to 2.12 SQL Upgrade hold-queue-pos-materialized [2.10-to-2.12-upgrade] 2018-03-25T18:50:51Z Bill Erickson,,, # materialized hold queue posistions search-strip-apos-recover [hold-queue-pos-materialized] 2018-03-30T16:15:21Z Bill Erickson,,, # Recover lost apostrophe stripping in search +vand-import-edit-date-fix [hold-queue-pos-materialized] 2018-03-30T14:50:53Z Bill Erickson,,, # Recover lost vandelay edit date changes diff --git a/KCLS/sql/schema/verify/vand-import-edit-date-fix.sql b/KCLS/sql/schema/verify/vand-import-edit-date-fix.sql new file mode 100644 index 0000000000..97ce27004b --- /dev/null +++ b/KCLS/sql/schema/verify/vand-import-edit-date-fix.sql @@ -0,0 +1,7 @@ +-- Verify kcls-evergreen:vand-import-edit-date-fix on pg + +BEGIN; + +-- XXX Add verifications here. + +ROLLBACK;