]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/version-upgrade/2.2.0-2.2.1-upgrade-db.sql
LP1779158 Angular7 and ng-lint updates
[Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 2.2.0-2.2.1-upgrade-db.sql
1 --Upgrade Script for 2.2.0 to 2.2.1
2 \set eg_version '''2.2.1'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('2.2.1', :eg_version);
5 -- Evergreen DB patch 0722.schema.acq-po-state-constraint.sql
6 --
7
8
9 -- check whether patch can be applied
10 SELECT evergreen.upgrade_deps_block_check('0722', :eg_version);
11
12 ALTER TABLE acq.purchase_order ADD CONSTRAINT valid_po_state 
13     CHECK (state IN ('new','pending','on-order','received','cancelled'));
14
15 -- Evergreen DB patch 0723.schema.function.get_locale_name.sql
16 --
17
18
19 -- check whether patch can be applied
20 SELECT evergreen.upgrade_deps_block_check('0723', :eg_version);
21
22 CREATE OR REPLACE FUNCTION evergreen.get_locale_name(
23     IN locale TEXT,
24     OUT name TEXT,
25     OUT description TEXT
26 ) AS $$
27 DECLARE
28     eg_locale TEXT;
29 BEGIN
30     eg_locale := LOWER(SUBSTRING(locale FROM 1 FOR 2)) || '-' || UPPER(SUBSTRING(locale FROM 4 FOR 2));
31         
32     SELECT i18nc.string INTO name
33     FROM config.i18n_locale i18nl
34        INNER JOIN config.i18n_core i18nc ON i18nl.code = i18nc.translation
35     WHERE i18nc.identity_value = eg_locale
36        AND code = eg_locale
37        AND i18nc.fq_field = 'i18n_l.name';
38
39     IF name IS NULL THEN
40        SELECT i18nl.name INTO name
41        FROM config.i18n_locale i18nl
42        WHERE code = eg_locale;
43     END IF;
44
45     SELECT i18nc.string INTO description
46     FROM config.i18n_locale i18nl
47        INNER JOIN config.i18n_core i18nc ON i18nl.code = i18nc.translation
48     WHERE i18nc.identity_value = eg_locale
49        AND code = eg_locale
50        AND i18nc.fq_field = 'i18n_l.description';
51
52     IF description IS NULL THEN
53        SELECT i18nl.description INTO description
54        FROM config.i18n_locale i18nl
55        WHERE code = eg_locale;
56     END IF;
57 END;
58 $$ LANGUAGE PLPGSQL COST 1 STABLE;
59
60
61 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('0724', :eg_version); -- denials/gmcharlt
62
63 CREATE OR REPLACE FUNCTION evergreen.maintain_901 () RETURNS TRIGGER AS $func$
64 use strict;
65 use MARC::Record;
66 use MARC::File::XML (BinaryEncoding => 'UTF-8');
67 use MARC::Charset;
68 use Encode;
69 use Unicode::Normalize;
70
71 MARC::Charset->assume_unicode(1);
72
73 my $schema = $_TD->{table_schema};
74 my $marc = MARC::Record->new_from_xml($_TD->{new}{marc});
75
76 my @old901s = $marc->field('901');
77 $marc->delete_fields(@old901s);
78
79 if ($schema eq 'biblio') {
80     my $tcn_value = $_TD->{new}{tcn_value};
81
82     # Set TCN value to record ID?
83     my $id_as_tcn = spi_exec_query("
84         SELECT enabled
85         FROM config.global_flag
86         WHERE name = 'cat.bib.use_id_for_tcn'
87     ");
88     if (($id_as_tcn->{processed}) && $id_as_tcn->{rows}[0]->{enabled} eq 't') {
89         $tcn_value = $_TD->{new}{id}; 
90     }
91
92     my $new_901 = MARC::Field->new("901", " ", " ",
93         "a" => $tcn_value,
94         "b" => $_TD->{new}{tcn_source},
95         "c" => $_TD->{new}{id},
96         "t" => $schema
97     );
98
99     if ($_TD->{new}{owner}) {
100         $new_901->add_subfields("o" => $_TD->{new}{owner});
101     }
102
103     if ($_TD->{new}{share_depth}) {
104         $new_901->add_subfields("d" => $_TD->{new}{share_depth});
105     }
106
107     $marc->append_fields($new_901);
108 } elsif ($schema = 'authority') {
109     $marc->append_fields(
110         ["901", " ", " ",
111             "c" => $_TD->{new}{id},
112             "t" => $schema,
113         ]
114     );
115 } elsif ($schema = 'serial') {
116     my $new_901 = MARC::Field->new("901", " ", " ",
117         "c" => $_TD->{new}{id},
118         "t" => $schema,
119         "o" => $_TD->{new}{owning_lib},
120     );
121
122     if ($_TD->{new}{record}) {
123         $new_901->add_subfields("r" => $_TD->{new}{record});
124     }
125
126     $marc->append_fields($new_901);
127 } else {
128     $marc->append_fields(
129         ["901", " ", " ",
130             "c" => $_TD->{new}{id},
131             "t" => $schema,
132         ]
133     );
134 }
135
136 my $xml = $marc->as_xml_record();
137 $xml =~ s/\n//sgo;
138 $xml =~ s/^<\?xml.+\?\s*>//go;
139 $xml =~ s/>\s+</></go;
140 $xml =~ s/\p{Cc}//go;
141
142 # Embed a version of OpenILS::Application::AppUtils->entityize()
143 # to avoid having to set PERL5LIB for PostgreSQL as well
144
145 # If we are going to convert non-ASCII characters to XML entities,
146 # we had better be dealing with a UTF8 string to begin with
147 $xml = decode_utf8($xml);
148
149 $xml = NFC($xml);
150
151 # Convert raw ampersands to entities
152 $xml =~ s/&(?!\S+;)/&amp;/gso;
153
154 # Convert Unicode characters to entities
155 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
156
157 $xml =~ s/[\x00-\x1f]//go;
158 $_TD->{new}{marc} = $xml;
159
160 return "MODIFY";
161 $func$ LANGUAGE PLPERLU;
162
163
164 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('0725', :eg_version); -- gmcharlt/denials
165
166 CREATE OR REPLACE FUNCTION evergreen.maintain_901 () RETURNS TRIGGER AS $func$
167 use strict;
168 use MARC::Record;
169 use MARC::File::XML (BinaryEncoding => 'UTF-8');
170 use MARC::Charset;
171 use Encode;
172 use Unicode::Normalize;
173
174 MARC::Charset->assume_unicode(1);
175
176 my $schema = $_TD->{table_schema};
177 my $marc = MARC::Record->new_from_xml($_TD->{new}{marc});
178
179 my @old901s = $marc->field('901');
180 $marc->delete_fields(@old901s);
181
182 if ($schema eq 'biblio') {
183     my $tcn_value = $_TD->{new}{tcn_value};
184
185     # Set TCN value to record ID?
186     my $id_as_tcn = spi_exec_query("
187         SELECT enabled
188         FROM config.global_flag
189         WHERE name = 'cat.bib.use_id_for_tcn'
190     ");
191     if (($id_as_tcn->{processed}) && $id_as_tcn->{rows}[0]->{enabled} eq 't') {
192         $tcn_value = $_TD->{new}{id}; 
193     }
194
195     my $new_901 = MARC::Field->new("901", " ", " ",
196         "a" => $tcn_value,
197         "b" => $_TD->{new}{tcn_source},
198         "c" => $_TD->{new}{id},
199         "t" => $schema
200     );
201
202     if ($_TD->{new}{owner}) {
203         $new_901->add_subfields("o" => $_TD->{new}{owner});
204     }
205
206     if ($_TD->{new}{share_depth}) {
207         $new_901->add_subfields("d" => $_TD->{new}{share_depth});
208     }
209
210     $marc->append_fields($new_901);
211 } elsif ($schema eq 'authority') {
212     $marc->append_fields(
213         ["901", " ", " ",
214             "c" => $_TD->{new}{id},
215             "t" => $schema,
216         ]
217     );
218 } elsif ($schema eq 'serial') {
219     my $new_901 = MARC::Field->new("901", " ", " ",
220         "c" => $_TD->{new}{id},
221         "t" => $schema,
222         "o" => $_TD->{new}{owning_lib},
223     );
224
225     if ($_TD->{new}{record}) {
226         $new_901->add_subfields("r" => $_TD->{new}{record});
227     }
228
229     $marc->append_fields($new_901);
230 } else {
231     $marc->append_fields(
232         ["901", " ", " ",
233             "c" => $_TD->{new}{id},
234             "t" => $schema,
235         ]
236     );
237 }
238
239 my $xml = $marc->as_xml_record();
240 $xml =~ s/\n//sgo;
241 $xml =~ s/^<\?xml.+\?\s*>//go;
242 $xml =~ s/>\s+</></go;
243 $xml =~ s/\p{Cc}//go;
244
245 # Embed a version of OpenILS::Application::AppUtils->entityize()
246 # to avoid having to set PERL5LIB for PostgreSQL as well
247
248 # If we are going to convert non-ASCII characters to XML entities,
249 # we had better be dealing with a UTF8 string to begin with
250 $xml = decode_utf8($xml);
251
252 $xml = NFC($xml);
253
254 # Convert raw ampersands to entities
255 $xml =~ s/&(?!\S+;)/&amp;/gso;
256
257 # Convert Unicode characters to entities
258 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
259
260 $xml =~ s/[\x00-\x1f]//go;
261 $_TD->{new}{marc} = $xml;
262
263 return "MODIFY";
264 $func$ LANGUAGE PLPERLU;
265
266
267 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('0726', :eg_version); -- denials
268
269 CREATE OR REPLACE FUNCTION evergreen.maintain_901 () RETURNS TRIGGER AS $func$
270 use strict;
271 use MARC::Record;
272 use MARC::File::XML (BinaryEncoding => 'UTF-8');
273 use MARC::Charset;
274 use Encode;
275 use Unicode::Normalize;
276
277 MARC::Charset->assume_unicode(1);
278
279 my $schema = $_TD->{table_schema};
280 my $marc = MARC::Record->new_from_xml($_TD->{new}{marc});
281
282 my @old901s = $marc->field('901');
283 $marc->delete_fields(@old901s);
284
285 if ($schema eq 'biblio') {
286     my $tcn_value = $_TD->{new}{tcn_value};
287
288     # Set TCN value to record ID?
289     my $id_as_tcn = spi_exec_query("
290         SELECT enabled
291         FROM config.global_flag
292         WHERE name = 'cat.bib.use_id_for_tcn'
293     ");
294     if (($id_as_tcn->{processed}) && $id_as_tcn->{rows}[0]->{enabled} eq 't') {
295         $tcn_value = $_TD->{new}{id}; 
296     }
297
298     my $new_901 = MARC::Field->new("901", " ", " ",
299         "a" => $tcn_value,
300         "b" => $_TD->{new}{tcn_source},
301         "c" => $_TD->{new}{id},
302         "t" => $schema
303     );
304
305     if ($_TD->{new}{owner}) {
306         $new_901->add_subfields("o" => $_TD->{new}{owner});
307     }
308
309     if ($_TD->{new}{share_depth}) {
310         $new_901->add_subfields("d" => $_TD->{new}{share_depth});
311     }
312
313     $marc->append_fields($new_901);
314 } elsif ($schema eq 'authority') {
315     my $new_901 = MARC::Field->new("901", " ", " ",
316         "c" => $_TD->{new}{id},
317         "t" => $schema,
318     );
319     $marc->append_fields($new_901);
320 } elsif ($schema eq 'serial') {
321     my $new_901 = MARC::Field->new("901", " ", " ",
322         "c" => $_TD->{new}{id},
323         "t" => $schema,
324         "o" => $_TD->{new}{owning_lib},
325     );
326
327     if ($_TD->{new}{record}) {
328         $new_901->add_subfields("r" => $_TD->{new}{record});
329     }
330
331     $marc->append_fields($new_901);
332 } else {
333     my $new_901 = MARC::Field->new("901", " ", " ",
334         "c" => $_TD->{new}{id},
335         "t" => $schema,
336     );
337     $marc->append_fields($new_901);
338 }
339
340 my $xml = $marc->as_xml_record();
341 $xml =~ s/\n//sgo;
342 $xml =~ s/^<\?xml.+\?\s*>//go;
343 $xml =~ s/>\s+</></go;
344 $xml =~ s/\p{Cc}//go;
345
346 # Embed a version of OpenILS::Application::AppUtils->entityize()
347 # to avoid having to set PERL5LIB for PostgreSQL as well
348
349 # If we are going to convert non-ASCII characters to XML entities,
350 # we had better be dealing with a UTF8 string to begin with
351 $xml = decode_utf8($xml);
352
353 $xml = NFC($xml);
354
355 # Convert raw ampersands to entities
356 $xml =~ s/&(?!\S+;)/&amp;/gso;
357
358 # Convert Unicode characters to entities
359 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
360
361 $xml =~ s/[\x00-\x1f]//go;
362 $_TD->{new}{marc} = $xml;
363
364 return "MODIFY";
365 $func$ LANGUAGE PLPERLU;
366
367 COMMIT;