]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/1037.data.fix_long_overdue_perm.sql
LP#1661688: Add a link and other tweaks to alternate hold pickup feature
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 1037.data.fix_long_overdue_perm.sql
1 -- Evergreen DB patch XXXX.data.fix_long_overdue_perm.sql
2 --
3 -- Update permission 549 to have a "code" value that matches what
4 -- the Perl code references
5 --
6 BEGIN;
7
8
9 -- check whether patch can be applied
10 SELECT evergreen.upgrade_deps_block_check('1037', :eg_version); -- jeff
11
12 -- For some time now, the database seed data / upgrade scripts have created
13 -- a permission with id 549 and code COPY_STATUS_LONGOVERDUE.override, while
14 -- the Perl code references a permission with code
15 -- COPY_STATUS_LONG_OVERDUE.override
16 --
17 -- Below, we attempt to handle at least three possible database states:
18 --
19 -- 1) no corrective action has been taken, permission exists with id 549 and
20 --    code COPY_STATUS_LONGOVERDUE.override
21 --
22 -- 2) permission with id 549 has already been updated to have code
23 --    COPY_STATUS_LONG_OVERDUE.override
24 --
25 -- 3) new permission with unknown id and code COPY_STATUS_LONG_OVERDUE.override
26 --    has been added, and potentially assigned to users/groups
27 --
28 -- In the case of 3, users and groups may have been assigned both perm id 549
29 -- and the local permission of unknown id.
30 --
31 -- The desired end result is that we should have a permission.perm_list
32 -- entry with id 549 and code COPY_STATUS_LONG_OVERDUE.override,
33 -- any locally-created permission with that same code but a different id
34 -- is deleted, and any users or groups that had been granted that locally-created
35 -- permission (by id) have been granted permission id 549 if not already granted.
36 --
37 -- If for some reason the permission at id 549 has an unexpected value for "code",
38 -- the end result of this upgrade script should be a no-op.
39
40 -- grant permission 549 to any group that
41 -- has a potentially locally-added perm
42 -- with code COPY_STATUS_LONG_OVERDUE.override
43 WITH new_grp_perms AS (
44 SELECT grp, 549 AS perm, depth, grantable
45 FROM permission.grp_perm_map pgpm
46 JOIN permission.perm_list ppl ON ppl.id = pgpm.perm
47 WHERE ppl.code = 'COPY_STATUS_LONG_OVERDUE.override'
48 -- short circuit if perm id 549 exists and doesn't have the expected code
49 AND EXISTS (SELECT 1 FROM permission.perm_list ppl WHERE ppl.id = 549 and ppl.code = 'COPY_STATUS_LONGOVERDUE.override')
50 -- don't try to assign perm 549 if already assigned
51 AND NOT EXISTS (SELECT 1 FROM permission.grp_perm_map pgpm2 WHERE pgpm2.grp = pgpm.grp AND pgpm2.perm = 549)
52 )
53 INSERT INTO permission.grp_perm_map
54 (grp, perm, depth, grantable)
55 SELECT grp, perm, depth, grantable
56 FROM new_grp_perms;
57
58 -- grant permission 549 to any user that
59 -- has a potentially locally-added perm
60 -- with code COPY_STATUS_LONG_OVERDUE.override
61 WITH new_usr_perms AS (
62 SELECT usr, 549 AS perm, depth, grantable
63 FROM permission.usr_perm_map pupm
64 JOIN permission.perm_list ppl ON ppl.id = pupm.perm
65 WHERE ppl.code = 'COPY_STATUS_LONG_OVERDUE.override'
66 -- short circuit if perm id 549 exists and doesn't have the expected code
67 AND EXISTS (SELECT 1 FROM permission.perm_list ppl WHERE ppl.id = 549 and ppl.code = 'COPY_STATUS_LONGOVERDUE.override')
68 -- don't try to assign perm 549 if already assigned
69 AND NOT EXISTS (SELECT 1 FROM permission.usr_perm_map pupm2 WHERE pupm2.usr = pupm.usr AND pupm2.perm = 549)
70 )
71 INSERT INTO permission.usr_perm_map
72 (usr, perm, depth, grantable)
73 SELECT usr, perm, depth, grantable
74 FROM new_usr_perms;
75
76 -- delete any group assignments of the locally-added perm
77 DELETE FROM permission.grp_perm_map
78 WHERE perm = (SELECT id FROM permission.perm_list WHERE code = 'COPY_STATUS_LONG_OVERDUE.override' AND id <> 549)
79 -- short circuit if perm id 549 exists and doesn't have the expected code
80 AND EXISTS (SELECT 1 FROM permission.perm_list ppl WHERE ppl.id = 549 and ppl.code = 'COPY_STATUS_LONGOVERDUE.override');
81
82 -- delete any user assignments of the locally-added perm
83 DELETE FROM permission.usr_perm_map
84 WHERE perm = (SELECT id FROM permission.perm_list WHERE code = 'COPY_STATUS_LONG_OVERDUE.override' AND id <> 549)
85 -- short circuit if perm id 549 exists and doesn't have the expected code
86 AND EXISTS (SELECT 1 FROM permission.perm_list ppl WHERE ppl.id = 549 and ppl.code = 'COPY_STATUS_LONGOVERDUE.override');
87
88 -- delete the locally-added perm, if any
89 DELETE FROM permission.perm_list
90 WHERE code = 'COPY_STATUS_LONG_OVERDUE.override'
91 AND id <> 549
92 -- short circuit if perm id 549 exists and doesn't have the expected code
93 AND EXISTS (SELECT 1 FROM permission.perm_list ppl WHERE ppl.id = 549 and ppl.code = 'COPY_STATUS_LONGOVERDUE.override');
94
95 -- update perm id 549 to the correct code, if not already
96 UPDATE permission.perm_list
97 SET code = 'COPY_STATUS_LONG_OVERDUE.override'
98 WHERE id = 549
99 AND code = 'COPY_STATUS_LONGOVERDUE.override';
100
101 COMMIT;