]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/version-upgrade/2.11.4-2.11.5-upgrade-db.sql
forward-port 2.11.4-2.11.5 schema update
[working/Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 2.11.4-2.11.5-upgrade-db.sql
1 --Upgrade Script for 2.11.4 to 2.11.5
2 \set eg_version '''2.11.5'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('2.11.5', :eg_version);
5 -- Evergreen DB patch XXXX.data.fix_long_overdue_perm.sql
6 --
7 -- Update permission 549 to have a "code" value that matches what
8 -- the Perl code references
9 --
10
11
12 -- check whether patch can be applied
13 SELECT evergreen.upgrade_deps_block_check('1037', :eg_version); -- jeff
14
15 -- For some time now, the database seed data / upgrade scripts have created
16 -- a permission with id 549 and code COPY_STATUS_LONGOVERDUE.override, while
17 -- the Perl code references a permission with code
18 -- COPY_STATUS_LONG_OVERDUE.override
19 --
20 -- Below, we attempt to handle at least three possible database states:
21 --
22 -- 1) no corrective action has been taken, permission exists with id 549 and
23 --    code COPY_STATUS_LONGOVERDUE.override
24 --
25 -- 2) permission with id 549 has already been updated to have code
26 --    COPY_STATUS_LONG_OVERDUE.override
27 --
28 -- 3) new permission with unknown id and code COPY_STATUS_LONG_OVERDUE.override
29 --    has been added, and potentially assigned to users/groups
30 --
31 -- In the case of 3, users and groups may have been assigned both perm id 549
32 -- and the local permission of unknown id.
33 --
34 -- The desired end result is that we should have a permission.perm_list
35 -- entry with id 549 and code COPY_STATUS_LONG_OVERDUE.override,
36 -- any locally-created permission with that same code but a different id
37 -- is deleted, and any users or groups that had been granted that locally-created
38 -- permission (by id) have been granted permission id 549 if not already granted.
39 --
40 -- If for some reason the permission at id 549 has an unexpected value for "code",
41 -- the end result of this upgrade script should be a no-op.
42
43 -- grant permission 549 to any group that
44 -- has a potentially locally-added perm
45 -- with code COPY_STATUS_LONG_OVERDUE.override
46 WITH new_grp_perms AS (
47 SELECT grp, 549 AS perm, depth, grantable
48 FROM permission.grp_perm_map pgpm
49 JOIN permission.perm_list ppl ON ppl.id = pgpm.perm
50 WHERE ppl.code = 'COPY_STATUS_LONG_OVERDUE.override'
51 -- short circuit if perm id 549 exists and doesn't have the expected code
52 AND EXISTS (SELECT 1 FROM permission.perm_list ppl WHERE ppl.id = 549 and ppl.code = 'COPY_STATUS_LONGOVERDUE.override')
53 -- don't try to assign perm 549 if already assigned
54 AND NOT EXISTS (SELECT 1 FROM permission.grp_perm_map pgpm2 WHERE pgpm2.grp = pgpm.grp AND pgpm2.perm = 549)
55 )
56 INSERT INTO permission.grp_perm_map
57 (grp, perm, depth, grantable)
58 SELECT grp, perm, depth, grantable
59 FROM new_grp_perms;
60
61 -- grant permission 549 to any user that
62 -- has a potentially locally-added perm
63 -- with code COPY_STATUS_LONG_OVERDUE.override
64 WITH new_usr_perms AS (
65 SELECT usr, 549 AS perm, depth, grantable
66 FROM permission.usr_perm_map pupm
67 JOIN permission.perm_list ppl ON ppl.id = pupm.perm
68 WHERE ppl.code = 'COPY_STATUS_LONG_OVERDUE.override'
69 -- short circuit if perm id 549 exists and doesn't have the expected code
70 AND EXISTS (SELECT 1 FROM permission.perm_list ppl WHERE ppl.id = 549 and ppl.code = 'COPY_STATUS_LONGOVERDUE.override')
71 -- don't try to assign perm 549 if already assigned
72 AND NOT EXISTS (SELECT 1 FROM permission.usr_perm_map pupm2 WHERE pupm2.usr = pupm.usr AND pupm2.perm = 549)
73 )
74 INSERT INTO permission.usr_perm_map
75 (usr, perm, depth, grantable)
76 SELECT usr, perm, depth, grantable
77 FROM new_usr_perms;
78
79 -- delete any group assignments of the locally-added perm
80 DELETE FROM permission.grp_perm_map
81 WHERE perm = (SELECT id FROM permission.perm_list WHERE code = 'COPY_STATUS_LONG_OVERDUE.override' AND id <> 549)
82 -- short circuit if perm id 549 exists and doesn't have the expected code
83 AND EXISTS (SELECT 1 FROM permission.perm_list ppl WHERE ppl.id = 549 and ppl.code = 'COPY_STATUS_LONGOVERDUE.override');
84
85 -- delete any user assignments of the locally-added perm
86 DELETE FROM permission.usr_perm_map
87 WHERE perm = (SELECT id FROM permission.perm_list WHERE code = 'COPY_STATUS_LONG_OVERDUE.override' AND id <> 549)
88 -- short circuit if perm id 549 exists and doesn't have the expected code
89 AND EXISTS (SELECT 1 FROM permission.perm_list ppl WHERE ppl.id = 549 and ppl.code = 'COPY_STATUS_LONGOVERDUE.override');
90
91 -- delete the locally-added perm, if any
92 DELETE FROM permission.perm_list
93 WHERE code = 'COPY_STATUS_LONG_OVERDUE.override'
94 AND id <> 549
95 -- short circuit if perm id 549 exists and doesn't have the expected code
96 AND EXISTS (SELECT 1 FROM permission.perm_list ppl WHERE ppl.id = 549 and ppl.code = 'COPY_STATUS_LONGOVERDUE.override');
97
98 -- update perm id 549 to the correct code, if not already
99 UPDATE permission.perm_list
100 SET code = 'COPY_STATUS_LONG_OVERDUE.override'
101 WHERE id = 549
102 AND code = 'COPY_STATUS_LONGOVERDUE.override';
103
104
105 SELECT evergreen.upgrade_deps_block_check('1039', :eg_version); -- jeffdavis/gmcharlt
106
107 UPDATE config.org_unit_setting_type
108 SET datatype = 'link', fm_class = 'vms'
109 WHERE name = 'vandelay.default_match_set'
110 AND   datatype = 'string'
111 AND   fm_class IS NULL;
112
113 \echo Existing vandelay.default_match_set that do not
114 \echo correspond to match sets
115 SELECT aou.shortname, aous.value
116 FROM   actor.org_unit_setting aous
117 JOIN   actor.org_unit aou ON (aou.id = aous.org_unit)
118 WHERE  aous.name = 'vandelay.default_match_set'
119 AND    (
120   value !~ '^"[0-9]+"$'
121   OR
122     oils_json_to_text(aous.value)::INT NOT IN (
123       SELECT id FROM vandelay.match_set
124     )
125 );
126
127 \echo And now deleting the bad values, as otherwise they
128 \echo will break the Library Settings Editor.
129 DELETE
130 FROM actor.org_unit_setting aous
131 WHERE  aous.name = 'vandelay.default_match_set'
132 AND    (
133   value !~ '^"[0-9]+"$'
134   OR
135     oils_json_to_text(aous.value)::INT NOT IN (
136       SELECT id FROM vandelay.match_set
137     )
138 );
139
140
141 SELECT evergreen.upgrade_deps_block_check('1040', :eg_version);
142
143 CREATE INDEX edi_message_remote_file_idx ON acq.edi_message (evergreen.lowercase(remote_file));
144
145 COMMIT;