From 54f0ffae30181676340c593bcc3851d3e9f96a34 Mon Sep 17 00:00:00 2001 From: Mike Rylander Date: Tue, 25 Aug 2020 15:25:08 -0400 Subject: [PATCH] LP#1749475: Safer upgrade script This modifies the database upgrade script to avoid overwriting the existing templates if they have been differ from the default supplied in the most recent seed data. Signed-off-by: Mike Rylander Signed-off-by: Michele Morgan Signed-off-by: Galen Charlton --- .../sql/Pg/upgrade/XXXX.schema.AT-def-groups.sql | 283 ++++++++++++++++++++- 1 file changed, 269 insertions(+), 14 deletions(-) diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.AT-def-groups.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.AT-def-groups.sql index de6d125e62..9d64d9ad8b 100644 --- a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.AT-def-groups.sql +++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.AT-def-groups.sql @@ -44,22 +44,140 @@ CREATE TABLE action_trigger.event_def_group_member ( INSERT INTO action_trigger.event_def_group (id, owner, hook, name) VALUES (1, 1, 'biblio.format.record_entry.print','Print Record(s)'); -INSERT INTO action_trigger.event_def_group_member (grp, name, event_def) - SELECT 1, 'Brief', id FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.print'; - -INSERT INTO action_trigger.event_def_group_member (grp, name, holdings, event_def) - SELECT 1, 'Full', TRUE, id FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.print'; - INSERT INTO action_trigger.event_def_group (id, owner, hook, name) VALUES (2,1,'biblio.format.record_entry.email','Email Record(s)'); -INSERT INTO action_trigger.event_def_group_member (grp, name, event_def) - SELECT 2, 'Brief', id FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.email'; +DO $block$ +BEGIN + PERFORM * FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.email' AND owner = 1 AND active AND template = +$$ +[%- USE date -%] +[%- SET user = target.0.owner -%] +To: [%- params.recipient_email || user.email %] +From: [%- params.sender_email || default_sender %] +Date: [%- date.format(date.now, '%a, %d %b %Y %T -0000', gmt => 1) %] +Subject: Bibliographic Records +Auto-Submitted: auto-generated + +[% FOR cbreb IN target %] +[% FOR item IN cbreb.items; + bre_id = item.target_biblio_record_entry; + + bibxml = helpers.unapi_bre(bre_id, {flesh => '{mra}'}); + title = ''; + FOR part IN bibxml.findnodes('//*[@tag="245"]/*[@code="a" or @code="b"]'); + title = title _ part.textContent; + END; + + author = bibxml.findnodes('//*[@tag="100"]/*[@code="a"]').textContent; + item_type = bibxml.findnodes('//*[local-name()="attributes"]/*[local-name()="field"][@name="item_type"]').getAttribute('coded-value'); + publisher = bibxml.findnodes('//*[@tag="260"]/*[@code="b"]').textContent; + pubdate = bibxml.findnodes('//*[@tag="260"]/*[@code="c"]').textContent; + isbn = bibxml.findnodes('//*[@tag="020"]/*[@code="a"]').textContent; + issn = bibxml.findnodes('//*[@tag="022"]/*[@code="a"]').textContent; + upc = bibxml.findnodes('//*[@tag="024"]/*[@code="a"]').textContent; +%] + +[% loop.count %]/[% loop.size %]. Bib ID# [% bre_id %] +[% IF isbn %]ISBN: [% isbn _ "\n" %][% END -%] +[% IF issn %]ISSN: [% issn _ "\n" %][% END -%] +[% IF upc %]UPC: [% upc _ "\n" %] [% END -%] +Title: [% title %] +Author: [% author %] +Publication Info: [% publisher %] [% pubdate %] +Item Type: [% item_type %] + +[% END %] +[% END %] +$$; + + IF FOUND THEN -- update + + INSERT INTO action_trigger.event_def_group_member (grp, name, event_def) + SELECT 2, 'Brief', id FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.email'; + + INSERT INTO action_trigger.event_def_group_member (grp, name, holdings, event_def) + SELECT 2, 'Full', TRUE, id FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.email'; + + UPDATE action_trigger.event_definition SET template = $$ +[%- USE date -%] +[%- SET user = target.0.owner -%] +To: [%- params.recipient_email || user_data.0.email || user.email %] +From: [%- params.sender_email || default_sender %] +Date: [%- date.format(date.now, '%a, %d %b %Y %T -0000', gmt => 1) %] +Subject: [%- user_data.0.subject || 'Bibliographic Records' %] +Auto-Submitted: auto-generated + +[%- FOR cbreb IN target; + + flesh_list = '{mra'; + IF user_data.0.type == 'full'; + flesh_list = flesh_list _ ',holdings_xml,acp'; + IF params.holdings_limit; + flimit = 'acn=>' _ params.holdings_limit _ ',acp=>' _ params.holdings_limit; + END; + END; + flesh_list = flesh_list _ '}'; + + item_list = helpers.sort_bucket_unapi_bre(cbreb.items,{flesh => flesh_list, site => user_data.0.context_org, flesh_limit => flimit}, user_data.0.sort_by, user_data.0.sort_dir); -INSERT INTO action_trigger.event_def_group_member (grp, name, holdings, event_def) - SELECT 2, 'Full', TRUE, id FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.email'; +FOR item IN item_list -%] -UPDATE action_trigger.event_definition SET template = $$ +[% loop.count %]/[% loop.size %]. Bib ID# [% item.id %] +[% IF item.isbn %]ISBN: [% item.isbn _ "\n" %][% END -%] +[% IF item.issn %]ISSN: [% item.issn _ "\n" %][% END -%] +[% IF item.upc %]UPC: [% item.upc _ "\n" %][% END -%] +Title: [% item.title %] +[% IF item.author %]Author: [% item.author _ "\n" %][% END -%] +Publication Info: [% item.publisher %] [% item.pubdate %] +Item Type: [% item.item_type %] +[% IF user_data.0.type == 'full' && item.holdings.size == 0 %] + * No items for this record at the selected location +[%- END %] +[% FOR cp IN item.holdings -%] + * Library: [% cp.circ_lib %] + Location: [% cp.location %] + Call Number: [% cp.prefix _ ' ' _ cp.callnumber _ ' ' _ cp.suffix %] +[% IF cp.parts %] Parts: [% cp.parts _ "\n" %][% END -%] + Status: [% cp.status_label %] + Barcode: [% cp.barcode %] + +[% END -%] +[%- END -%] +[%- END -%] +$$ WHERE hook = 'biblio.format.record_entry.email' AND owner = 1 AND active; + + ELSE -- insert full and add existing brief + + INSERT INTO action_trigger.event_def_group_member (grp, name, event_def) + SELECT 2, 'Brief', id FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.email' AND active; + + INSERT INTO action_trigger.event_definition ( + active, + owner, + name, + hook, + validator, + reactor, + cleanup_success, + cleanup_failure, + group_field, + granularity, + delay, + template + ) SELECT + TRUE, + owner, + 'biblio.record_entry.email.full', + 'biblio.format.record_entry.email', + 'NOOP_True', + 'SendEmail', + 'DeleteTempBiblioBucket', + 'DeleteTempBiblioBucket', + 'owner', + NULL, + '00:00:00', + $$ [%- USE date -%] [%- SET user = target.0.owner -%] To: [%- params.recipient_email || user_data.0.email || user.email %] @@ -105,9 +223,139 @@ Item Type: [% item.item_type %] [% END -%] [%- END -%] [%- END -%] -$$ WHERE hook = 'biblio.format.record_entry.email'; +$$) FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.email' AND active; + + INSERT INTO action_trigger.event_def_group_member (grp, name, holdings, event_def) + SELECT 2, 'Full', TRUE, id FROM action_trigger.event_definition WHERE name = 'biblio.record_entry.email.full' and active; + + END IF; +END +$block$; + +DO $block$ +BEGIN + PERFORM * FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.print' AND owner = 1 AND active AND template = +$$ +
+ +
    + [% FOR cbreb IN target %] + [% FOR item IN cbreb.items; + bre_id = item.target_biblio_record_entry; + + bibxml = helpers.unapi_bre(bre_id, {flesh => '{mra}'}); + title = ''; + FOR part IN bibxml.findnodes('//*[@tag="245"]/*[@code="a" or @code="b"]'); + title = title _ part.textContent; + END; + + author = bibxml.findnodes('//*[@tag="100"]/*[@code="a"]').textContent; + item_type = bibxml.findnodes('//*[local-name()="attributes"]/*[local-name()="field"][@name="item_type"]').getAttribute('coded-value'); + publisher = bibxml.findnodes('//*[@tag="260"]/*[@code="b"]').textContent; + pubdate = bibxml.findnodes('//*[@tag="260"]/*[@code="c"]').textContent; + isbn = bibxml.findnodes('//*[@tag="020"]/*[@code="a"]').textContent; + %] + +
  1. + Bib ID# [% bre_id %] ISBN: [% isbn %]
    + Title: [% title %]
    + Author: [% author %]
    + Publication Info: [% publisher %] [% pubdate %]
    + Item Type: [% item_type %] +
  2. + [% END %] + [% END %] +
