From 4ce1ac1f51da53bfcc86aaa89972d390a8424096 Mon Sep 17 00:00:00 2001 From: erickson Date: Mon, 7 Jun 2010 16:10:48 +0000 Subject: [PATCH] Patch from Galen Charlton: The attached patch adds triggers on biblio.record_entry and authority.record_entry to prevent the insertion or update of MARCXML that is not well-formed. Since the various consumers of bre.marc and are.marc tend not to handle invalid XML all that well, these triggers serve as a (very basic) data integrity check. http://libmail.georgialibraries.org/pipermail/open-ils-dev/2010-June/006116.html git-svn-id: svn://svn.open-ils.org/ILS/trunk@16614 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/src/sql/Pg/002.schema.config.sql | 2 +- Open-ILS/src/sql/Pg/010.schema.biblio.sql | 13 +++++++++++++ Open-ILS/src/sql/Pg/011.schema.authority.sql | 1 + .../upgrade/0295.schema.marcxml_check_trigger.sql | 21 +++++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/0295.schema.marcxml_check_trigger.sql diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql index faee60f27..7d286c5e3 100644 --- a/Open-ILS/src/sql/Pg/002.schema.config.sql +++ b/Open-ILS/src/sql/Pg/002.schema.config.sql @@ -65,7 +65,7 @@ CREATE TABLE config.upgrade_log ( install_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); -INSERT INTO config.upgrade_log (version) VALUES ('0294'); -- phasefx +INSERT INTO config.upgrade_log (version) VALUES ('0295'); -- gmcharlt CREATE TABLE config.bib_source ( id SERIAL PRIMARY KEY, diff --git a/Open-ILS/src/sql/Pg/010.schema.biblio.sql b/Open-ILS/src/sql/Pg/010.schema.biblio.sql index b0f87c14f..1bd5b9afe 100644 --- a/Open-ILS/src/sql/Pg/010.schema.biblio.sql +++ b/Open-ILS/src/sql/Pg/010.schema.biblio.sql @@ -25,6 +25,18 @@ CREATE OR REPLACE FUNCTION biblio.next_autogen_tcn_value () RETURNS TEXT AS $$ BEGIN RETURN 'AUTOGENERATED-' || nextval('biblio.autogen_tcn_value_seq'::TEXT); END; $$ LANGUAGE PLPGSQL; +CREATE OR REPLACE FUNCTION biblio.check_marcxml_well_formed () RETURNS TRIGGER AS $func$ +BEGIN + + IF xml_is_well_formed(NEW.marc) THEN + RETURN NEW; + ELSE + RAISE EXCEPTION 'Attempted to % MARCXML that is not well formed', TG_OP; + END IF; + +END; +$func$ LANGUAGE PLPGSQL; + CREATE TABLE biblio.record_entry ( id BIGSERIAL PRIMARY KEY, creator INT NOT NULL DEFAULT 1, @@ -49,6 +61,7 @@ CREATE INDEX biblio_record_entry_editor_idx ON biblio.record_entry ( editor ); CREATE INDEX biblio_record_entry_edit_date_idx ON biblio.record_entry ( edit_date ); CREATE INDEX biblio_record_entry_fp_idx ON biblio.record_entry ( fingerprint ); CREATE UNIQUE INDEX biblio_record_unique_tcn ON biblio.record_entry (tcn_value) WHERE deleted = FALSE OR deleted IS FALSE; +CREATE TRIGGER a_marcxml_is_well_formed BEFORE INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE biblio.check_marcxml_well_formed(); CREATE TABLE biblio.record_note ( id BIGSERIAL 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 9b010beb1..a362cec45 100644 --- a/Open-ILS/src/sql/Pg/011.schema.authority.sql +++ b/Open-ILS/src/sql/Pg/011.schema.authority.sql @@ -37,6 +37,7 @@ CREATE TABLE authority.record_entry ( CREATE INDEX authority_record_entry_creator_idx ON authority.record_entry ( creator ); CREATE INDEX authority_record_entry_editor_idx ON authority.record_entry ( editor ); CREATE UNIQUE INDEX authority_record_unique_tcn ON authority.record_entry (arn_source,arn_value) WHERE deleted = FALSE OR deleted IS FALSE; +CREATE TRIGGER a_marcxml_is_well_formed BEFORE INSERT OR UPDATE ON authority.record_entry FOR EACH ROW EXECUTE PROCEDURE biblio.check_marcxml_well_formed(); CREATE TABLE authority.record_note ( id BIGSERIAL PRIMARY KEY, diff --git a/Open-ILS/src/sql/Pg/upgrade/0295.schema.marcxml_check_trigger.sql b/Open-ILS/src/sql/Pg/upgrade/0295.schema.marcxml_check_trigger.sql new file mode 100644 index 000000000..0b65f363c --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/0295.schema.marcxml_check_trigger.sql @@ -0,0 +1,21 @@ +BEGIN; + +INSERT INTO config.upgrade_log (version) VALUES ('0295'); -- gmcharlt + +CREATE OR REPLACE FUNCTION biblio.check_marcxml_well_formed () RETURNS TRIGGER AS $func$ +BEGIN + + IF xml_is_well_formed(NEW.marc) THEN + RETURN NEW; + ELSE + RAISE EXCEPTION 'Attempted to % MARCXML that is not well formed', TG_OP; + END IF; + +END; +$func$ LANGUAGE PLPGSQL; + +CREATE TRIGGER a_marcxml_is_well_formed BEFORE INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE biblio.check_marcxml_well_formed(); + +CREATE TRIGGER a_marcxml_is_well_formed BEFORE INSERT OR UPDATE ON authority.record_entry FOR EACH ROW EXECUTE PROCEDURE biblio.check_marcxml_well_formed(); + +COMMIT; -- 2.11.0