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