From c107c091c364e1ec8d08ddc252c34284f5fc8ac3 Mon Sep 17 00:00:00 2001 From: dbs Date: Tue, 2 Nov 2010 02:46:51 +0000 Subject: [PATCH] Enable merge of authority records to do the right thing The target and source authority record was flipped, causing the update to fail. In reconsidering this function, it is not necessary to change the contents of the source authority record just to propagate the content of the target authority record to any linked bibliographic records. Instead, take the approach of updating the ID of the controlled field in the bib record, then temporarily set "reingest on same MARC" to TRUE and update the target authority record by setting deleted = FALSE (which propagates the "changes" to the linked bib records), then set "reingest on same MARC" flag back to its original value. This has the bonus of actually working. git-svn-id: svn://svn.open-ils.org/ILS/trunk@18567 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/src/sql/Pg/002.schema.config.sql | 2 +- Open-ILS/src/sql/Pg/011.schema.authority.sql | 40 ++++++++++----- .../Pg/0454.function.authority_merge_records.sql | 60 ++++++++++++++++++++++ 3 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 Open-ILS/src/sql/Pg/0454.function.authority_merge_records.sql diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql index e68ff3538..32b31c1fb 100644 --- a/Open-ILS/src/sql/Pg/002.schema.config.sql +++ b/Open-ILS/src/sql/Pg/002.schema.config.sql @@ -70,7 +70,7 @@ CREATE TABLE config.upgrade_log ( install_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); -INSERT INTO config.upgrade_log (version) VALUES ('0453'); -- miker +INSERT INTO config.upgrade_log (version) VALUES ('0454'); -- dbs CREATE TABLE config.bib_source ( id SERIAL PRIMARY KEY, diff --git a/Open-ILS/src/sql/Pg/011.schema.authority.sql b/Open-ILS/src/sql/Pg/011.schema.authority.sql index 480a429c2..b4691f207 100644 --- a/Open-ILS/src/sql/Pg/011.schema.authority.sql +++ b/Open-ILS/src/sql/Pg/011.schema.authority.sql @@ -196,21 +196,13 @@ DECLARE bib_id INT := 0; bib_rec biblio.record_entry%ROWTYPE; auth_link authority.bib_linking%ROWTYPE; + ingest_same boolean; BEGIN - -- 1. Make source_record MARC a copy of the target_record to get auto-sync in linked bib records - UPDATE authority.record_entry - SET marc = ( - SELECT marc - FROM authority.record_entry - WHERE id = target_record - ) - WHERE id = source_record; - - -- 2. Update all bib records with the ID from target_record in their $0 + -- 1. Update all bib records with the ID from target_record in their $0 FOR bib_rec IN SELECT bre.* FROM biblio.record_entry bre INNER JOIN authority.bib_linking abl ON abl.bib = bre.id - WHERE abl.authority = target_record LOOP + WHERE abl.authority = source_record LOOP UPDATE biblio.record_entry SET marc = REGEXP_REPLACE(marc, @@ -221,10 +213,34 @@ BEGIN moved_objects := moved_objects + 1; END LOOP; - -- 3. "Delete" source_record + -- 2. Grab the current value of reingest on same MARC flag + SELECT enabled INTO ingest_same + FROM config.internal_flag + WHERE name = 'ingest.reingest.force_on_same_marc' + ; + + -- 3. Temporarily set reingest on same to TRUE + UPDATE config.internal_flag + SET enabled = TRUE + WHERE name = 'ingest.reingest.force_on_same_marc' + ; + + -- 4. Make a harmless update to target_record to trigger auto-update + -- in linked bibliographic records + UPDATE authority.record_entry + SET deleted = FALSE + WHERE id = source_record; + + -- 5. "Delete" source_record DELETE FROM authority.record_entry WHERE id = source_record; + -- 6. Set "reingest on same MARC" flag back to initial value + UPDATE config.internal_flag + SET enabled = ingest_same + WHERE name = 'ingest.reingest.force_on_same_marc' + ; + RETURN moved_objects; END; $func$ LANGUAGE plpgsql; diff --git a/Open-ILS/src/sql/Pg/0454.function.authority_merge_records.sql b/Open-ILS/src/sql/Pg/0454.function.authority_merge_records.sql new file mode 100644 index 000000000..a21a507be --- /dev/null +++ b/Open-ILS/src/sql/Pg/0454.function.authority_merge_records.sql @@ -0,0 +1,60 @@ +BEGIN; + +INSERT INTO config.upgrade_log (version) VALUES ('0454'); -- dbs + +CREATE OR REPLACE FUNCTION authority.merge_records ( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$ +DECLARE + moved_objects INT := 0; + bib_id INT := 0; + bib_rec biblio.record_entry%ROWTYPE; + auth_link authority.bib_linking%ROWTYPE; + ingest_same boolean; +BEGIN + + -- 1. Update all bib records with the ID from target_record in their $0 + FOR bib_rec IN SELECT bre.* FROM biblio.record_entry bre + INNER JOIN authority.bib_linking abl ON abl.bib = bre.id + WHERE abl.authority = source_record LOOP + + UPDATE biblio.record_entry + SET marc = REGEXP_REPLACE(marc, + E'([^<]*?\\))' || source_record || '<', + E'\\1' || target_record || '<', 'g') + WHERE id = bib_rec.id; + + moved_objects := moved_objects + 1; + END LOOP; + + -- 2. Grab the current value of reingest on same MARC flag + SELECT enabled INTO ingest_same + FROM config.internal_flag + WHERE name = 'ingest.reingest.force_on_same_marc' + ; + + -- 3. Temporarily set reingest on same to TRUE + UPDATE config.internal_flag + SET enabled = TRUE + WHERE name = 'ingest.reingest.force_on_same_marc' + ; + + -- 4. Make a harmless update to target_record to trigger auto-update + -- in linked bibliographic records + UPDATE authority.record_entry + SET DELETED = FALSE + WHERE id = source_record; + + -- 5. "Delete" source_record + DELETE FROM authority.record_entry + WHERE id = source_record; + + -- 6. Set "reingest on same MARC" flag back to initial value + UPDATE config.internal_flag + SET enabled = ingest_same + WHERE name = 'ingest.reingest.force_on_same_marc' + ; + + RETURN moved_objects; +END; +$func$ LANGUAGE plpgsql; + +COMMIT; -- 2.11.0