+
+$$; + + IF FOUND THEN -- update + + INSERT INTO action_trigger.event_def_group_member (grp, name, event_def) + SELECT 1, 'Brief', id FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.print'; + + INSERT INTO action_trigger.event_def_group_member (grp, name, holdings, event_def) + SELECT 1, 'Full', TRUE, id FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.print'; + + UPDATE action_trigger.event_definition SET template = $$ +
+ +
    + [% FOR cbreb IN target; + + flesh_list = '{mra'; + IF user_data.0.type == 'full'; + flesh_list = flesh_list _ ',holdings_xml,acp'; + IF params.holdings_limit; + flimit = 'acn=>' _ params.holdings_limit _ ',acp=>' _ params.holdings_limit; + END; + END; + flesh_list = flesh_list _ '}'; + + item_list = helpers.sort_bucket_unapi_bre(cbreb.items,{flesh => flesh_list, site => user_data.0.context_org, flesh_limit => flimit}, user_data.0.sort_by, user_data.0.sort_dir); + FOR item IN item_list %] +
  1. + Bib ID# [% item.id %]
    + [% IF item.isbn %]ISBN: [% item.isbn %]
    [% END %] + [% IF item.issn %]ISSN: [% item.issn %]
    [% END %] + [% IF item.upc %]UPC: [% item.upc %]
    [% END %] + Title: [% item.title %]
    +[% IF item.author %] Author: [% item.author %]
    [% END -%] + Publication Info: [% item.publisher %] [% item.pubdate %]
    + Item Type: [% item.item_type %] +
      + [% IF user_data.0.type == 'full' && item.holdings.size == 0 %] +
    • No items for this record at the selected location
    • + [% END %] + [% FOR cp IN item.holdings -%] +
    • + Library: [% cp.circ_lib %]
      + Location: [% cp.location %]
      + Call Number: [% cp.prefix _ ' ' _ cp.callnumber _ ' ' _ cp.suffix %]
      + [% IF cp.parts %]Parts: [% cp.parts %]
      [% END %] + Status: [% cp.status_label %]
      + Barcode: [% cp.barcode %] +
    • + [% END %] +
    +
  2. + [% END %] + [% END %] +
