1 -- Copyright 2009-2012, Equinox Software, Inc.
3 -- This program is free software; you can redistribute it and/or
4 -- modify it under the terms of the GNU General Public License
5 -- as published by the Free Software Foundation; either version 2
6 -- of the License, or (at your option) any later version.
8 -- This program is distributed in the hope that it will be useful,
9 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
10 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 -- GNU General Public License for more details.
13 -- You should have received a copy of the GNU General Public License
14 -- along with this program; if not, write to the Free Software
15 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 --------------------------------------------------------------------------
18 -- An example of how to use:
20 -- DROP SCHEMA foo CASCADE; CREATE SCHEMA foo;
22 -- SELECT migration_tools.init('foo');
23 -- SELECT migration_tools.build('foo');
24 -- SELECT * FROM foo.fields_requiring_mapping;
26 -- create some incoming ILS specific staging tables, like CREATE foo.legacy_items ( l_barcode TEXT, .. ) INHERITS (foo.asset_copy);
27 -- Do some mapping, like UPDATE foo.legacy_items SET barcode = TRIM(BOTH ' ' FROM l_barcode);
28 -- Then, to move into production, do: select migration_tools.insert_base_into_production('foo')
30 CREATE SCHEMA migration_tools;
32 CREATE OR REPLACE FUNCTION migration_tools.production_tables (TEXT) RETURNS TEXT[] AS $$
34 migration_schema ALIAS FOR $1;
38 EXECUTE 'SELECT string_to_array(value,'','') AS tables FROM ' || migration_schema || '.config WHERE key = ''production_tables'';'
43 $$ LANGUAGE PLPGSQL STRICT STABLE;
45 CREATE OR REPLACE FUNCTION migration_tools.country_code (TEXT) RETURNS TEXT AS $$
47 migration_schema ALIAS FOR $1;
51 EXECUTE 'SELECT value FROM ' || migration_schema || '.config WHERE key = ''country_code'';'
56 $$ LANGUAGE PLPGSQL STRICT STABLE;
59 CREATE OR REPLACE FUNCTION migration_tools.log (TEXT,TEXT,INTEGER) RETURNS VOID AS $$
61 migration_schema ALIAS FOR $1;
65 EXECUTE 'INSERT INTO ' || migration_schema || '.sql_log ( sql, row_count ) VALUES ( ' || quote_literal(sql) || ', ' || nrows || ' );';
67 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
69 CREATE OR REPLACE FUNCTION migration_tools.exec (TEXT,TEXT) RETURNS VOID AS $$
71 migration_schema ALIAS FOR $1;
75 EXECUTE 'UPDATE ' || migration_schema || '.sql_current SET sql = ' || quote_literal(sql) || ';';
76 --RAISE INFO '%', sql;
78 GET DIAGNOSTICS nrows = ROW_COUNT;
79 PERFORM migration_tools.log(migration_schema,sql,nrows);
82 RAISE EXCEPTION '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
84 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
86 CREATE OR REPLACE FUNCTION migration_tools.debug_exec (TEXT,TEXT) RETURNS VOID AS $$
88 migration_schema ALIAS FOR $1;
92 EXECUTE 'UPDATE ' || migration_schema || '.sql_current SET sql = ' || quote_literal(sql) || ';';
93 RAISE INFO 'debug_exec sql = %', sql;
95 GET DIAGNOSTICS nrows = ROW_COUNT;
96 PERFORM migration_tools.log(migration_schema,sql,nrows);
99 RAISE EXCEPTION '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
101 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
103 CREATE OR REPLACE FUNCTION migration_tools.init (TEXT) RETURNS VOID AS $$
105 migration_schema ALIAS FOR $1;
108 EXECUTE 'DROP TABLE IF EXISTS ' || migration_schema || '.sql_current;';
109 EXECUTE 'CREATE TABLE ' || migration_schema || '.sql_current ( sql TEXT);';
110 EXECUTE 'INSERT INTO ' || migration_schema || '.sql_current ( sql ) VALUES ( '''' );';
112 SELECT 'CREATE TABLE ' || migration_schema || '.sql_log ( time TIMESTAMP NOT NULL DEFAULT NOW(), row_count INTEGER, sql TEXT );' INTO STRICT sql;
116 RAISE INFO '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
118 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.config;' );
119 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.config ( key TEXT UNIQUE, value TEXT);' );
120 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''production_tables'', ''asset.call_number,asset.call_number_prefix,asset.call_number_suffix,asset.copy_location,asset.copy,asset.copy_alert,asset.stat_cat,asset.stat_cat_entry,asset.stat_cat_entry_copy_map,asset.copy_note,actor.usr,actor.card,actor.usr_address,actor.stat_cat,actor.stat_cat_entry,actor.stat_cat_entry_usr_map,actor.usr_note,actor.usr_standing_penalty,actor.usr_setting,action.circulation,action.hold_request,action.hold_notification,action.hold_request_note,action.hold_transit_copy,action.transit_copy,money.grocery,money.billing,money.cash_payment,money.forgive_payment,acq.provider,acq.provider_address,acq.provider_note,acq.provider_contact,acq.provider_contact_address,acq.fund,acq.fund_allocation,acq.fund_tag,acq.fund_tag_map,acq.funding_source,acq.funding_source_credit,acq.lineitem,acq.purchase_order,acq.po_item,acq.invoice,acq.invoice_item,acq.invoice_entry,acq.lineitem_detail,acq.fund_debit,acq.fund_transfer,acq.po_note,config.circ_matrix_matchpoint,config.circ_matrix_limit_set_map,config.hold_matrix_matchpoint,asset.copy_tag,asset.copy_tag_copy_map,config.copy_tag_type,serial.item,serial.item_note,serial.record_entry,biblio.record_entry'' );' );
121 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''country_code'', ''USA'' );' );
122 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.fields_requiring_mapping;' );
123 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.fields_requiring_mapping( table_schema TEXT, table_name TEXT, column_name TEXT, data_type TEXT);' );
124 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.base_profile_map;' );
125 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || E'.base_profile_map (
128 transcribed_perm_group TEXT,
136 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''base_profile_map'', ''base_profile_map'' );' );
137 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.base_item_dynamic_field_map;' );
138 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || E'.base_item_dynamic_field_map (
140 evergreen_field TEXT,
141 evergreen_value TEXT,
142 evergreen_datatype TEXT,
150 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_item_dynamic_lf1_idx ON ' || migration_schema || '.base_item_dynamic_field_map (legacy_field1,legacy_value1);' );
151 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_item_dynamic_lf2_idx ON ' || migration_schema || '.base_item_dynamic_field_map (legacy_field2,legacy_value2);' );
152 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_item_dynamic_lf3_idx ON ' || migration_schema || '.base_item_dynamic_field_map (legacy_field3,legacy_value3);' );
153 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''base_item_dynamic_field_map'', ''base_item_dynamic_field_map'' );' );
154 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.base_copy_location_map;' );
155 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || E'.base_copy_location_map (
158 holdable BOOLEAN NOT NULL DEFAULT TRUE,
159 hold_verify BOOLEAN NOT NULL DEFAULT FALSE,
160 opac_visible BOOLEAN NOT NULL DEFAULT TRUE,
161 circulate BOOLEAN NOT NULL DEFAULT TRUE,
162 transcribed_location TEXT,
170 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_base_copy_location_lf1_idx ON ' || migration_schema || '.base_copy_location_map (legacy_field1,legacy_value1);' );
171 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_base_copy_location_lf2_idx ON ' || migration_schema || '.base_copy_location_map (legacy_field2,legacy_value2);' );
172 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_base_copy_location_lf3_idx ON ' || migration_schema || '.base_copy_location_map (legacy_field3,legacy_value3);' );
173 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_base_copy_location_loc_idx ON ' || migration_schema || '.base_copy_location_map (transcribed_location);' );
174 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''base_copy_location_map'', ''base_copy_location_map'' );' );
175 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.base_circ_field_map;' );
176 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || E'.base_circ_field_map (
194 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_circ_dynamic_lf1_idx ON ' || migration_schema || '.base_circ_field_map (item_field1,item_value1);' );
195 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_circ_dynamic_lf2_idx ON ' || migration_schema || '.base_circ_field_map (item_field2,item_value2);' );
196 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_circ_dynamic_lf3_idx ON ' || migration_schema || '.base_circ_field_map (patron_field1,patron_value1);' );
197 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_circ_dynamic_lf4_idx ON ' || migration_schema || '.base_circ_field_map (patron_field2,patron_value2);' );
198 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''base_circ_field_map'', ''base_circ_field_map'' );' );
201 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_init'', now() );' );
203 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_init'';' );
206 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
208 CREATE OR REPLACE FUNCTION migration_tools.build (TEXT) RETURNS VOID AS $$
210 migration_schema ALIAS FOR $1;
211 production_tables TEXT[];
213 --RAISE INFO 'In migration_tools.build(%)', migration_schema;
214 SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
215 PERFORM migration_tools.build_base_staging_tables(migration_schema,production_tables);
216 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_barcode_key ON ' || migration_schema || '.actor_card ( barcode );' );
217 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_usrname_key ON ' || migration_schema || '.actor_usr ( usrname );' );
218 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_copy_barcode_key ON ' || migration_schema || '.asset_copy ( barcode );' );
219 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_copy_id_key ON ' || migration_schema || '.asset_copy ( id );' );
220 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_callnum_record_idx ON ' || migration_schema || '.asset_call_number ( record );' );
221 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_callnum_upper_label_id_lib_idx ON ' || migration_schema || '.asset_call_number ( UPPER(label),id,owning_lib );' );
222 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_callnum_label_once_per_lib ON ' || migration_schema || '.asset_call_number ( record,owning_lib,label,prefix,suffix );' );
224 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
226 CREATE OR REPLACE FUNCTION migration_tools.build_base_staging_tables (TEXT,TEXT[]) RETURNS VOID AS $$
228 migration_schema ALIAS FOR $1;
229 production_tables ALIAS FOR $2;
231 --RAISE INFO 'In migration_tools.build_base_staging_tables(%,%)', migration_schema, production_tables;
232 FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
233 PERFORM migration_tools.build_specific_base_staging_table(migration_schema,production_tables[i]);
236 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
238 CREATE OR REPLACE FUNCTION migration_tools.build_specific_base_staging_table (TEXT,TEXT) RETURNS VOID AS $$
240 migration_schema ALIAS FOR $1;
241 production_table ALIAS FOR $2;
242 base_staging_table TEXT;
245 base_staging_table = REPLACE( production_table, '.', '_' );
246 --RAISE INFO 'In migration_tools.build_specific_base_staging_table(%,%) -> %', migration_schema, production_table, base_staging_table;
247 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.' || base_staging_table || ' ( LIKE ' || production_table || ' INCLUDING DEFAULTS EXCLUDING CONSTRAINTS );' );
248 PERFORM migration_tools.exec( $1, '
249 INSERT INTO ' || migration_schema || '.fields_requiring_mapping
250 SELECT table_schema, table_name, column_name, data_type
251 FROM information_schema.columns
252 WHERE table_schema = ''' || migration_schema || ''' AND table_name = ''' || base_staging_table || ''' AND is_nullable = ''NO'' AND column_default IS NULL;
255 SELECT table_schema, table_name, column_name, data_type
256 FROM information_schema.columns
257 WHERE table_schema = migration_schema AND table_name = base_staging_table AND is_nullable = 'NO' AND column_default IS NULL
259 PERFORM migration_tools.exec( $1, 'ALTER TABLE ' || columns.table_schema || '.' || columns.table_name || ' ALTER COLUMN ' || columns.column_name || ' DROP NOT NULL;' );
262 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
264 -- creates other child table so you can have more than one child table in a schema from a base table
265 CREATE OR REPLACE FUNCTION build_variant_staging_table(text, text, text)
271 migration_schema ALIAS FOR $1;
272 production_table ALIAS FOR $2;
273 base_staging_table ALIAS FOR $3;
276 --RAISE INFO 'In migration_tools.build_specific_base_staging_table(%,%) -> %', migration_schema, production_table, base_staging_table;
277 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.' || base_staging_table || ' ( LIKE ' || production_table || ' INCLUDING DEFAULTS EXCLUDING CONSTRAINTS );' );
278 PERFORM migration_tools.exec( $1, '
279 INSERT INTO ' || migration_schema || '.fields_requiring_mapping
280 SELECT table_schema, table_name, column_name, data_type
281 FROM information_schema.columns
282 WHERE table_schema = ''' || migration_schema || ''' AND table_name = ''' || base_staging_table || ''' AND is_nullable = ''NO'' AND column_default IS NULL;
285 SELECT table_schema, table_name, column_name, data_type
286 FROM information_schema.columns
287 WHERE table_schema = migration_schema AND table_name = base_staging_table AND is_nullable = 'NO' AND column_default IS NULL
289 PERFORM migration_tools.exec( $1, 'ALTER TABLE ' || columns.table_schema || '.' || columns.table_name || ' ALTER COLUMN ' || columns.column_name || ' DROP NOT NULL;' );
294 CREATE OR REPLACE FUNCTION migration_tools.create_linked_legacy_table_from (TEXT,TEXT,TEXT) RETURNS VOID AS $$
296 migration_schema ALIAS FOR $1;
297 parent_table ALIAS FOR $2;
298 source_table ALIAS FOR $3;
302 column_list TEXT := '';
303 column_count INTEGER := 0;
305 create_sql := 'CREATE TABLE ' || migration_schema || '.' || parent_table || '_legacy ( ';
307 SELECT table_schema, table_name, column_name, data_type
308 FROM information_schema.columns
309 WHERE table_schema = migration_schema AND table_name = source_table
311 column_count := column_count + 1;
312 if column_count > 1 then
313 create_sql := create_sql || ', ';
314 column_list := column_list || ', ';
316 create_sql := create_sql || columns.column_name || ' ';
317 if columns.data_type = 'ARRAY' then
318 create_sql := create_sql || 'TEXT[]';
320 create_sql := create_sql || columns.data_type;
322 column_list := column_list || columns.column_name;
324 create_sql := create_sql || ' ) INHERITS ( ' || migration_schema || '.' || parent_table || ' );';
325 --RAISE INFO 'create_sql = %', create_sql;
327 insert_sql := 'INSERT INTO ' || migration_schema || '.' || parent_table || '_legacy (' || column_list || ') SELECT ' || column_list || ' FROM ' || migration_schema || '.' || source_table || ';';
328 --RAISE INFO 'insert_sql = %', insert_sql;
331 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
333 CREATE OR REPLACE FUNCTION migration_tools.insert_base_into_production (TEXT) RETURNS VOID AS $$
335 migration_schema ALIAS FOR $1;
336 production_tables TEXT[];
338 --RAISE INFO 'In migration_tools.insert_into_production(%)', migration_schema;
339 SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
340 FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
341 PERFORM migration_tools.insert_into_production(migration_schema,production_tables[i]);
344 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
346 CREATE OR REPLACE FUNCTION migration_tools.insert_into_production (TEXT,TEXT) RETURNS VOID AS $$
348 migration_schema ALIAS FOR $1;
349 production_table ALIAS FOR $2;
350 base_staging_table TEXT;
353 base_staging_table = REPLACE( production_table, '.', '_' );
354 --RAISE INFO 'In migration_tools.insert_into_production(%,%) -> %', migration_schema, production_table, base_staging_table;
355 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || production_table || ' SELECT * FROM ' || migration_schema || '.' || base_staging_table || ';' );
357 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
360 CREATE OR REPLACE FUNCTION migration_tools.assert (BOOLEAN) RETURNS VOID AS $$
365 RAISE EXCEPTION 'assertion';
368 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
370 CREATE OR REPLACE FUNCTION migration_tools.assert (BOOLEAN,TEXT) RETURNS VOID AS $$
376 RAISE EXCEPTION '%', msg;
379 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
381 CREATE OR REPLACE FUNCTION migration_tools.assert (BOOLEAN,TEXT,TEXT) RETURNS TEXT AS $$
384 fail_msg ALIAS FOR $2;
385 success_msg ALIAS FOR $3;
388 RAISE EXCEPTION '%', fail_msg;
392 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
394 -- push bib sequence and return starting value for reserved range
395 CREATE OR REPLACE FUNCTION migration_tools.push_bib_sequence(INTEGER) RETURNS BIGINT AS $$
397 bib_count ALIAS FOR $1;
400 PERFORM setval('biblio.record_entry_id_seq',(SELECT MAX(id) FROM biblio.record_entry) + bib_count + 2000);
402 SELECT CEIL(MAX(id)/1000)*1000+1000 FROM biblio.record_entry WHERE id < (SELECT last_value FROM biblio.record_entry_id_seq)
407 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
409 -- set a new salted password
411 CREATE OR REPLACE FUNCTION migration_tools.set_salted_passwd(INTEGER,TEXT) RETURNS BOOLEAN AS $$
414 plain_passwd ALIAS FOR $2;
419 SELECT actor.create_salt('main') INTO plain_salt;
421 SELECT MD5(plain_passwd) INTO md5_passwd;
423 PERFORM actor.set_passwd(usr_id, 'main', MD5(plain_salt || md5_passwd), plain_salt);
428 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
431 -- convenience functions for handling copy_location maps
432 CREATE OR REPLACE FUNCTION migration_tools.handle_shelf (TEXT,TEXT,TEXT,INTEGER) RETURNS VOID AS $$
433 SELECT migration_tools._handle_shelf($1,$2,$3,$4,TRUE);
436 CREATE OR REPLACE FUNCTION migration_tools._handle_shelf (TEXT,TEXT,TEXT,INTEGER,BOOLEAN) RETURNS VOID AS $$
438 table_schema ALIAS FOR $1;
439 table_name ALIAS FOR $2;
440 org_shortname ALIAS FOR $3;
441 org_range ALIAS FOR $4;
442 make_assertion ALIAS FOR $5;
445 -- if x_org is on the mapping table, it'll take precedence over the passed org_shortname param
446 -- though we'll still use the passed org for the full path traversal when needed
453 EXECUTE 'SELECT EXISTS (
455 FROM information_schema.columns
456 WHERE table_schema = $1
458 and column_name = ''desired_shelf''
459 )' INTO proceed USING table_schema, table_name;
461 RAISE EXCEPTION 'Missing column desired_shelf';
464 EXECUTE 'SELECT EXISTS (
466 FROM information_schema.columns
467 WHERE table_schema = $1
469 and column_name = ''x_org''
470 )' INTO x_org_found USING table_schema, table_name;
472 SELECT id INTO org FROM actor.org_unit WHERE shortname = org_shortname;
474 RAISE EXCEPTION 'Cannot find org by shortname';
477 SELECT INTO org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( org );
479 EXECUTE 'ALTER TABLE '
480 || quote_ident(table_name)
481 || ' DROP COLUMN IF EXISTS x_shelf';
482 EXECUTE 'ALTER TABLE '
483 || quote_ident(table_name)
484 || ' ADD COLUMN x_shelf INTEGER';
487 RAISE INFO 'Found x_org column';
488 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
489 || ' SET x_shelf = b.id FROM asset_copy_location b'
490 || ' WHERE BTRIM(UPPER(a.desired_shelf)) = BTRIM(UPPER(b.name))'
491 || ' AND b.owning_lib = x_org'
492 || ' AND NOT b.deleted';
493 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
494 || ' SET x_shelf = b.id FROM asset.copy_location b'
495 || ' WHERE BTRIM(UPPER(a.desired_shelf)) = BTRIM(UPPER(b.name))'
496 || ' AND b.owning_lib = x_org'
497 || ' AND x_shelf IS NULL'
498 || ' AND NOT b.deleted';
500 RAISE INFO 'Did not find x_org column';
501 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
502 || ' SET x_shelf = b.id FROM asset_copy_location b'
503 || ' WHERE BTRIM(UPPER(a.desired_shelf)) = BTRIM(UPPER(b.name))'
504 || ' AND b.owning_lib = $1'
505 || ' AND NOT b.deleted'
507 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
508 || ' SET x_shelf = b.id FROM asset_copy_location b'
509 || ' WHERE BTRIM(UPPER(a.desired_shelf)) = BTRIM(UPPER(b.name))'
510 || ' AND b.owning_lib = $1'
511 || ' AND x_shelf IS NULL'
512 || ' AND NOT b.deleted'
516 FOREACH o IN ARRAY org_list LOOP
517 RAISE INFO 'Considering org %', o;
518 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
519 || ' SET x_shelf = b.id FROM asset.copy_location b'
520 || ' WHERE BTRIM(UPPER(a.desired_shelf)) = BTRIM(UPPER(b.name))'
521 || ' AND b.owning_lib = $1 AND x_shelf IS NULL'
522 || ' AND NOT b.deleted'
524 GET DIAGNOSTICS row_count = ROW_COUNT;
525 RAISE INFO 'Updated % rows', row_count;
528 IF make_assertion THEN
529 EXECUTE 'SELECT migration_tools.assert(
530 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_shelf <> '''' AND x_shelf IS NULL),
531 ''Cannot find a desired location'',
532 ''Found all desired locations''
537 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
539 -- convenience functions for handling circmod maps
541 CREATE OR REPLACE FUNCTION migration_tools.handle_circmod (TEXT,TEXT) RETURNS VOID AS $$
543 table_schema ALIAS FOR $1;
544 table_name ALIAS FOR $2;
547 EXECUTE 'SELECT EXISTS (
549 FROM information_schema.columns
550 WHERE table_schema = $1
552 and column_name = ''desired_circmod''
553 )' INTO proceed USING table_schema, table_name;
555 RAISE EXCEPTION 'Missing column desired_circmod';
558 EXECUTE 'ALTER TABLE '
559 || quote_ident(table_name)
560 || ' DROP COLUMN IF EXISTS x_circmod';
561 EXECUTE 'ALTER TABLE '
562 || quote_ident(table_name)
563 || ' ADD COLUMN x_circmod TEXT';
565 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
566 || ' SET x_circmod = code FROM config.circ_modifier b'
567 || ' WHERE BTRIM(UPPER(a.desired_circmod)) = BTRIM(UPPER(b.code))';
569 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
570 || ' SET x_circmod = code FROM config.circ_modifier b'
571 || ' WHERE BTRIM(UPPER(a.desired_circmod)) = BTRIM(UPPER(b.name))'
572 || ' AND x_circmod IS NULL';
574 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
575 || ' SET x_circmod = code FROM config.circ_modifier b'
576 || ' WHERE BTRIM(UPPER(a.desired_circmod)) = BTRIM(UPPER(b.description))'
577 || ' AND x_circmod IS NULL';
579 EXECUTE 'SELECT migration_tools.assert(
580 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_circmod <> '''' AND x_circmod IS NULL),
581 ''Cannot find a desired circulation modifier'',
582 ''Found all desired circulation modifiers''
586 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
588 -- convenience functions for handling item status maps
590 CREATE OR REPLACE FUNCTION migration_tools.handle_status (TEXT,TEXT) RETURNS VOID AS $$
592 table_schema ALIAS FOR $1;
593 table_name ALIAS FOR $2;
596 EXECUTE 'SELECT EXISTS (
598 FROM information_schema.columns
599 WHERE table_schema = $1
601 and column_name = ''desired_status''
602 )' INTO proceed USING table_schema, table_name;
604 RAISE EXCEPTION 'Missing column desired_status';
607 EXECUTE 'ALTER TABLE '
608 || quote_ident(table_name)
609 || ' DROP COLUMN IF EXISTS x_status';
610 EXECUTE 'ALTER TABLE '
611 || quote_ident(table_name)
612 || ' ADD COLUMN x_status INTEGER';
614 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
615 || ' SET x_status = id FROM config.copy_status b'
616 || ' WHERE BTRIM(UPPER(a.desired_status)) = BTRIM(UPPER(b.name))';
618 EXECUTE 'SELECT migration_tools.assert(
619 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_status <> '''' AND x_status IS NULL),
620 ''Cannot find a desired copy status'',
621 ''Found all desired copy statuses''
625 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
627 -- convenience functions for handling org maps
629 CREATE OR REPLACE FUNCTION migration_tools.handle_org (TEXT,TEXT) RETURNS VOID AS $$
631 table_schema ALIAS FOR $1;
632 table_name ALIAS FOR $2;
635 EXECUTE 'SELECT EXISTS (
637 FROM information_schema.columns
638 WHERE table_schema = $1
640 and column_name = ''desired_org''
641 )' INTO proceed USING table_schema, table_name;
643 RAISE EXCEPTION 'Missing column desired_org';
646 EXECUTE 'ALTER TABLE '
647 || quote_ident(table_name)
648 || ' DROP COLUMN IF EXISTS x_org';
649 EXECUTE 'ALTER TABLE '
650 || quote_ident(table_name)
651 || ' ADD COLUMN x_org INTEGER';
653 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
654 || ' SET x_org = b.id FROM actor.org_unit b'
655 || ' WHERE BTRIM(a.desired_org) = BTRIM(b.shortname)';
657 EXECUTE 'SELECT migration_tools.assert(
658 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_org <> '''' AND x_org IS NULL),
659 ''Cannot find a desired org unit'',
660 ''Found all desired org units''
664 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
666 -- convenience function for handling desired_not_migrate
668 CREATE OR REPLACE FUNCTION migration_tools.handle_not_migrate (TEXT,TEXT) RETURNS VOID AS $$
670 table_schema ALIAS FOR $1;
671 table_name ALIAS FOR $2;
674 EXECUTE 'SELECT EXISTS (
676 FROM information_schema.columns
677 WHERE table_schema = $1
679 and column_name = ''desired_not_migrate''
680 )' INTO proceed USING table_schema, table_name;
682 RAISE EXCEPTION 'Missing column desired_not_migrate';
685 EXECUTE 'ALTER TABLE '
686 || quote_ident(table_name)
687 || ' DROP COLUMN IF EXISTS x_migrate';
688 EXECUTE 'ALTER TABLE '
689 || quote_ident(table_name)
690 || ' ADD COLUMN x_migrate BOOLEAN';
692 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
693 || ' SET x_migrate = CASE'
694 || ' WHEN BTRIM(desired_not_migrate) = ''TRUE'' THEN FALSE'
695 || ' WHEN BTRIM(desired_not_migrate) = ''DNM'' THEN FALSE'
696 || ' WHEN BTRIM(desired_not_migrate) = ''Do Not Migrate'' THEN FALSE'
697 || ' WHEN BTRIM(desired_not_migrate) = ''FALSE'' THEN TRUE'
698 || ' WHEN BTRIM(desired_not_migrate) = ''Migrate'' THEN TRUE'
699 || ' WHEN BTRIM(desired_not_migrate) = '''' THEN TRUE'
702 EXECUTE 'SELECT migration_tools.assert(
703 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE x_migrate IS NULL),
704 ''Not all desired_not_migrate values understood'',
705 ''All desired_not_migrate values understood''
709 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
711 -- convenience function for handling desired_not_migrate
713 CREATE OR REPLACE FUNCTION migration_tools.handle_barred_or_blocked (TEXT,TEXT) RETURNS VOID AS $$
715 table_schema ALIAS FOR $1;
716 table_name ALIAS FOR $2;
719 EXECUTE 'SELECT EXISTS (
721 FROM information_schema.columns
722 WHERE table_schema = $1
724 and column_name = ''desired_barred_or_blocked''
725 )' INTO proceed USING table_schema, table_name;
727 RAISE EXCEPTION 'Missing column desired_barred_or_blocked';
730 EXECUTE 'ALTER TABLE '
731 || quote_ident(table_name)
732 || ' DROP COLUMN IF EXISTS x_barred';
733 EXECUTE 'ALTER TABLE '
734 || quote_ident(table_name)
735 || ' ADD COLUMN x_barred BOOLEAN';
737 EXECUTE 'ALTER TABLE '
738 || quote_ident(table_name)
739 || ' DROP COLUMN IF EXISTS x_blocked';
740 EXECUTE 'ALTER TABLE '
741 || quote_ident(table_name)
742 || ' ADD COLUMN x_blocked BOOLEAN';
744 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
745 || ' SET x_barred = CASE'
746 || ' WHEN BTRIM(desired_barred_or_blocked) = ''Barred'' THEN TRUE'
747 || ' WHEN BTRIM(desired_barred_or_blocked) = ''Blocked'' THEN FALSE'
748 || ' WHEN BTRIM(desired_barred_or_blocked) = ''Neither'' THEN FALSE'
749 || ' WHEN BTRIM(desired_barred_or_blocked) = '''' THEN FALSE'
752 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
753 || ' SET x_blocked = CASE'
754 || ' WHEN BTRIM(desired_barred_or_blocked) = ''Blocked'' THEN TRUE'
755 || ' WHEN BTRIM(desired_barred_or_blocked) = ''Barred'' THEN FALSE'
756 || ' WHEN BTRIM(desired_barred_or_blocked) = ''Neither'' THEN FALSE'
757 || ' WHEN BTRIM(desired_barred_or_blocked) = '''' THEN FALSE'
760 EXECUTE 'SELECT migration_tools.assert(
761 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE x_barred IS NULL or x_blocked IS NULL),
762 ''Not all desired_barred_or_blocked values understood'',
763 ''All desired_barred_or_blocked values understood''
767 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
769 -- convenience function for handling desired_profile
771 CREATE OR REPLACE FUNCTION migration_tools.handle_profile (TEXT,TEXT) RETURNS VOID AS $$
773 table_schema ALIAS FOR $1;
774 table_name ALIAS FOR $2;
777 EXECUTE 'SELECT EXISTS (
779 FROM information_schema.columns
780 WHERE table_schema = $1
782 and column_name = ''desired_profile''
783 )' INTO proceed USING table_schema, table_name;
785 RAISE EXCEPTION 'Missing column desired_profile';
788 EXECUTE 'ALTER TABLE '
789 || quote_ident(table_name)
790 || ' DROP COLUMN IF EXISTS x_profile';
791 EXECUTE 'ALTER TABLE '
792 || quote_ident(table_name)
793 || ' ADD COLUMN x_profile INTEGER';
795 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
796 || ' SET x_profile = b.id FROM permission.grp_tree b'
797 || ' WHERE BTRIM(UPPER(a.desired_profile)) = BTRIM(UPPER(b.name))';
799 EXECUTE 'SELECT migration_tools.assert(
800 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_profile <> '''' AND x_profile IS NULL),
801 ''Cannot find a desired profile'',
802 ''Found all desired profiles''
806 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
808 -- convenience function for handling desired actor stat cats
810 CREATE OR REPLACE FUNCTION migration_tools.vivicate_actor_sc_and_sce (TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
812 table_schema ALIAS FOR $1;
813 table_name ALIAS FOR $2;
814 field_suffix ALIAS FOR $3; -- for distinguishing between desired_sce1, desired_sce2, etc.
815 org_shortname ALIAS FOR $4;
823 SELECT 'desired_sc' || field_suffix INTO sc;
824 SELECT 'desired_sce' || field_suffix INTO sce;
826 EXECUTE 'SELECT EXISTS (
828 FROM information_schema.columns
829 WHERE table_schema = $1
832 )' INTO proceed USING table_schema, table_name, sc;
834 RAISE EXCEPTION 'Missing column %', sc;
836 EXECUTE 'SELECT EXISTS (
838 FROM information_schema.columns
839 WHERE table_schema = $1
842 )' INTO proceed USING table_schema, table_name, sce;
844 RAISE EXCEPTION 'Missing column %', sce;
847 SELECT id INTO org FROM actor.org_unit WHERE shortname = org_shortname;
849 RAISE EXCEPTION 'Cannot find org by shortname';
851 SELECT INTO org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( org );
853 -- caller responsible for their own truncates though we try to prevent duplicates
854 EXECUTE 'INSERT INTO actor_stat_cat (owner, name)
859 ' || quote_ident(table_name) || '
861 NULLIF(BTRIM('||sc||'),'''') IS NOT NULL
865 WHERE owner = ANY ($2)
866 AND name = BTRIM('||sc||')
871 WHERE owner = ANY ($2)
872 AND name = BTRIM('||sc||')
877 EXECUTE 'INSERT INTO actor_stat_cat_entry (stat_cat, owner, value)
882 WHERE owner = ANY ($2)
883 AND BTRIM('||sc||') = BTRIM(name))
886 WHERE owner = ANY ($2)
887 AND BTRIM('||sc||') = BTRIM(name))
892 ' || quote_ident(table_name) || '
894 NULLIF(BTRIM('||sc||'),'''') IS NOT NULL
895 AND NULLIF(BTRIM('||sce||'),'''') IS NOT NULL
898 FROM actor.stat_cat_entry
902 WHERE owner = ANY ($2)
903 AND BTRIM('||sc||') = BTRIM(name)
904 ) AND value = BTRIM('||sce||')
909 FROM actor_stat_cat_entry
913 WHERE owner = ANY ($2)
914 AND BTRIM('||sc||') = BTRIM(name)
915 ) AND value = BTRIM('||sce||')
921 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
923 CREATE OR REPLACE FUNCTION migration_tools.handle_actor_sc_and_sce (TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
925 table_schema ALIAS FOR $1;
926 table_name ALIAS FOR $2;
927 field_suffix ALIAS FOR $3; -- for distinguishing between desired_sce1, desired_sce2, etc.
928 org_shortname ALIAS FOR $4;
936 SELECT 'desired_sc' || field_suffix INTO sc;
937 SELECT 'desired_sce' || field_suffix INTO sce;
938 EXECUTE 'SELECT EXISTS (
940 FROM information_schema.columns
941 WHERE table_schema = $1
944 )' INTO proceed USING table_schema, table_name, sc;
946 RAISE EXCEPTION 'Missing column %', sc;
948 EXECUTE 'SELECT EXISTS (
950 FROM information_schema.columns
951 WHERE table_schema = $1
954 )' INTO proceed USING table_schema, table_name, sce;
956 RAISE EXCEPTION 'Missing column %', sce;
959 SELECT id INTO org FROM actor.org_unit WHERE shortname = org_shortname;
961 RAISE EXCEPTION 'Cannot find org by shortname';
964 SELECT INTO org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( org );
966 EXECUTE 'ALTER TABLE '
967 || quote_ident(table_name)
968 || ' DROP COLUMN IF EXISTS x_sc' || field_suffix;
969 EXECUTE 'ALTER TABLE '
970 || quote_ident(table_name)
971 || ' ADD COLUMN x_sc' || field_suffix || ' INTEGER';
972 EXECUTE 'ALTER TABLE '
973 || quote_ident(table_name)
974 || ' DROP COLUMN IF EXISTS x_sce' || field_suffix;
975 EXECUTE 'ALTER TABLE '
976 || quote_ident(table_name)
977 || ' ADD COLUMN x_sce' || field_suffix || ' INTEGER';
980 EXECUTE 'UPDATE ' || quote_ident(table_name) || '
982 x_sc' || field_suffix || ' = id
984 (SELECT id, name, owner FROM actor_stat_cat
985 UNION SELECT id, name, owner FROM actor.stat_cat) u
987 BTRIM(UPPER(u.name)) = BTRIM(UPPER(' || sc || '))
988 AND u.owner = ANY ($1);'
991 EXECUTE 'UPDATE ' || quote_ident(table_name) || '
993 x_sce' || field_suffix || ' = id
995 (SELECT id, stat_cat, owner, value FROM actor_stat_cat_entry
996 UNION SELECT id, stat_cat, owner, value FROM actor.stat_cat_entry) u
998 u.stat_cat = x_sc' || field_suffix || '
999 AND BTRIM(UPPER(u.value)) = BTRIM(UPPER(' || sce || '))
1000 AND u.owner = ANY ($1);'
1003 EXECUTE 'SELECT migration_tools.assert(
1004 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_sc' || field_suffix || ' <> '''' AND x_sc' || field_suffix || ' IS NULL),
1005 ''Cannot find a desired stat cat'',
1006 ''Found all desired stat cats''
1009 EXECUTE 'SELECT migration_tools.assert(
1010 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_sce' || field_suffix || ' <> '''' AND x_sce' || field_suffix || ' IS NULL),
1011 ''Cannot find a desired stat cat entry'',
1012 ''Found all desired stat cat entries''
1016 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1018 -- convenience functions for adding shelving locations
1019 DROP FUNCTION IF EXISTS migration_tools.find_shelf(INT,TEXT);
1020 CREATE OR REPLACE FUNCTION migration_tools.find_shelf(org_id INT, shelf_name TEXT) RETURNS INTEGER AS $$
1026 SELECT INTO d MAX(distance) FROM actor.org_unit_ancestors_distance(org_id);
1029 SELECT INTO cur_id id FROM actor.org_unit_ancestor_at_depth(org_id,d);
1030 SELECT INTO return_id id FROM asset.copy_location WHERE owning_lib = cur_id AND name ILIKE shelf_name;
1031 IF return_id IS NOT NULL THEN
1039 $$ LANGUAGE plpgsql;
1041 -- may remove later but testing using this with new migration scripts and not loading acls until go live
1043 DROP FUNCTION IF EXISTS migration_tools.find_mig_shelf(INT,TEXT);
1044 CREATE OR REPLACE FUNCTION migration_tools.find_mig_shelf(org_id INT, shelf_name TEXT) RETURNS INTEGER AS $$
1050 SELECT INTO d MAX(distance) FROM actor.org_unit_ancestors_distance(org_id);
1053 SELECT INTO cur_id id FROM actor.org_unit_ancestor_at_depth(org_id,d);
1055 SELECT INTO return_id id FROM
1056 (SELECT * FROM asset.copy_location UNION ALL SELECT * FROM asset_copy_location) x
1057 WHERE owning_lib = cur_id AND name ILIKE shelf_name;
1058 IF return_id IS NOT NULL THEN
1066 $$ LANGUAGE plpgsql;
1070 -- convenience function for linking to the item staging table
1072 CREATE OR REPLACE FUNCTION migration_tools.handle_item_barcode (TEXT,TEXT,TEXT,TEXT,BOOLEAN) RETURNS VOID AS $$
1074 table_schema ALIAS FOR $1;
1075 table_name ALIAS FOR $2;
1076 foreign_column_name ALIAS FOR $3;
1077 main_column_name ALIAS FOR $4;
1078 btrim_desired ALIAS FOR $5;
1081 EXECUTE 'SELECT EXISTS (
1083 FROM information_schema.columns
1084 WHERE table_schema = $1
1086 and column_name = $3
1087 )' INTO proceed USING table_schema, table_name, foreign_column_name;
1089 RAISE EXCEPTION '%.% missing column %', table_schema, table_name, foreign_column_name;
1092 EXECUTE 'SELECT EXISTS (
1094 FROM information_schema.columns
1095 WHERE table_schema = $1
1096 AND table_name = ''asset_copy_legacy''
1097 and column_name = $2
1098 )' INTO proceed USING table_schema, main_column_name;
1100 RAISE EXCEPTION 'No %.asset_copy_legacy with column %', table_schema, main_column_name;
1103 EXECUTE 'ALTER TABLE '
1104 || quote_ident(table_name)
1105 || ' DROP COLUMN IF EXISTS x_item';
1106 EXECUTE 'ALTER TABLE '
1107 || quote_ident(table_name)
1108 || ' ADD COLUMN x_item BIGINT';
1110 IF btrim_desired THEN
1111 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1112 || ' SET x_item = b.id FROM asset_copy_legacy b'
1113 || ' WHERE BTRIM(a.' || quote_ident(foreign_column_name)
1114 || ') = BTRIM(b.' || quote_ident(main_column_name) || ')';
1116 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1117 || ' SET x_item = b.id FROM asset_copy_legacy b'
1118 || ' WHERE a.' || quote_ident(foreign_column_name)
1119 || ' = b.' || quote_ident(main_column_name);
1122 --EXECUTE 'SELECT migration_tools.assert(
1123 -- NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE ' || quote_ident(foreign_column_name) || ' <> '''' AND x_item IS NULL),
1124 -- ''Cannot link every barcode'',
1125 -- ''Every barcode linked''
1129 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1131 -- convenience function for linking to the user staging table
1133 CREATE OR REPLACE FUNCTION migration_tools.handle_user_barcode (TEXT,TEXT,TEXT,TEXT,BOOLEAN) RETURNS VOID AS $$
1135 table_schema ALIAS FOR $1;
1136 table_name ALIAS FOR $2;
1137 foreign_column_name ALIAS FOR $3;
1138 main_column_name ALIAS FOR $4;
1139 btrim_desired ALIAS FOR $5;
1142 EXECUTE 'SELECT EXISTS (
1144 FROM information_schema.columns
1145 WHERE table_schema = $1
1147 and column_name = $3
1148 )' INTO proceed USING table_schema, table_name, foreign_column_name;
1150 RAISE EXCEPTION '%.% missing column %', table_schema, table_name, foreign_column_name;
1153 EXECUTE 'SELECT EXISTS (
1155 FROM information_schema.columns
1156 WHERE table_schema = $1
1157 AND table_name = ''actor_usr_legacy''
1158 and column_name = $2
1159 )' INTO proceed USING table_schema, main_column_name;
1161 RAISE EXCEPTION 'No %.actor_usr_legacy with column %', table_schema, main_column_name;
1164 EXECUTE 'ALTER TABLE '
1165 || quote_ident(table_name)
1166 || ' DROP COLUMN IF EXISTS x_user';
1167 EXECUTE 'ALTER TABLE '
1168 || quote_ident(table_name)
1169 || ' ADD COLUMN x_user INTEGER';
1171 IF btrim_desired THEN
1172 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1173 || ' SET x_user = b.id FROM actor_usr_legacy b'
1174 || ' WHERE BTRIM(a.' || quote_ident(foreign_column_name)
1175 || ') = BTRIM(b.' || quote_ident(main_column_name) || ')';
1177 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1178 || ' SET x_user = b.id FROM actor_usr_legacy b'
1179 || ' WHERE a.' || quote_ident(foreign_column_name)
1180 || ' = b.' || quote_ident(main_column_name);
1183 --EXECUTE 'SELECT migration_tools.assert(
1184 -- NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE ' || quote_ident(foreign_column_name) || ' <> '''' AND x_user IS NULL),
1185 -- ''Cannot link every barcode'',
1186 -- ''Every barcode linked''
1190 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1192 -- convenience function for linking two tables
1193 -- e.g. select migration_tools.handle_link(:'migschema','asset_copy','barcode','test_foo','l_barcode','x_acp_id',false);
1194 CREATE OR REPLACE FUNCTION migration_tools.handle_link (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,BOOLEAN) RETURNS VOID AS $$
1196 table_schema ALIAS FOR $1;
1197 table_a ALIAS FOR $2;
1198 column_a ALIAS FOR $3;
1199 table_b ALIAS FOR $4;
1200 column_b ALIAS FOR $5;
1201 column_x ALIAS FOR $6;
1202 btrim_desired ALIAS FOR $7;
1205 EXECUTE 'SELECT EXISTS (
1207 FROM information_schema.columns
1208 WHERE table_schema = $1
1210 and column_name = $3
1211 )' INTO proceed USING table_schema, table_a, column_a;
1213 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1216 EXECUTE 'SELECT EXISTS (
1218 FROM information_schema.columns
1219 WHERE table_schema = $1
1221 and column_name = $3
1222 )' INTO proceed USING table_schema, table_b, column_b;
1224 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1227 EXECUTE 'ALTER TABLE '
1228 || quote_ident(table_b)
1229 || ' DROP COLUMN IF EXISTS ' || quote_ident(column_x);
1230 EXECUTE 'ALTER TABLE '
1231 || quote_ident(table_b)
1232 || ' ADD COLUMN ' || quote_ident(column_x) || ' BIGINT';
1234 IF btrim_desired THEN
1235 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1236 || ' SET ' || quote_ident(column_x) || ' = a.id FROM ' || quote_ident(table_a) || ' a'
1237 || ' WHERE BTRIM(a.' || quote_ident(column_a)
1238 || ') = BTRIM(b.' || quote_ident(column_b) || ')';
1240 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1241 || ' SET ' || quote_ident(column_x) || ' = a.id FROM ' || quote_ident(table_a) || ' a'
1242 || ' WHERE a.' || quote_ident(column_a)
1243 || ' = b.' || quote_ident(column_b);
1247 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1249 -- convenience function for linking two tables, but copying column w into column x instead of "id"
1250 -- e.g. select migration_tools.handle_link2(:'migschema','asset_copy','barcode','test_foo','l_barcode','id','x_acp_id',false);
1251 CREATE OR REPLACE FUNCTION migration_tools.handle_link2 (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,BOOLEAN) RETURNS VOID AS $$
1253 table_schema ALIAS FOR $1;
1254 table_a ALIAS FOR $2;
1255 column_a ALIAS FOR $3;
1256 table_b ALIAS FOR $4;
1257 column_b ALIAS FOR $5;
1258 column_w ALIAS FOR $6;
1259 column_x ALIAS FOR $7;
1260 btrim_desired ALIAS FOR $8;
1263 EXECUTE 'SELECT EXISTS (
1265 FROM information_schema.columns
1266 WHERE table_schema = $1
1268 and column_name = $3
1269 )' INTO proceed USING table_schema, table_a, column_a;
1271 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1274 EXECUTE 'SELECT EXISTS (
1276 FROM information_schema.columns
1277 WHERE table_schema = $1
1279 and column_name = $3
1280 )' INTO proceed USING table_schema, table_b, column_b;
1282 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1285 EXECUTE 'ALTER TABLE '
1286 || quote_ident(table_b)
1287 || ' DROP COLUMN IF EXISTS ' || quote_ident(column_x);
1288 EXECUTE 'ALTER TABLE '
1289 || quote_ident(table_b)
1290 || ' ADD COLUMN ' || quote_ident(column_x) || ' TEXT';
1292 IF btrim_desired THEN
1293 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1294 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1295 || ' WHERE BTRIM(a.' || quote_ident(column_a)
1296 || ') = BTRIM(b.' || quote_ident(column_b) || ')';
1298 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1299 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1300 || ' WHERE a.' || quote_ident(column_a)
1301 || ' = b.' || quote_ident(column_b);
1305 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1307 -- convenience function for linking two tables, but copying column w into column x instead of "id". Unlike handle_link2, this one won't drop the target column, and it also doesn't have a final boolean argument for btrim
1308 -- e.g. select migration_tools.handle_link3(:'migschema','asset_copy','barcode','test_foo','l_barcode','id','x_acp_id');
1309 CREATE OR REPLACE FUNCTION migration_tools.handle_link3 (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1311 table_schema ALIAS FOR $1;
1312 table_a ALIAS FOR $2;
1313 column_a ALIAS FOR $3;
1314 table_b ALIAS FOR $4;
1315 column_b ALIAS FOR $5;
1316 column_w ALIAS FOR $6;
1317 column_x ALIAS FOR $7;
1320 EXECUTE 'SELECT EXISTS (
1322 FROM information_schema.columns
1323 WHERE table_schema = $1
1325 and column_name = $3
1326 )' INTO proceed USING table_schema, table_a, column_a;
1328 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1331 EXECUTE 'SELECT EXISTS (
1333 FROM information_schema.columns
1334 WHERE table_schema = $1
1336 and column_name = $3
1337 )' INTO proceed USING table_schema, table_b, column_b;
1339 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1342 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1343 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1344 || ' WHERE a.' || quote_ident(column_a)
1345 || ' = b.' || quote_ident(column_b);
1348 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1350 CREATE OR REPLACE FUNCTION migration_tools.handle_link3_skip_null_or_empty_string (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1352 table_schema ALIAS FOR $1;
1353 table_a ALIAS FOR $2;
1354 column_a ALIAS FOR $3;
1355 table_b ALIAS FOR $4;
1356 column_b ALIAS FOR $5;
1357 column_w ALIAS FOR $6;
1358 column_x ALIAS FOR $7;
1361 EXECUTE 'SELECT EXISTS (
1363 FROM information_schema.columns
1364 WHERE table_schema = $1
1366 and column_name = $3
1367 )' INTO proceed USING table_schema, table_a, column_a;
1369 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1372 EXECUTE 'SELECT EXISTS (
1374 FROM information_schema.columns
1375 WHERE table_schema = $1
1377 and column_name = $3
1378 )' INTO proceed USING table_schema, table_b, column_b;
1380 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1383 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1384 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1385 || ' WHERE a.' || quote_ident(column_a)
1386 || ' = b.' || quote_ident(column_b)
1387 || ' AND NULLIF(a.' || quote_ident(column_w) || ','''') IS NOT NULL';
1390 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1392 CREATE OR REPLACE FUNCTION migration_tools.handle_link3_skip_null (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1394 table_schema ALIAS FOR $1;
1395 table_a ALIAS FOR $2;
1396 column_a ALIAS FOR $3;
1397 table_b ALIAS FOR $4;
1398 column_b ALIAS FOR $5;
1399 column_w ALIAS FOR $6;
1400 column_x ALIAS FOR $7;
1403 EXECUTE 'SELECT EXISTS (
1405 FROM information_schema.columns
1406 WHERE table_schema = $1
1408 and column_name = $3
1409 )' INTO proceed USING table_schema, table_a, column_a;
1411 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1414 EXECUTE 'SELECT EXISTS (
1416 FROM information_schema.columns
1417 WHERE table_schema = $1
1419 and column_name = $3
1420 )' INTO proceed USING table_schema, table_b, column_b;
1422 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1425 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1426 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1427 || ' WHERE a.' || quote_ident(column_a)
1428 || ' = b.' || quote_ident(column_b)
1429 || ' AND a.' || quote_ident(column_w) || ' IS NOT NULL';
1432 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1434 CREATE OR REPLACE FUNCTION migration_tools.handle_link3_skip_true (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1436 table_schema ALIAS FOR $1;
1437 table_a ALIAS FOR $2;
1438 column_a ALIAS FOR $3;
1439 table_b ALIAS FOR $4;
1440 column_b ALIAS FOR $5;
1441 column_w ALIAS FOR $6;
1442 column_x ALIAS FOR $7;
1445 EXECUTE 'SELECT EXISTS (
1447 FROM information_schema.columns
1448 WHERE table_schema = $1
1450 and column_name = $3
1451 )' INTO proceed USING table_schema, table_a, column_a;
1453 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1456 EXECUTE 'SELECT EXISTS (
1458 FROM information_schema.columns
1459 WHERE table_schema = $1
1461 and column_name = $3
1462 )' INTO proceed USING table_schema, table_b, column_b;
1464 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1467 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1468 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1469 || ' WHERE a.' || quote_ident(column_a)
1470 || ' = b.' || quote_ident(column_b)
1471 || ' AND a.' || quote_ident(column_w) || ' IS NOT TRUE';
1474 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1476 CREATE OR REPLACE FUNCTION migration_tools.handle_link3_skip_false (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1478 table_schema ALIAS FOR $1;
1479 table_a ALIAS FOR $2;
1480 column_a ALIAS FOR $3;
1481 table_b ALIAS FOR $4;
1482 column_b ALIAS FOR $5;
1483 column_w ALIAS FOR $6;
1484 column_x ALIAS FOR $7;
1487 EXECUTE 'SELECT EXISTS (
1489 FROM information_schema.columns
1490 WHERE table_schema = $1
1492 and column_name = $3
1493 )' INTO proceed USING table_schema, table_a, column_a;
1495 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1498 EXECUTE 'SELECT EXISTS (
1500 FROM information_schema.columns
1501 WHERE table_schema = $1
1503 and column_name = $3
1504 )' INTO proceed USING table_schema, table_b, column_b;
1506 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1509 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1510 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1511 || ' WHERE a.' || quote_ident(column_a)
1512 || ' = b.' || quote_ident(column_b)
1513 || ' AND a.' || quote_ident(column_w) || ' IS NOT FALSE';
1516 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1518 CREATE OR REPLACE FUNCTION migration_tools.handle_link3_concat_skip_null (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1520 table_schema ALIAS FOR $1;
1521 table_a ALIAS FOR $2;
1522 column_a ALIAS FOR $3;
1523 table_b ALIAS FOR $4;
1524 column_b ALIAS FOR $5;
1525 column_w ALIAS FOR $6;
1526 column_x ALIAS FOR $7;
1529 EXECUTE 'SELECT EXISTS (
1531 FROM information_schema.columns
1532 WHERE table_schema = $1
1534 and column_name = $3
1535 )' INTO proceed USING table_schema, table_a, column_a;
1537 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1540 EXECUTE 'SELECT EXISTS (
1542 FROM information_schema.columns
1543 WHERE table_schema = $1
1545 and column_name = $3
1546 )' INTO proceed USING table_schema, table_b, column_b;
1548 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1551 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1552 || ' SET ' || quote_ident(column_x) || ' = CONCAT_WS('' ; '',b.' || quote_ident(column_x) || ',a.' || quote_ident(column_w) || ') FROM ' || quote_ident(table_a) || ' a'
1553 || ' WHERE a.' || quote_ident(column_a)
1554 || ' = b.' || quote_ident(column_b)
1555 || ' AND NULLIF(a.' || quote_ident(column_w) || ','''') IS NOT NULL';
1558 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1560 -- convenience function for handling desired asset stat cats
1562 CREATE OR REPLACE FUNCTION migration_tools.vivicate_asset_sc_and_sce (TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1564 table_schema ALIAS FOR $1;
1565 table_name ALIAS FOR $2;
1566 field_suffix ALIAS FOR $3; -- for distinguishing between desired_sce1, desired_sce2, etc.
1567 org_shortname ALIAS FOR $4;
1575 SELECT 'desired_sc' || field_suffix INTO sc;
1576 SELECT 'desired_sce' || field_suffix INTO sce;
1578 EXECUTE 'SELECT EXISTS (
1580 FROM information_schema.columns
1581 WHERE table_schema = $1
1583 and column_name = $3
1584 )' INTO proceed USING table_schema, table_name, sc;
1586 RAISE EXCEPTION 'Missing column %', sc;
1588 EXECUTE 'SELECT EXISTS (
1590 FROM information_schema.columns
1591 WHERE table_schema = $1
1593 and column_name = $3
1594 )' INTO proceed USING table_schema, table_name, sce;
1596 RAISE EXCEPTION 'Missing column %', sce;
1599 SELECT id INTO org FROM actor.org_unit WHERE shortname = org_shortname;
1601 RAISE EXCEPTION 'Cannot find org by shortname';
1603 SELECT INTO org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( org );
1605 -- caller responsible for their own truncates though we try to prevent duplicates
1606 EXECUTE 'INSERT INTO asset_stat_cat (owner, name)
1611 ' || quote_ident(table_name) || '
1613 NULLIF(BTRIM('||sc||'),'''') IS NOT NULL
1617 WHERE owner = ANY ($2)
1618 AND name = BTRIM('||sc||')
1623 WHERE owner = ANY ($2)
1624 AND name = BTRIM('||sc||')
1627 USING org, org_list;
1629 EXECUTE 'INSERT INTO asset_stat_cat_entry (stat_cat, owner, value)
1634 WHERE owner = ANY ($2)
1635 AND BTRIM('||sc||') = BTRIM(name))
1638 WHERE owner = ANY ($2)
1639 AND BTRIM('||sc||') = BTRIM(name))
1644 ' || quote_ident(table_name) || '
1646 NULLIF(BTRIM('||sc||'),'''') IS NOT NULL
1647 AND NULLIF(BTRIM('||sce||'),'''') IS NOT NULL
1650 FROM asset.stat_cat_entry
1654 WHERE owner = ANY ($2)
1655 AND BTRIM('||sc||') = BTRIM(name)
1656 ) AND value = BTRIM('||sce||')
1657 AND owner = ANY ($2)
1661 FROM asset_stat_cat_entry
1665 WHERE owner = ANY ($2)
1666 AND BTRIM('||sc||') = BTRIM(name)
1667 ) AND value = BTRIM('||sce||')
1668 AND owner = ANY ($2)
1671 USING org, org_list;
1673 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1675 CREATE OR REPLACE FUNCTION migration_tools.handle_asset_sc_and_sce (TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1677 table_schema ALIAS FOR $1;
1678 table_name ALIAS FOR $2;
1679 field_suffix ALIAS FOR $3; -- for distinguishing between desired_sce1, desired_sce2, etc.
1680 org_shortname ALIAS FOR $4;
1688 SELECT 'desired_sc' || field_suffix INTO sc;
1689 SELECT 'desired_sce' || field_suffix INTO sce;
1690 EXECUTE 'SELECT EXISTS (
1692 FROM information_schema.columns
1693 WHERE table_schema = $1
1695 and column_name = $3
1696 )' INTO proceed USING table_schema, table_name, sc;
1698 RAISE EXCEPTION 'Missing column %', sc;
1700 EXECUTE 'SELECT EXISTS (
1702 FROM information_schema.columns
1703 WHERE table_schema = $1
1705 and column_name = $3
1706 )' INTO proceed USING table_schema, table_name, sce;
1708 RAISE EXCEPTION 'Missing column %', sce;
1711 SELECT id INTO org FROM actor.org_unit WHERE shortname = org_shortname;
1713 RAISE EXCEPTION 'Cannot find org by shortname';
1716 SELECT INTO org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( org );
1718 EXECUTE 'ALTER TABLE '
1719 || quote_ident(table_name)
1720 || ' DROP COLUMN IF EXISTS x_sc' || field_suffix;
1721 EXECUTE 'ALTER TABLE '
1722 || quote_ident(table_name)
1723 || ' ADD COLUMN x_sc' || field_suffix || ' INTEGER';
1724 EXECUTE 'ALTER TABLE '
1725 || quote_ident(table_name)
1726 || ' DROP COLUMN IF EXISTS x_sce' || field_suffix;
1727 EXECUTE 'ALTER TABLE '
1728 || quote_ident(table_name)
1729 || ' ADD COLUMN x_sce' || field_suffix || ' INTEGER';
1732 EXECUTE 'UPDATE ' || quote_ident(table_name) || '
1734 x_sc' || field_suffix || ' = id
1736 (SELECT id, name, owner FROM asset_stat_cat
1737 UNION SELECT id, name, owner FROM asset.stat_cat) u
1739 BTRIM(UPPER(u.name)) = BTRIM(UPPER(' || sc || '))
1740 AND u.owner = ANY ($1);'
1743 EXECUTE 'UPDATE ' || quote_ident(table_name) || '
1745 x_sce' || field_suffix || ' = id
1747 (SELECT id, stat_cat, owner, value FROM asset_stat_cat_entry
1748 UNION SELECT id, stat_cat, owner, value FROM asset.stat_cat_entry) u
1750 u.stat_cat = x_sc' || field_suffix || '
1751 AND BTRIM(UPPER(u.value)) = BTRIM(UPPER(' || sce || '))
1752 AND u.owner = ANY ($1);'
1755 EXECUTE 'SELECT migration_tools.assert(
1756 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_sc' || field_suffix || ' <> '''' AND x_sc' || field_suffix || ' IS NULL),
1757 ''Cannot find a desired stat cat'',
1758 ''Found all desired stat cats''
1761 EXECUTE 'SELECT migration_tools.assert(
1762 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_sce' || field_suffix || ' <> '''' AND x_sce' || field_suffix || ' IS NULL),
1763 ''Cannot find a desired stat cat entry'',
1764 ''Found all desired stat cat entries''
1768 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1770 DROP FUNCTION IF EXISTS migration_tools.btrim_lcolumns(TEXT,TEXT);
1771 CREATE OR REPLACE FUNCTION migration_tools.btrim_lcolumns(s_name TEXT, t_name TEXT) RETURNS BOOLEAN
1778 FOR c_name IN SELECT column_name FROM information_schema.columns WHERE
1780 AND table_schema = s_name
1781 AND (data_type='text' OR data_type='character varying')
1782 AND column_name like 'l_%'
1784 EXECUTE FORMAT('UPDATE ' || s_name || '.' || t_name || ' SET ' || c_name || ' = BTRIM(' || c_name || ')');
1791 DROP FUNCTION IF EXISTS migration_tools.btrim_columns(TEXT,TEXT);
1792 CREATE OR REPLACE FUNCTION migration_tools.btrim_columns(s_name TEXT, t_name TEXT) RETURNS BOOLEAN
1799 FOR c_name IN SELECT column_name FROM information_schema.columns WHERE
1801 AND table_schema = s_name
1802 AND (data_type='text' OR data_type='character varying')
1804 EXECUTE FORMAT('UPDATE ' || s_name || '.' || t_name || ' SET ' || c_name || ' = BTRIM(' || c_name || ')');
1811 DROP FUNCTION IF EXISTS migration_tools.null_empty_lcolumns(TEXT,TEXT);
1812 CREATE OR REPLACE FUNCTION migration_tools.null_empty_lcolumns(s_name TEXT, t_name TEXT) RETURNS BOOLEAN
1819 FOR c_name IN SELECT column_name FROM information_schema.columns WHERE
1821 AND table_schema = s_name
1822 AND (data_type='text' OR data_type='character varying')
1823 AND column_name like 'l_%'
1825 EXECUTE FORMAT('UPDATE ' || s_name || '.' || t_name || ' SET ' || c_name || ' = NULL WHERE ' || c_name || ' = '''' ');
1832 DROP FUNCTION IF EXISTS migration_tools.null_empty_columns(TEXT,TEXT);
1833 CREATE OR REPLACE FUNCTION migration_tools.null_empty_columns(s_name TEXT, t_name TEXT) RETURNS BOOLEAN
1840 FOR c_name IN SELECT column_name FROM information_schema.columns WHERE
1842 AND table_schema = s_name
1843 AND (data_type='text' OR data_type='character varying')
1845 EXECUTE FORMAT('UPDATE ' || s_name || '.' || t_name || ' SET ' || c_name || ' = NULL WHERE ' || c_name || ' = '''' ');
1853 -- convenience function for handling item barcode collisions in asset_copy_legacy
1855 CREATE OR REPLACE FUNCTION migration_tools.handle_asset_barcode_collisions(migration_schema TEXT) RETURNS VOID AS $function$
1860 internal_collision_count NUMERIC := 0;
1861 incumbent_collision_count NUMERIC := 0;
1863 FOR x_barcode IN SELECT barcode FROM asset_copy_legacy WHERE x_migrate GROUP BY 1 HAVING COUNT(*) > 1
1865 FOR x_id IN SELECT id FROM asset_copy WHERE barcode = x_barcode
1867 UPDATE asset_copy SET barcode = migration_schema || '_internal_collision_' || id || '_' || barcode WHERE id = x_id;
1868 GET DIAGNOSTICS row_count = ROW_COUNT;
1869 internal_collision_count := internal_collision_count + row_count;
1872 RAISE INFO '% internal collisions', internal_collision_count;
1873 FOR x_barcode IN SELECT a.barcode FROM asset.copy a, asset_copy_legacy b WHERE x_migrate AND a.deleted IS FALSE AND a.barcode = b.barcode
1875 FOR x_id IN SELECT id FROM asset_copy_legacy WHERE barcode = x_barcode
1877 UPDATE asset_copy_legacy SET barcode = migration_schema || '_incumbent_collision_' || id || '_' || barcode WHERE id = x_id;
1878 GET DIAGNOSTICS row_count = ROW_COUNT;
1879 incumbent_collision_count := incumbent_collision_count + row_count;
1882 RAISE INFO '% incumbent collisions', incumbent_collision_count;
1884 $function$ LANGUAGE plpgsql;
1886 -- convenience function for handling patron barcode/usrname collisions in actor_usr_legacy
1887 -- this should be ran prior to populating actor_card
1889 CREATE OR REPLACE FUNCTION migration_tools.handle_actor_barcode_collisions(migration_schema TEXT) RETURNS VOID AS $function$
1894 internal_collision_count NUMERIC := 0;
1895 incumbent_barcode_collision_count NUMERIC := 0;
1896 incumbent_usrname_collision_count NUMERIC := 0;
1898 FOR x_barcode IN SELECT usrname FROM actor_usr_legacy WHERE x_migrate GROUP BY 1 HAVING COUNT(*) > 1
1900 FOR x_id IN SELECT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1902 UPDATE actor_usr_legacy SET usrname = migration_schema || '_internal_collision_' || id || '_' || usrname WHERE id = x_id;
1903 GET DIAGNOSTICS row_count = ROW_COUNT;
1904 internal_collision_count := internal_collision_count + row_count;
1907 RAISE INFO '% internal usrname/barcode collisions', internal_collision_count;
1910 SELECT a.barcode FROM actor.card a, actor_usr_legacy b WHERE x_migrate AND a.barcode = b.usrname
1912 FOR x_id IN SELECT DISTINCT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1914 UPDATE actor_usr_legacy SET usrname = migration_schema || '_incumbent_barcode_collision_' || id || '_' || usrname WHERE id = x_id;
1915 GET DIAGNOSTICS row_count = ROW_COUNT;
1916 incumbent_barcode_collision_count := incumbent_barcode_collision_count + row_count;
1919 RAISE INFO '% incumbent barcode collisions', incumbent_barcode_collision_count;
1922 SELECT a.usrname FROM actor.usr a, actor_usr_legacy b WHERE x_migrate AND a.usrname = b.usrname
1924 FOR x_id IN SELECT DISTINCT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1926 UPDATE actor_usr_legacy SET usrname = migration_schema || '_incumbent_usrname_collision_' || id || '_' || usrname WHERE id = x_id;
1927 GET DIAGNOSTICS row_count = ROW_COUNT;
1928 incumbent_usrname_collision_count := incumbent_usrname_collision_count + row_count;
1931 RAISE INFO '% incumbent usrname collisions (post barcode collision munging)', incumbent_usrname_collision_count;
1933 $function$ LANGUAGE plpgsql;
1935 -- alternate version: convenience function for handling item barcode collisions in asset_copy_legacy
1937 CREATE OR REPLACE FUNCTION migration_tools.handle_asset_barcode_collisions2(migration_schema TEXT) RETURNS VOID AS $function$
1942 internal_collision_count NUMERIC := 0;
1943 incumbent_collision_count NUMERIC := 0;
1945 FOR x_barcode IN SELECT barcode FROM asset_copy_legacy WHERE x_migrate GROUP BY 1 HAVING COUNT(*) > 1
1947 FOR x_id IN SELECT id FROM asset_copy WHERE barcode = x_barcode
1949 UPDATE asset_copy SET barcode = migration_schema || '_internal_collision_' || id || '_' || barcode WHERE id = x_id;
1950 GET DIAGNOSTICS row_count = ROW_COUNT;
1951 internal_collision_count := internal_collision_count + row_count;
1954 RAISE INFO '% internal collisions', internal_collision_count;
1955 FOR x_barcode IN SELECT a.barcode FROM asset.copy a, asset_copy_legacy b WHERE x_migrate AND a.deleted IS FALSE AND a.barcode = b.barcode
1957 FOR x_id IN SELECT id FROM asset_copy_legacy WHERE barcode = x_barcode
1959 UPDATE asset_copy_legacy SET barcode = migration_schema || '_' || barcode WHERE id = x_id;
1960 GET DIAGNOSTICS row_count = ROW_COUNT;
1961 incumbent_collision_count := incumbent_collision_count + row_count;
1964 RAISE INFO '% incumbent collisions', incumbent_collision_count;
1966 $function$ LANGUAGE plpgsql;
1968 -- alternate version: convenience function for handling patron barcode/usrname collisions in actor_usr_legacy
1969 -- this should be ran prior to populating actor_card
1971 CREATE OR REPLACE FUNCTION migration_tools.handle_actor_barcode_collisions2(migration_schema TEXT) RETURNS VOID AS $function$
1976 internal_collision_count NUMERIC := 0;
1977 incumbent_barcode_collision_count NUMERIC := 0;
1978 incumbent_usrname_collision_count NUMERIC := 0;
1980 FOR x_barcode IN SELECT usrname FROM actor_usr_legacy WHERE x_migrate GROUP BY 1 HAVING COUNT(*) > 1
1982 FOR x_id IN SELECT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1984 UPDATE actor_usr_legacy SET usrname = migration_schema || '_internal_collision_' || id || '_' || usrname WHERE id = x_id;
1985 GET DIAGNOSTICS row_count = ROW_COUNT;
1986 internal_collision_count := internal_collision_count + row_count;
1989 RAISE INFO '% internal usrname/barcode collisions', internal_collision_count;
1992 SELECT a.barcode FROM actor.card a, actor_usr_legacy b WHERE x_migrate AND a.barcode = b.usrname
1994 FOR x_id IN SELECT DISTINCT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1996 UPDATE actor_usr_legacy SET usrname = migration_schema || '_' || usrname WHERE id = x_id;
1997 GET DIAGNOSTICS row_count = ROW_COUNT;
1998 incumbent_barcode_collision_count := incumbent_barcode_collision_count + row_count;
2001 RAISE INFO '% incumbent barcode collisions', incumbent_barcode_collision_count;
2004 SELECT a.usrname FROM actor.usr a, actor_usr_legacy b WHERE x_migrate AND a.usrname = b.usrname
2006 FOR x_id IN SELECT DISTINCT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
2008 UPDATE actor_usr_legacy SET usrname = migration_schema || '_' || usrname WHERE id = x_id;
2009 GET DIAGNOSTICS row_count = ROW_COUNT;
2010 incumbent_usrname_collision_count := incumbent_usrname_collision_count + row_count;
2013 RAISE INFO '% incumbent usrname collisions (post barcode collision munging)', incumbent_usrname_collision_count;
2015 $function$ LANGUAGE plpgsql;
2017 CREATE OR REPLACE FUNCTION migration_tools.is_circ_rule_safe_to_delete( test_matchpoint INTEGER ) RETURNS BOOLEAN AS $func$
2018 -- WARNING: Use at your own risk
2019 -- FIXME: not considering marc_type, marc_form, marc_bib_level, marc_vr_format, usr_age_lower_bound, usr_age_upper_bound, item_age
2021 item_object asset.copy%ROWTYPE;
2022 user_object actor.usr%ROWTYPE;
2023 test_rule_object config.circ_matrix_matchpoint%ROWTYPE;
2024 result_rule_object config.circ_matrix_matchpoint%ROWTYPE;
2025 safe_to_delete BOOLEAN := FALSE;
2026 m action.found_circ_matrix_matchpoint;
2027 n action.found_circ_matrix_matchpoint;
2028 -- ( success BOOL, matchpoint config.circ_matrix_matchpoint, buildrows INT[] )
2029 result_matchpoint INTEGER;
2031 SELECT INTO test_rule_object * FROM config.circ_matrix_matchpoint WHERE id = test_matchpoint;
2032 RAISE INFO 'testing rule: %', test_rule_object;
2034 INSERT INTO actor.usr (
2044 COALESCE(test_rule_object.grp, 2),
2045 'is_circ_rule_safe_to_delete_' || test_matchpoint || '_' || NOW()::text,
2050 COALESCE(test_rule_object.user_home_ou, test_rule_object.org_unit),
2051 COALESCE(test_rule_object.juvenile_flag, FALSE)
2054 SELECT INTO user_object * FROM actor.usr WHERE id = currval('actor.usr_id_seq');
2056 INSERT INTO asset.call_number (
2067 COALESCE(test_rule_object.copy_owning_lib,test_rule_object.org_unit),
2068 'is_circ_rule_safe_to_delete_' || test_matchpoint || '_' || NOW()::text,
2072 INSERT INTO asset.copy (
2084 'is_circ_rule_safe_to_delete_' || test_matchpoint || '_' || NOW()::text,
2085 COALESCE(test_rule_object.copy_circ_lib,test_rule_object.org_unit),
2087 currval('asset.call_number_id_seq'),
2089 COALESCE(test_rule_object.copy_location,1),
2092 COALESCE(test_rule_object.ref_flag,FALSE),
2093 test_rule_object.circ_modifier
2096 SELECT INTO item_object * FROM asset.copy WHERE id = currval('asset.copy_id_seq');
2098 SELECT INTO m * FROM action.find_circ_matrix_matchpoint(
2099 test_rule_object.org_unit,
2102 COALESCE(test_rule_object.is_renewal,FALSE)
2104 RAISE INFO ' action.find_circ_matrix_matchpoint(%,%,%,%) = (%,%,%)',
2105 test_rule_object.org_unit,
2108 COALESCE(test_rule_object.is_renewal,FALSE),
2114 -- disable the rule being tested to see if the outcome changes
2115 UPDATE config.circ_matrix_matchpoint SET active = FALSE WHERE id = (m.matchpoint).id;
2117 SELECT INTO n * FROM action.find_circ_matrix_matchpoint(
2118 test_rule_object.org_unit,
2121 COALESCE(test_rule_object.is_renewal,FALSE)
2123 RAISE INFO 'VS action.find_circ_matrix_matchpoint(%,%,%,%) = (%,%,%)',
2124 test_rule_object.org_unit,
2127 COALESCE(test_rule_object.is_renewal,FALSE),
2133 -- FIXME: We could dig deeper and see if the referenced config.rule_*
2134 -- entries are effectively equivalent, but for now, let's assume no
2135 -- duplicate rules at that level
2137 (m.matchpoint).circulate = (n.matchpoint).circulate
2138 AND (m.matchpoint).duration_rule = (n.matchpoint).duration_rule
2139 AND (m.matchpoint).recurring_fine_rule = (n.matchpoint).recurring_fine_rule
2140 AND (m.matchpoint).max_fine_rule = (n.matchpoint).max_fine_rule
2142 (m.matchpoint).hard_due_date = (n.matchpoint).hard_due_date
2144 (m.matchpoint).hard_due_date IS NULL
2145 AND (n.matchpoint).hard_due_date IS NULL
2149 (m.matchpoint).renewals = (n.matchpoint).renewals
2151 (m.matchpoint).renewals IS NULL
2152 AND (n.matchpoint).renewals IS NULL
2156 (m.matchpoint).grace_period = (n.matchpoint).grace_period
2158 (m.matchpoint).grace_period IS NULL
2159 AND (n.matchpoint).grace_period IS NULL
2163 (m.matchpoint).total_copy_hold_ratio = (n.matchpoint).total_copy_hold_ratio
2165 (m.matchpoint).total_copy_hold_ratio IS NULL
2166 AND (n.matchpoint).total_copy_hold_ratio IS NULL
2170 (m.matchpoint).available_copy_hold_ratio = (n.matchpoint).available_copy_hold_ratio
2172 (m.matchpoint).available_copy_hold_ratio IS NULL
2173 AND (n.matchpoint).available_copy_hold_ratio IS NULL
2177 SELECT limit_set, fallthrough
2178 FROM config.circ_matrix_limit_set_map
2179 WHERE active and matchpoint = (m.matchpoint).id
2181 SELECT limit_set, fallthrough
2182 FROM config.circ_matrix_limit_set_map
2183 WHERE active and matchpoint = (n.matchpoint).id
2187 RAISE INFO 'rule has same outcome';
2188 safe_to_delete := TRUE;
2190 RAISE INFO 'rule has different outcome';
2191 safe_to_delete := FALSE;
2194 RAISE EXCEPTION 'rollback the temporary changes';
2196 EXCEPTION WHEN OTHERS THEN
2198 RAISE INFO 'inside exception block: %, %', SQLSTATE, SQLERRM;
2199 RETURN safe_to_delete;
2202 $func$ LANGUAGE plpgsql;