From 11507993c4befcbdbe8d2c46c2f9e5ab40920c31 Mon Sep 17 00:00:00 2001 From: Dan Wells Date: Fri, 20 Feb 2015 14:37:51 -0500 Subject: [PATCH] LP#1251394 Fix DB order of operations 'representative_field' (and its check function) create circular references between metabib_class and metabib_field. The most straightforward fix is to deal with those fields separate from the initial table creation and field insertion. Signed-off-by: Dan Wells Signed-off-by: Bill Erickson Signed-off-by: Mike Rylander --- Open-ILS/src/sql/Pg/002.schema.config.sql | 26 +++++++++++++++----- Open-ILS/src/sql/Pg/950.data.seed-values.sql | 6 +++-- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql index a7fc62b15e..d9029b74d3 100644 --- a/Open-ILS/src/sql/Pg/002.schema.config.sql +++ b/Open-ILS/src/sql/Pg/002.schema.config.sql @@ -203,12 +203,6 @@ CREATE TABLE config.metabib_class ( b_weight NUMERIC DEFAULT 0.4 NOT NULL, c_weight NUMERIC DEFAULT 0.2 NOT NULL, d_weight NUMERIC DEFAULT 0.1 NOT NULL - representative_field INTEGER REFERENCES config.metabib_field(id), - CONSTRAINT rep_field_unique UNIQUE(representative_field), - CONSTRAINT rep_field_is_valid CHECK ( - representative_field IS NULL OR - config.metabib_representative_field_is_valid(representative_field, name) - ) ); CREATE TABLE config.metabib_field ( @@ -240,6 +234,26 @@ a "class" of either title, subject, author, keyword, series or identifier. $$; +CREATE OR REPLACE FUNCTION + config.metabib_representative_field_is_valid(INTEGER, TEXT) RETURNS BOOLEAN AS $$ + SELECT EXISTS (SELECT 1 FROM config.metabib_field WHERE id = $1 AND field_class = $2); +$$ LANGUAGE SQL STRICT IMMUTABLE; + +COMMENT ON FUNCTION config.metabib_representative_field_is_valid(INTEGER, TEXT) IS $$ +Ensure the field_class value on the selected representative field matches +the class name. +$$; + +ALTER TABLE config.metabib_class + ADD COLUMN representative_field + INTEGER REFERENCES config.metabib_field(id), + ADD CONSTRAINT rep_field_unique UNIQUE(representative_field), + ADD CONSTRAINT rep_field_is_valid CHECK ( + representative_field IS NULL OR + config.metabib_representative_field_is_valid(representative_field, name) + ) +; + CREATE UNIQUE INDEX config_metabib_field_class_name_idx ON config.metabib_field (field_class, name); CREATE TABLE config.ts_config_list ( diff --git a/Open-ILS/src/sql/Pg/950.data.seed-values.sql b/Open-ILS/src/sql/Pg/950.data.seed-values.sql index 360397b9ab..99ea346ad5 100644 --- a/Open-ILS/src/sql/Pg/950.data.seed-values.sql +++ b/Open-ILS/src/sql/Pg/950.data.seed-values.sql @@ -96,8 +96,8 @@ SELECT SETVAL('config.standing_penalty_id_seq', 100); INSERT INTO config.metabib_class ( name, label ) VALUES ( 'identifier', oils_i18n_gettext('identifier', 'Identifier', 'cmc', 'label') ); INSERT INTO config.metabib_class ( name, label ) VALUES ( 'keyword', oils_i18n_gettext('keyword', 'Keyword', 'cmc', 'label') ); -INSERT INTO config.metabib_class ( name, label, representative_field ) VALUES ( 'title', oils_i18n_gettext('title', 'Title', 'cmc', 'label'), 6 ); -INSERT INTO config.metabib_class ( name, label, representative_field ) VALUES ( 'author', oils_i18n_gettext('author', 'Author', 'cmc', 'label'), 8 ); +INSERT INTO config.metabib_class ( name, label ) VALUES ( 'title', oils_i18n_gettext('title', 'Title', 'cmc', 'label')); +INSERT INTO config.metabib_class ( name, label ) VALUES ( 'author', oils_i18n_gettext('author', 'Author', 'cmc', 'label')); INSERT INTO config.metabib_class ( name, label ) VALUES ( 'subject', oils_i18n_gettext('subject', 'Subject', 'cmc', 'label') ); INSERT INTO config.metabib_class ( name, label ) VALUES ( 'series', oils_i18n_gettext('series', 'Series', 'cmc', 'label') ); @@ -127,11 +127,13 @@ INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath, (5, 'title', 'uniform', oils_i18n_gettext(5, 'Uniform Title', 'cmf', 'label'), 'mods32', $$//mods32:mods/mods32:titleInfo[mods32:title and (@type='uniform-nfi')]$$, '//@xlink:href', $$*[local-name() != "nonSort"]$$ ); INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath, authority_xpath, browse_field ) VALUES (6, 'title', 'proper', oils_i18n_gettext(6, 'Title Proper', 'cmf', 'label'), 'mods32', $$//mods32:mods/mods32:titleNonfiling[mods32:title and not (@type)]$$, '//@xlink:href', FALSE ); +UPDATE config.metabib_class SET representative_field = 6 WHERE name = 'title'; INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath, facet_xpath, facet_field , authority_xpath, browse_xpath) VALUES (7, 'author', 'corporate', oils_i18n_gettext(7, 'Corporate Author', 'cmf', 'label'), 'mods32', $$//mods32:mods/mods32:name[@type='corporate' and (mods32:role/mods32:roleTerm[text()='creator'] or mods32:role/mods32:roleTerm[text()='aut'] or mods32:role/mods32:roleTerm[text()='cre'])]$$, $$//*[local-name()='namePart']$$, TRUE, '//@xlink:href',$$//*[local-name()='namePart']$$ ); -- /* to fool vim */; INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath, facet_xpath, facet_field, authority_xpath, browse_xpath ) VALUES (8, 'author', 'personal', oils_i18n_gettext(8, 'Personal Author', 'cmf', 'label'), 'mods32', $$//mods32:mods/mods32:name[@type='personal' and mods32:role/mods32:roleTerm[text()='creator']]$$, $$//*[local-name()='namePart']$$, TRUE, '//@xlink:href',$$//*[local-name()='namePart']$$ ); -- /* to fool vim */; +UPDATE config.metabib_class SET representative_field = 8 WHERE name = 'author'; INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath, facet_xpath, facet_field, authority_xpath, browse_xpath ) VALUES (9, 'author', 'conference', oils_i18n_gettext(9, 'Conference Author', 'cmf', 'label'), 'mods32', $$//mods32:mods/mods32:name[@type='conference' and mods32:role/mods32:roleTerm[text()='creator']]$$, $$//*[local-name()='namePart']$$, TRUE, '//@xlink:href',$$//*[local-name()='namePart']$$ ); -- /* to fool vim */; INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath, facet_xpath, facet_field, authority_xpath, browse_xpath ) VALUES -- 2.43.2