From: Bill Erickson Date: Wed, 1 Jun 2016 18:37:10 +0000 (-0400) Subject: JBAS-1417 Vand. authority import set edit date SQL X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=7cfe1d86aec151661d2c8d82c302d4af95d4545b;p=working%2FEvergreen.git JBAS-1417 Vand. authority import set edit date SQL Signed-off-by: Bill Erickson --- diff --git a/KCLS/sql/schema/deploy/vand-auth-edit-date.sql b/KCLS/sql/schema/deploy/vand-auth-edit-date.sql new file mode 100644 index 0000000000..8eaaffffed --- /dev/null +++ b/KCLS/sql/schema/deploy/vand-auth-edit-date.sql @@ -0,0 +1,122 @@ +-- Deploy kcls-evergreen:vand-auth-edit-date to pg +-- requires: purge-user-activity + +BEGIN; + +CREATE OR REPLACE FUNCTION vandelay.overlay_authority_record ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$ +DECLARE + merge_profile vandelay.merge_profile%ROWTYPE; + dyn_profile vandelay.compile_profile%ROWTYPE; + editor_string TEXT; + editor_id INT; + source_marc TEXT; + target_marc TEXT; + eg_marc TEXT; + v_marc TEXT; + replace_rule TEXT; + match_count INT; + update_fields TEXT[]; + update_query TEXT; +BEGIN + + SELECT b.marc INTO eg_marc + FROM authority.record_entry b + JOIN vandelay.authority_match m ON (m.eg_record = b.id AND m.queued_record = import_id) + LIMIT 1; + + SELECT q.marc INTO v_marc + FROM vandelay.queued_record q + JOIN vandelay.authority_match m ON (m.queued_record = q.id AND q.id = import_id) + LIMIT 1; + + IF eg_marc IS NULL OR v_marc IS NULL THEN + -- RAISE NOTICE 'no marc for vandelay or authority record'; + RETURN FALSE; + END IF; + + -- Extract the editor string before any modification to the vandelay + -- MARC occur. + editor_string := + (oils_xpath('//*[@tag="905"]/*[@code="u"]/text()',v_marc))[1]; + + dyn_profile := vandelay.compile_profile( v_marc ); + + IF merge_profile_id IS NOT NULL THEN + SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id; + IF FOUND THEN + dyn_profile.add_rule := BTRIM( dyn_profile.add_rule || ',' || COALESCE(merge_profile.add_spec,''), ','); + dyn_profile.strip_rule := BTRIM( dyn_profile.strip_rule || ',' || COALESCE(merge_profile.strip_spec,''), ','); + dyn_profile.replace_rule := BTRIM( dyn_profile.replace_rule || ',' || COALESCE(merge_profile.replace_spec,''), ','); + dyn_profile.preserve_rule := BTRIM( dyn_profile.preserve_rule || ',' || COALESCE(merge_profile.preserve_spec,''), ','); + END IF; + END IF; + + IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN + -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule; + RETURN FALSE; + END IF; + + IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' AND dyn_profile.strip_rule = '' THEN + --Since we have nothing to do, just return a NOOP "we did it" + RETURN TRUE; + ELSIF dyn_profile.replace_rule <> '' THEN + source_marc = v_marc; + target_marc = eg_marc; + replace_rule = dyn_profile.replace_rule; + ELSE + source_marc = eg_marc; + target_marc = v_marc; + replace_rule = dyn_profile.preserve_rule; + END IF; + + UPDATE authority.record_entry + SET marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule ) + WHERE id = eg_id; + + IF NOT FOUND THEN + -- Import/merge failed. Nothing left to do. + RETURN FALSE; + END IF; + + -- Authority record successfully merged / imported. + + -- Update the vandelay record to show the successful import. + UPDATE vandelay.queued_authority_record + SET imported_as = eg_id, + import_time = NOW() + WHERE id = import_id; + + -- If an editor value can be found, update the authority record + -- editor and edit_date values. + IF editor_string IS NOT NULL AND editor_string <> '' THEN + + -- Vandelay.pm sets the value to 'usrname' when needed. + SELECT id INTO editor_id + FROM actor.usr WHERE usrname = editor_string; + + IF editor_id IS NULL THEN + SELECT usr INTO editor_id + FROM actor.card WHERE barcode = 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 ARRAY_LENGTH(update_fields, 1) > 0 THEN + update_query := 'UPDATE authority.record_entry SET ' || + ARRAY_TO_STRING(update_fields, ',') || + ' WHERE id = ' || eg_id || ';'; + --RAISE NOTICE 'query: %', update_query; + EXECUTE update_query; + END IF; + + RETURN TRUE; + +END; +$$ LANGUAGE PLPGSQL; + +COMMIT; diff --git a/KCLS/sql/schema/revert/vand-auth-edit-date.sql b/KCLS/sql/schema/revert/vand-auth-edit-date.sql new file mode 100644 index 0000000000..ea13eefae1 --- /dev/null +++ b/KCLS/sql/schema/revert/vand-auth-edit-date.sql @@ -0,0 +1,84 @@ +-- Revert kcls-evergreen:vand-auth-edit-date from pg + +BEGIN; + +CREATE FUNCTION overlay_authority_record(import_id bigint, eg_id bigint, merge_profile_id integer) RETURNS boolean + LANGUAGE plpgsql + AS $$ + DECLARE + merge_profile vandelay.merge_profile%ROWTYPE; + dyn_profile vandelay.compile_profile%ROWTYPE; + source_marc TEXT; + target_marc TEXT; + eg_marc TEXT; + v_marc TEXT; + replace_rule TEXT; + match_count INT; + BEGIN + + SELECT b.marc INTO eg_marc + FROM authority.record_entry b + JOIN vandelay.authority_match m ON (m.eg_record = b.id AND m.queued_record = import_id) + LIMIT 1; + + SELECT q.marc INTO v_marc + FROM vandelay.queued_record q + JOIN vandelay.authority_match m ON (m.queued_record = q.id AND q.id = import_id) + LIMIT 1; + + IF eg_marc IS NULL OR v_marc IS NULL THEN + -- RAISE NOTICE 'no marc for vandelay or authority record'; + RETURN FALSE; + END IF; + + dyn_profile := vandelay.compile_profile( v_marc ); + + IF merge_profile_id IS NOT NULL THEN + SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id; + IF FOUND THEN + dyn_profile.add_rule := BTRIM( dyn_profile.add_rule || ',' || COALESCE(merge_profile.add_spec,''), ','); + dyn_profile.strip_rule := BTRIM( dyn_profile.strip_rule || ',' || COALESCE(merge_profile.strip_spec,''), ','); + dyn_profile.replace_rule := BTRIM( dyn_profile.replace_rule || ',' || COALESCE(merge_profile.replace_spec,''), ','); + dyn_profile.preserve_rule := BTRIM( dyn_profile.preserve_rule || ',' || COALESCE(merge_profile.preserve_spec,''), ','); + END IF; + END IF; + + IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN + -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule; + RETURN FALSE; + END IF; + + IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' AND dyn_profile.strip_rule = '' THEN + --Since we have nothing to do, just return a NOOP "we did it" + RETURN TRUE; + ELSIF dyn_profile.replace_rule <> '' THEN + source_marc = v_marc; + target_marc = eg_marc; + replace_rule = dyn_profile.replace_rule; + ELSE + source_marc = eg_marc; + target_marc = v_marc; + replace_rule = dyn_profile.preserve_rule; + END IF; + + UPDATE authority.record_entry + SET marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule ) + WHERE id = eg_id; + + IF FOUND THEN + UPDATE vandelay.queued_authority_record + SET imported_as = eg_id, + import_time = NOW() + WHERE id = import_id; + RETURN TRUE; + END IF; + + -- RAISE NOTICE 'update of authority.record_entry failed'; + + RETURN FALSE; + + END; +$$ LANGUAGE PLPGSQL; + + +COMMIT; diff --git a/KCLS/sql/schema/sqitch.plan b/KCLS/sql/schema/sqitch.plan index cf3e77ddea..a17cdbcaa4 100644 --- a/KCLS/sql/schema/sqitch.plan +++ b/KCLS/sql/schema/sqitch.plan @@ -26,3 +26,4 @@ org-unit-addrs-copy [sip-activity-types] 2016-05-03T14:17:59Z Bill Erickson # Drop unneeded CC payment columns connexion-auth-imports [sip-activity-types] 2016-05-11T15:10:49Z Bill Erickson,,, # OCLC Connexion Authority Record Imports Data purge-user-activity [sip-activity-types] 2016-04-29T17:07:46Z Bill Erickson # Clean up actor.usr_activity +vand-auth-edit-date [purge-user-activity] 2016-06-01T18:24:54Z Bill Erickson # Vandelay authority import sets edit[or|_date] diff --git a/KCLS/sql/schema/verify/vand-auth-edit-date.sql b/KCLS/sql/schema/verify/vand-auth-edit-date.sql new file mode 100644 index 0000000000..7535d389dc --- /dev/null +++ b/KCLS/sql/schema/verify/vand-auth-edit-date.sql @@ -0,0 +1,7 @@ +-- Verify kcls-evergreen:vand-auth-edit-date on pg + +BEGIN; + +-- XXX Add verifications here. + +ROLLBACK;