]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/version-upgrade/3.3.4-3.3.5-upgrade-db.sql
LP 2061136 follow-up: ng lint --fix
[Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 3.3.4-3.3.5-upgrade-db.sql
1 --Upgrade Script for 3.3.4 to 3.3.5
2 \set eg_version '''3.3.5'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('3.3.5', :eg_version);
5
6 SELECT evergreen.upgrade_deps_block_check('1185', :eg_version); -- csharp / gmcharlt / jboyer
7
8 ALTER FUNCTION permission.grp_descendants( INT ) STABLE;
9
10
11 SELECT evergreen.upgrade_deps_block_check('1187', :eg_version);
12
13 CREATE OR REPLACE FUNCTION action.age_circ_on_delete () RETURNS TRIGGER AS $$
14 DECLARE
15 found char := 'N';
16 BEGIN
17
18     -- If there are any renewals for this circulation, don't archive or delete
19     -- it yet.   We'll do so later, when we archive and delete the renewals.
20
21     SELECT 'Y' INTO found
22     FROM action.circulation
23     WHERE parent_circ = OLD.id
24     LIMIT 1;
25
26     IF found = 'Y' THEN
27         RETURN NULL;  -- don't delete
28         END IF;
29
30     -- Archive a copy of the old row to action.aged_circulation
31
32     INSERT INTO action.aged_circulation
33         (id,usr_post_code, usr_home_ou, usr_profile, usr_birth_year, copy_call_number, copy_location,
34         copy_owning_lib, copy_circ_lib, copy_bib_record, xact_start, xact_finish, target_copy,
35         circ_lib, circ_staff, checkin_staff, checkin_lib, renewal_remaining, grace_period, due_date,
36         stop_fines_time, checkin_time, create_time, duration, fine_interval, recurring_fine,
37         max_fine, phone_renewal, desk_renewal, opac_renewal, duration_rule, recurring_fine_rule,
38         max_fine_rule, stop_fines, workstation, checkin_workstation, checkin_scan_time, parent_circ,
39         auto_renewal, auto_renewal_remaining)
40       SELECT
41         id,usr_post_code, usr_home_ou, usr_profile, usr_birth_year, copy_call_number, copy_location,
42         copy_owning_lib, copy_circ_lib, copy_bib_record, xact_start, xact_finish, target_copy,
43         circ_lib, circ_staff, checkin_staff, checkin_lib, renewal_remaining, grace_period, due_date,
44         stop_fines_time, checkin_time, create_time, duration, fine_interval, recurring_fine,
45         max_fine, phone_renewal, desk_renewal, opac_renewal, duration_rule, recurring_fine_rule,
46         max_fine_rule, stop_fines, workstation, checkin_workstation, checkin_scan_time, parent_circ,
47         auto_renewal, auto_renewal_remaining
48         FROM action.all_circulation WHERE id = OLD.id;
49
50     RETURN OLD;
51 END;
52 $$ LANGUAGE 'plpgsql';
53
54
55
56 SELECT evergreen.upgrade_deps_block_check('1188', :eg_version);
57
58 UPDATE action.circulation SET auto_renewal = FALSE WHERE auto_renewal IS NULL;
59
60 UPDATE action.aged_circulation SET auto_renewal = FALSE WHERE auto_renewal IS NULL;
61
62
63 -- The following two changes cannot occur in a transaction with the
64 -- above updates because we will get an error about not being able to
65 -- alter a table with pending transactions.  They also need to occur
66 -- after the above updates or the SET NOT NULL change will fail.
67
68 ALTER TABLE action.circulation ALTER COLUMN auto_renewal SET DEFAULT FALSE;
69 ALTER TABLE action.circulation ALTER COLUMN auto_renewal SET NOT NULL;
70
71 ALTER TABLE action.aged_circulation ALTER COLUMN auto_renewal SET DEFAULT FALSE;
72 ALTER TABLE action.aged_circulation ALTER COLUMN auto_renewal SET NOT NULL;
73
74 SELECT evergreen.upgrade_deps_block_check('1189', :eg_version);
75
76 CREATE OR REPLACE VIEW action.open_circulation AS
77         SELECT  *
78           FROM  action.circulation
79           WHERE checkin_time IS NULL
80           ORDER BY due_date;
81
82 CREATE OR REPLACE VIEW action.billable_circulations AS
83         SELECT  *
84           FROM  action.circulation
85           WHERE xact_finish IS NULL;
86
87 CREATE OR REPLACE VIEW reporter.overdue_circs AS
88 SELECT  *
89   FROM  "action".circulation
90   WHERE checkin_time is null
91         AND (stop_fines NOT IN ('LOST','CLAIMSRETURNED') OR stop_fines IS NULL)
92         AND due_date < now();
93
94 CREATE OR REPLACE VIEW reporter.circ_type AS
95 SELECT  id,
96         CASE WHEN opac_renewal OR phone_renewal OR desk_renewal OR auto_renewal
97                 THEN 'RENEWAL'
98                 ELSE 'CHECKOUT'
99         END AS "type"
100   FROM  action.circulation;
101
102
103 SELECT evergreen.upgrade_deps_block_check('1190', :eg_version);
104
105 UPDATE action.circulation SET desk_renewal = FALSE WHERE auto_renewal IS TRUE;
106
107 UPDATE action.aged_circulation SET desk_renewal = FALSE WHERE auto_renewal IS TRUE;
108
109
110 SELECT evergreen.upgrade_deps_block_check('1191', :eg_version);
111
112 INSERT INTO permission.perm_list ( id, code, description ) SELECT DISTINCT
113   619,
114   'EDIT_SELF_IN_CLIENT',
115   oils_i18n_gettext(619,
116     'Allow a user to edit their own account in the staff client', 'ppl', 'description'
117   )
118   FROM permission.perm_list
119   WHERE NOT EXISTS (SELECT 1 FROM permission.perm_list WHERE code = 'EDIT_SELF_IN_CLIENT');
120
121
122 SELECT evergreen.upgrade_deps_block_check('1193', :eg_version);
123
124 INSERT INTO config.workstation_setting_type
125     (name, grp, datatype, label)
126 VALUES (
127     'eg.grid.circ.patron.xact_details_details_bills', 'gui', 'object',
128     oils_i18n_gettext(
129     'eg.grid.circ.patron.xact_details_details_bills',
130     'Grid Config: circ.patron.xact_details_details_bills',
131     'cwst', 'label')
132 ), (
133     'eg.grid.circ.patron.xact_details_details_payments', 'gui', 'object',
134     oils_i18n_gettext(
135     'eg.grid.circ.patron.xact_details_details_payments',
136     'Grid Config: circ.patron.xact_details_details_payments',
137     'cwst', 'label')
138 );
139
140
141 SELECT evergreen.upgrade_deps_block_check('1195', :eg_version);
142
143 CREATE OR REPLACE FUNCTION metabib.suggest_browse_entries(raw_query_text text, search_class text, headline_opts text, visibility_org integer, query_limit integer, normalization integer)
144  RETURNS TABLE(value text, field integer, buoyant_and_class_match boolean, field_match boolean, field_weight integer, rank real, buoyant boolean, match text)
145 AS $f$
146 DECLARE
147     prepared_query_texts    TEXT[];
148     query                   TSQUERY;
149     plain_query             TSQUERY;
150     opac_visibility_join    TEXT;
151     search_class_join       TEXT;
152     r_fields                RECORD;
153     b_tests                 TEXT := '';
154 BEGIN
155     prepared_query_texts := metabib.autosuggest_prepare_tsquery(raw_query_text);
156
157     query := TO_TSQUERY('keyword', prepared_query_texts[1]);
158     plain_query := TO_TSQUERY('keyword', prepared_query_texts[2]);
159
160     visibility_org := NULLIF(visibility_org,-1);
161     IF visibility_org IS NOT NULL THEN
162         PERFORM FROM actor.org_unit WHERE id = visibility_org AND parent_ou IS NULL;
163         IF FOUND THEN
164             opac_visibility_join := '';
165         ELSE
166             PERFORM 1 FROM config.internal_flag WHERE enabled AND name = 'opac.located_uri.act_as_copy';
167             IF FOUND THEN
168                 b_tests := search.calculate_visibility_attribute_test(
169                     'luri_org',
170                     (SELECT ARRAY_AGG(id) FROM actor.org_unit_full_path(visibility_org))
171                 );
172             ELSE
173                 b_tests := search.calculate_visibility_attribute_test(
174                     'luri_org',
175                     (SELECT ARRAY_AGG(id) FROM actor.org_unit_ancestors(visibility_org))
176                 );
177             END IF;
178             opac_visibility_join := '
179     LEFT JOIN asset.copy_vis_attr_cache acvac ON (acvac.record = x.source)
180     LEFT JOIN biblio.record_entry b ON (b.id = x.source)
181     JOIN vm ON (acvac.vis_attr_vector @@
182             (vm.c_attrs || $$&$$ ||
183                 search.calculate_visibility_attribute_test(
184                     $$circ_lib$$,
185                     (SELECT ARRAY_AGG(id) FROM actor.org_unit_descendants($4))
186                 )
187             )::query_int
188          ) OR (b.vis_attr_vector @@ $$' || b_tests || '$$::query_int)
189 ';
190         END IF;
191     ELSE
192         opac_visibility_join := '';
193     END IF;
194
195     -- The following determines whether we only provide suggestsons matching
196     -- the user's selected search_class, or whether we show other suggestions
197     -- too. The reason for MIN() is that for search_classes like
198     -- 'title|proper|uniform' you would otherwise get multiple rows.  The
199     -- implication is that if title as a class doesn't have restrict,
200     -- nor does the proper field, but the uniform field does, you're going
201     -- to get 'false' for your overall evaluation of 'should we restrict?'
202     -- To invert that, change from MIN() to MAX().
203
204     SELECT
205         INTO r_fields
206             MIN(cmc.restrict::INT) AS restrict_class,
207             MIN(cmf.restrict::INT) AS restrict_field
208         FROM metabib.search_class_to_registered_components(search_class)
209             AS _registered (field_class TEXT, field INT)
210         JOIN
211             config.metabib_class cmc ON (cmc.name = _registered.field_class)
212         LEFT JOIN
213             config.metabib_field cmf ON (cmf.id = _registered.field);
214
215     -- evaluate 'should we restrict?'
216     IF r_fields.restrict_field::BOOL OR r_fields.restrict_class::BOOL THEN
217         search_class_join := '
218     JOIN
219         metabib.search_class_to_registered_components($2)
220         AS _registered (field_class TEXT, field INT) ON (
221             (_registered.field IS NULL AND
222                 _registered.field_class = cmf.field_class) OR
223             (_registered.field = cmf.id)
224         )
225     ';
226     ELSE
227         search_class_join := '
228     LEFT JOIN
229         metabib.search_class_to_registered_components($2)
230         AS _registered (field_class TEXT, field INT) ON (
231             _registered.field_class = cmc.name
232         )
233     ';
234     END IF;
235
236     RETURN QUERY EXECUTE '
237 WITH vm AS ( SELECT * FROM asset.patron_default_visibility_mask() ),
238      mbe AS (SELECT * FROM metabib.browse_entry WHERE index_vector @@ $1 LIMIT 10000)
239 SELECT  DISTINCT
240         x.value,
241         x.id,
242         x.push,
243         x.restrict,
244         x.weight,
245         x.ts_rank_cd,
246         x.buoyant,
247         TS_HEADLINE(value, $7, $3)
248   FROM  (SELECT DISTINCT
249                 mbe.value,
250                 cmf.id,
251                 cmc.buoyant AND _registered.field_class IS NOT NULL AS push,
252                 _registered.field = cmf.id AS restrict,
253                 cmf.weight,
254                 TS_RANK_CD(mbe.index_vector, $1, $6),
255                 cmc.buoyant,
256                 mbedm.source
257           FROM  metabib.browse_entry_def_map mbedm
258                 JOIN mbe ON (mbe.id = mbedm.entry)
259                 JOIN config.metabib_field cmf ON (cmf.id = mbedm.def)
260                 JOIN config.metabib_class cmc ON (cmf.field_class = cmc.name)
261                 '  || search_class_join || '
262           ORDER BY 3 DESC, 4 DESC NULLS LAST, 5 DESC, 6 DESC, 7 DESC, 1 ASC
263           LIMIT 1000) AS x
264         ' || opac_visibility_join || '
265   ORDER BY 3 DESC, 4 DESC NULLS LAST, 5 DESC, 6 DESC, 7 DESC, 1 ASC
266   LIMIT $5
267 '   -- sic, repeat the order by clause in the outer select too
268     USING
269         query, search_class, headline_opts,
270         visibility_org, query_limit, normalization, plain_query
271         ;
272
273     -- sort order:
274     --  buoyant AND chosen class = match class
275     --  chosen field = match field
276     --  field weight
277     --  rank
278     --  buoyancy
279     --  value itself
280
281 END;
282 $f$ LANGUAGE plpgsql ROWS 10;
283
284 COMMIT;