Forward port database upgrades
[evergreen-equinox.git] / Open-ILS / src / sql / Pg / version-upgrade / 3.10.1-3.10.2-upgrade-db.sql
1 --Upgrade Script for 3.10.1 to 3.10.2
2 \set eg_version '''3.10.2'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('3.10.2', :eg_version);
5
6 SELECT evergreen.upgrade_deps_block_check('1362', :eg_version);
7
8 CREATE INDEX hold_request_hopeless_date_idx ON action.hold_request (hopeless_date);
9
10
11 ANALYZE action.hold_request;
12
13 SELECT evergreen.upgrade_deps_block_check('1365', :eg_version);
14
15 CREATE OR REPLACE FUNCTION search.highlight_display_fields_impl(
16     rid         BIGINT,
17     tsq         TEXT,
18     field_list  INT[] DEFAULT '{}'::INT[],
19     css_class   TEXT DEFAULT 'oils_SH',
20     hl_all      BOOL DEFAULT TRUE,
21     minwords    INT DEFAULT 5,
22     maxwords    INT DEFAULT 25,
23     shortwords  INT DEFAULT 0,
24     maxfrags    INT DEFAULT 0,
25     delimiter   TEXT DEFAULT ' ... '
26 ) RETURNS SETOF search.highlight_result AS $f$
27 DECLARE
28     opts            TEXT := '';
29     v_css_class     TEXT := css_class;
30     v_delimiter     TEXT := delimiter;
31     v_field_list    INT[] := field_list;
32     hl_query        TEXT;
33 BEGIN
34     IF v_delimiter LIKE $$%'%$$ OR v_delimiter LIKE '%"%' THEN --"
35         v_delimiter := ' ... ';
36     END IF;
37
38     IF NOT hl_all THEN
39         opts := opts || 'MinWords=' || minwords;
40         opts := opts || ', MaxWords=' || maxwords;
41         opts := opts || ', ShortWords=' || shortwords;
42         opts := opts || ', MaxFragments=' || maxfrags;
43         opts := opts || ', FragmentDelimiter="' || delimiter || '"';
44     ELSE
45         opts := opts || 'HighlightAll=TRUE';
46     END IF;
47
48     IF v_css_class LIKE $$%'%$$ OR v_css_class LIKE '%"%' THEN -- "
49         v_css_class := 'oils_SH';
50     END IF;
51
52     opts := opts || $$, StopSel=</mark>, StartSel="<mark class='$$ || v_css_class; -- "
53
54     IF v_field_list = '{}'::INT[] THEN
55         SELECT ARRAY_AGG(id) INTO v_field_list FROM config.metabib_field WHERE display_field;
56     END IF;
57
58     hl_query := $$
59         SELECT  de.id,
60                 de.source,
61                 de.field,
62                 evergreen.escape_for_html(de.value) AS value,
63                 ts_headline(
64                     ts_config::REGCONFIG,
65                     evergreen.escape_for_html(de.value),
66                     $$ || quote_literal(tsq) || $$,
67                     $1 || ' ' || mf.field_class || ' ' || mf.name || $xx$'>"$xx$ -- "'
68                 ) AS highlight
69           FROM  metabib.display_entry de
70                 JOIN config.metabib_field mf ON (mf.id = de.field)
71                 JOIN search.best_tsconfig t ON (t.id = de.field)
72           WHERE de.source = $2
73                 AND field = ANY ($3)
74           ORDER BY de.id;$$;
75
76     RETURN QUERY EXECUTE hl_query USING opts, rid, v_field_list;
77 END;
78 $f$ LANGUAGE PLPGSQL;
79
80
81 SELECT evergreen.upgrade_deps_block_check('1376', :eg_version);
82
83 -- 1236
84
85 CREATE OR REPLACE VIEW action.all_circulation_combined_types AS
86  SELECT acirc.id AS id,
87     acirc.xact_start,
88     acirc.circ_lib,
89     acirc.circ_staff,
90     acirc.create_time,
91     ac_acirc.circ_modifier AS item_type,
92     'regular_circ'::text AS circ_type
93    FROM action.circulation acirc,
94     asset.copy ac_acirc
95   WHERE acirc.target_copy = ac_acirc.id
96 UNION ALL
97  SELECT ancc.id::BIGINT AS id,
98     ancc.circ_time AS xact_start,
99     ancc.circ_lib,
100     ancc.staff AS circ_staff,
101     ancc.circ_time AS create_time,
102     cnct_ancc.name AS item_type,
103     'non-cat_circ'::text AS circ_type
104    FROM action.non_cataloged_circulation ancc,
105     config.non_cataloged_type cnct_ancc
106   WHERE ancc.item_type = cnct_ancc.id
107 UNION ALL
108  SELECT aihu.id::BIGINT AS id,
109     aihu.use_time AS xact_start,
110     aihu.org_unit AS circ_lib,
111     aihu.staff AS circ_staff,
112     aihu.use_time AS create_time,
113     ac_aihu.circ_modifier AS item_type,
114     'in-house_use'::text AS circ_type
115    FROM action.in_house_use aihu,
116     asset.copy ac_aihu
117   WHERE aihu.item = ac_aihu.id
118 UNION ALL
119  SELECT ancihu.id::BIGINT AS id,
120     ancihu.use_time AS xact_start,
121     ancihu.org_unit AS circ_lib,
122     ancihu.staff AS circ_staff,
123     ancihu.use_time AS create_time,
124     cnct_ancihu.name AS item_type,
125     'non-cat-in-house_use'::text AS circ_type
126    FROM action.non_cat_in_house_use ancihu,
127     config.non_cataloged_type cnct_ancihu
128   WHERE ancihu.item_type = cnct_ancihu.id
129 UNION ALL
130  SELECT aacirc.id AS id,
131     aacirc.xact_start,
132     aacirc.circ_lib,
133     aacirc.circ_staff,
134     aacirc.create_time,
135     ac_aacirc.circ_modifier AS item_type,
136     'aged_circ'::text AS circ_type
137    FROM action.aged_circulation aacirc,
138     asset.copy ac_aacirc
139   WHERE aacirc.target_copy = ac_aacirc.id;
140
141 -- 1237
142
143 INSERT INTO config.workstation_setting_type (name, grp, datatype, label)
144 SELECT
145     'eg.staffcat.exclude_electronic', 'gui', 'bool',
146     oils_i18n_gettext(
147         'eg.staffcat.exclude_electronic',
148         'Staff Catalog "Exclude Electronic Resources" Option',
149         'cwst', 'label'
150     )
151 WHERE NOT EXISTS (
152     SELECT 1
153     FROM config.workstation_setting_type
154     WHERE name = 'eg.staffcat.exclude_electronic'
155 );
156
157 -- 1238
158
159 INSERT INTO permission.perm_list ( id, code, description ) SELECT
160  625, 'VIEW_BOOKING_RESERVATION', oils_i18n_gettext(625,
161     'View booking reservations', 'ppl', 'description')
162 WHERE NOT EXISTS (
163     SELECT 1
164     FROM permission.perm_list
165     WHERE id = 625
166     AND   code = 'VIEW_BOOKING_RESERVATION'
167 );
168
169 INSERT INTO permission.perm_list ( id, code, description ) SELECT
170  626, 'VIEW_BOOKING_RESERVATION_ATTR_MAP', oils_i18n_gettext(626,
171     'View booking reservation attribute maps', 'ppl', 'description')
172 WHERE NOT EXISTS (
173     SELECT 1
174     FROM permission.perm_list
175     WHERE id = 626
176     AND   code = 'VIEW_BOOKING_RESERVATION_ATTR_MAP'
177 );
178
179 -- reprise 1269 just in case now that the perms should definitely exist
180
181 WITH perms_to_add AS
182     (SELECT id FROM
183     permission.perm_list
184     WHERE code IN ('VIEW_BOOKING_RESERVATION', 'VIEW_BOOKING_RESERVATION_ATTR_MAP'))
185
186 INSERT INTO permission.grp_perm_map (grp, perm, depth, grantable)
187     SELECT grp, perms_to_add.id as perm, depth, grantable
188         FROM perms_to_add,
189         permission.grp_perm_map
190         
191         --- Don't add the permissions if they have already been assigned
192         WHERE grp NOT IN
193             (SELECT DISTINCT grp FROM permission.grp_perm_map
194             INNER JOIN perms_to_add ON perm=perms_to_add.id)
195             
196         --- Anybody who can view resources should also see reservations
197         --- at the same level
198         AND perm = (
199             SELECT id
200                 FROM permission.perm_list
201                 WHERE code = 'VIEW_BOOKING_RESOURCE'
202         );
203
204 -- 1239
205
206 INSERT INTO config.workstation_setting_type (name, grp, datatype, label)
207 SELECT
208     'eg.grid.booking.pull_list', 'gui', 'object',
209     oils_i18n_gettext(
210         'booking.pull_list',
211         'Grid Config: Booking Pull List',
212         'cwst', 'label')
213 WHERE NOT EXISTS (
214     SELECT 1
215     FROM config.workstation_setting_type
216     WHERE name = 'eg.grid.booking.pull_list'
217 );
218
219 -- 1240
220
221 INSERT INTO action_trigger.event_params (event_def, param, value)
222 SELECT id, 'check_sms_notify', 1
223 FROM action_trigger.event_definition
224 WHERE reactor = 'SendSMS'
225 AND validator IN ('HoldIsAvailable', 'HoldIsCancelled', 'HoldNotifyCheck')
226 AND NOT EXISTS (
227     SELECT * FROM action_trigger.event_params
228     WHERE param = 'check_sms_notify'
229 );
230
231 -- fill in the gaps, but only if the upgrade log indicates that
232 -- this database had been at version 3.6.0 at some point.
233 INSERT INTO config.upgrade_log (version, applied_to) SELECT '1236', :eg_version
234 WHERE NOT EXISTS (SELECT 1 FROM config.upgrade_log WHERE version = '1236')
235 AND       EXISTS (SELECT 1 FROM config.upgrade_log WHERE version = '3.6.0');
236 INSERT INTO config.upgrade_log (version, applied_to) SELECT '1237', :eg_version
237 WHERE NOT EXISTS (SELECT 1 FROM config.upgrade_log WHERE version = '1237')
238 AND       EXISTS (SELECT 1 FROM config.upgrade_log WHERE version = '3.6.0');
239 INSERT INTO config.upgrade_log (version, applied_to) SELECT '1238', :eg_version
240 WHERE NOT EXISTS (SELECT 1 FROM config.upgrade_log WHERE version = '1238')
241 AND       EXISTS (SELECT 1 FROM config.upgrade_log WHERE version = '3.6.0');
242 INSERT INTO config.upgrade_log (version, applied_to) SELECT '1239', :eg_version
243 WHERE NOT EXISTS (SELECT 1 FROM config.upgrade_log WHERE version = '1239')
244 AND       EXISTS (SELECT 1 FROM config.upgrade_log WHERE version = '3.6.0');
245 INSERT INTO config.upgrade_log (version, applied_to) SELECT '1240', :eg_version
246 WHERE NOT EXISTS (SELECT 1 FROM config.upgrade_log WHERE version = '1240')
247 AND       EXISTS (SELECT 1 FROM config.upgrade_log WHERE version = '3.6.0');
248
249
250 -- check whether patch can be applied
251 SELECT evergreen.upgrade_deps_block_check('1377', :eg_version);
252
253 -- 950.data.seed-values.sql
254
255 INSERT INTO config.global_flag (name, value, enabled, label)
256 VALUES (
257     'opac.login_redirect_domains',
258     '',
259     TRUE,
260     oils_i18n_gettext(
261         'opac.login_redirect_domains',
262         'Restrict post-login redirection to local URLs, or those that match the supplied comma-separated list of foreign domains or host names.',
263         'cgf', 'label'
264     )
265 );
266
267
268
269 SELECT evergreen.upgrade_deps_block_check('1378', :eg_version);
270
271 CREATE OR REPLACE FUNCTION search.highlight_display_fields(
272     rid         BIGINT,
273     tsq_map     TEXT, -- '(a | b) & c' => '1,2,3,4', ...
274     css_class   TEXT DEFAULT 'oils_SH',
275     hl_all      BOOL DEFAULT TRUE,
276     minwords    INT DEFAULT 5,
277     maxwords    INT DEFAULT 25,
278     shortwords  INT DEFAULT 0,
279     maxfrags    INT DEFAULT 0,
280     delimiter   TEXT DEFAULT ' ... '
281 ) RETURNS SETOF search.highlight_result AS $f$
282 DECLARE
283     tsq         TEXT;
284     fields      TEXT;
285     afields     INT[];
286     seen        INT[];
287 BEGIN
288
289     FOR tsq, fields IN SELECT key, value FROM each(tsq_map::HSTORE) LOOP
290         SELECT  ARRAY_AGG(unnest::INT) INTO afields
291           FROM  unnest(regexp_split_to_array(fields,','));
292         seen := seen || afields;
293
294         RETURN QUERY
295             SELECT * FROM search.highlight_display_fields_impl(
296                 rid, tsq, afields, css_class, hl_all,minwords,
297                 maxwords, shortwords, maxfrags, delimiter
298             );
299     END LOOP;
300
301     RETURN QUERY
302         SELECT  id,
303                 source,
304                 field,
305                 evergreen.escape_for_html(value) AS value,
306                 evergreen.escape_for_html(value) AS highlight
307           FROM  metabib.display_entry
308           WHERE source = rid
309                 AND NOT (field = ANY (seen));
310 END;
311 $f$ LANGUAGE PLPGSQL ROWS 10;
312
313
314 COMMIT;
315
316 -- Update auditor tables to catch changes to source tables.
317 --   Can be removed/skipped if there were no schema changes.
318 SELECT auditor.update_auditors();