+
+$$ WHERE hook = 'biblio.format.record_entry.print' AND owner = 1 AND active; -UPDATE action_trigger.event_definition SET template = $$ + ELSE -- insert full and add brief + + INSERT INTO action_trigger.event_def_group_member (grp, name, event_def) + SELECT 1, 'Brief', id FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.print' AND active; + + INSERT INTO action_trigger.event_definition ( + active, + owner, + name, + hook, + validator, + reactor, + cleanup_success, + cleanup_failure, + group_field, + granularity, + delay, + template + ) SELECT + TRUE, + owner, + 'biblio.record_entry.print.full', + 'biblio.format.record_entry.print', + 'NOOP_True', + 'ProcessTemplate', + 'DeleteTempBiblioBucket', + 'DeleteTempBiblioBucket', + 'owner', + 'print-on-demand', + '00:00:00', + $$
    @@ -153,7 +401,14 @@ UPDATE action_trigger.event_definition SET template = $$ [% END %]
-$$ WHERE hook = 'biblio.format.record_entry.print'; +$$ FROM action_trigger.event_definition WHERE hook = 'biblio.format.record_entry.print' AND active; + + INSERT INTO action_trigger.event_def_group_member (grp, name, holdings, event_def) + SELECT 1, 'Full', TRUE, id FROM action_trigger.event_definition WHERE name = 'biblio.record_entry.print.full' and active; + + END IF; +END +$block$; COMMIT; -- 2.11.0