]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0588.schema.replace_field-default-value.sql
Update permission name to match existing ones
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0588.schema.replace_field-default-value.sql
1 -- Evergreen DB patch XXXX.schema.replace_field-default-value.sql
2 --
3 -- Return the input XML when no replace rule is provided
4 --
5 BEGIN;
6
7 -- check whether patch can be applied
8 SELECT evergreen.upgrade_deps_block_check('0588', :eg_version);
9
10 CREATE OR REPLACE FUNCTION vandelay.replace_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
11 DECLARE
12     xml_output TEXT;
13     parsed_target TEXT;
14     curr_field TEXT;
15 BEGIN
16
17     parsed_target := vandelay.strip_field( target_xml, ''); -- this dance normalizes the format of the xml for the IF below
18     xml_output := parsed_target; -- if there are no replace rules, just return the input
19
20     FOR curr_field IN SELECT UNNEST( STRING_TO_ARRAY(field, ',') ) LOOP -- naive split, but it's the same we use in the perl
21
22         xml_output := vandelay.strip_field( parsed_target, curr_field);
23
24         IF xml_output <> parsed_target  AND curr_field ~ E'~' THEN
25             -- we removed something, and there was a regexp restriction in the curr_field definition, so proceed
26             xml_output := vandelay.add_field( xml_output, source_xml, curr_field, 1 );
27         ELSIF curr_field !~ E'~' THEN
28             -- No regexp restriction, add the curr_field
29             xml_output := vandelay.add_field( xml_output, source_xml, curr_field, 0 );
30         END IF;
31
32         parsed_target := xml_output; -- in prep for any following loop iterations
33
34     END LOOP;
35
36     RETURN xml_output;
37 END;
38 $_$ LANGUAGE PLPGSQL;
39
40
41 COMMIT;