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;
1068 -- convenience function for linking to the item staging table
1070 CREATE OR REPLACE FUNCTION migration_tools.handle_item_barcode (TEXT,TEXT,TEXT,TEXT,BOOLEAN) RETURNS VOID AS $$
1072 table_schema ALIAS FOR $1;
1073 table_name ALIAS FOR $2;
1074 foreign_column_name ALIAS FOR $3;
1075 main_column_name ALIAS FOR $4;
1076 btrim_desired ALIAS FOR $5;
1079 EXECUTE 'SELECT EXISTS (
1081 FROM information_schema.columns
1082 WHERE table_schema = $1
1084 and column_name = $3
1085 )' INTO proceed USING table_schema, table_name, foreign_column_name;
1087 RAISE EXCEPTION '%.% missing column %', table_schema, table_name, foreign_column_name;
1090 EXECUTE 'SELECT EXISTS (
1092 FROM information_schema.columns
1093 WHERE table_schema = $1
1094 AND table_name = ''asset_copy_legacy''
1095 and column_name = $2
1096 )' INTO proceed USING table_schema, main_column_name;
1098 RAISE EXCEPTION 'No %.asset_copy_legacy with column %', table_schema, main_column_name;
1101 EXECUTE 'ALTER TABLE '
1102 || quote_ident(table_name)
1103 || ' DROP COLUMN IF EXISTS x_item';
1104 EXECUTE 'ALTER TABLE '
1105 || quote_ident(table_name)
1106 || ' ADD COLUMN x_item BIGINT';
1108 IF btrim_desired THEN
1109 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1110 || ' SET x_item = b.id FROM asset_copy_legacy b'
1111 || ' WHERE BTRIM(a.' || quote_ident(foreign_column_name)
1112 || ') = BTRIM(b.' || quote_ident(main_column_name) || ')';
1114 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1115 || ' SET x_item = b.id FROM asset_copy_legacy b'
1116 || ' WHERE a.' || quote_ident(foreign_column_name)
1117 || ' = b.' || quote_ident(main_column_name);
1120 --EXECUTE 'SELECT migration_tools.assert(
1121 -- NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE ' || quote_ident(foreign_column_name) || ' <> '''' AND x_item IS NULL),
1122 -- ''Cannot link every barcode'',
1123 -- ''Every barcode linked''
1127 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1129 -- convenience function for linking to the user staging table
1131 CREATE OR REPLACE FUNCTION migration_tools.handle_user_barcode (TEXT,TEXT,TEXT,TEXT,BOOLEAN) RETURNS VOID AS $$
1133 table_schema ALIAS FOR $1;
1134 table_name ALIAS FOR $2;
1135 foreign_column_name ALIAS FOR $3;
1136 main_column_name ALIAS FOR $4;
1137 btrim_desired ALIAS FOR $5;
1140 EXECUTE 'SELECT EXISTS (
1142 FROM information_schema.columns
1143 WHERE table_schema = $1
1145 and column_name = $3
1146 )' INTO proceed USING table_schema, table_name, foreign_column_name;
1148 RAISE EXCEPTION '%.% missing column %', table_schema, table_name, foreign_column_name;
1151 EXECUTE 'SELECT EXISTS (
1153 FROM information_schema.columns
1154 WHERE table_schema = $1
1155 AND table_name = ''actor_usr_legacy''
1156 and column_name = $2
1157 )' INTO proceed USING table_schema, main_column_name;
1159 RAISE EXCEPTION 'No %.actor_usr_legacy with column %', table_schema, main_column_name;
1162 EXECUTE 'ALTER TABLE '
1163 || quote_ident(table_name)
1164 || ' DROP COLUMN IF EXISTS x_user';
1165 EXECUTE 'ALTER TABLE '
1166 || quote_ident(table_name)
1167 || ' ADD COLUMN x_user INTEGER';
1169 IF btrim_desired THEN
1170 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1171 || ' SET x_user = b.id FROM actor_usr_legacy b'
1172 || ' WHERE BTRIM(a.' || quote_ident(foreign_column_name)
1173 || ') = BTRIM(b.' || quote_ident(main_column_name) || ')';
1175 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1176 || ' SET x_user = b.id FROM actor_usr_legacy b'
1177 || ' WHERE a.' || quote_ident(foreign_column_name)
1178 || ' = b.' || quote_ident(main_column_name);
1181 --EXECUTE 'SELECT migration_tools.assert(
1182 -- NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE ' || quote_ident(foreign_column_name) || ' <> '''' AND x_user IS NULL),
1183 -- ''Cannot link every barcode'',
1184 -- ''Every barcode linked''
1188 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1190 -- convenience function for linking two tables
1191 -- e.g. select migration_tools.handle_link(:'migschema','asset_copy','barcode','test_foo','l_barcode','x_acp_id',false);
1192 CREATE OR REPLACE FUNCTION migration_tools.handle_link (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,BOOLEAN) RETURNS VOID AS $$
1194 table_schema ALIAS FOR $1;
1195 table_a ALIAS FOR $2;
1196 column_a ALIAS FOR $3;
1197 table_b ALIAS FOR $4;
1198 column_b ALIAS FOR $5;
1199 column_x ALIAS FOR $6;
1200 btrim_desired ALIAS FOR $7;
1203 EXECUTE 'SELECT EXISTS (
1205 FROM information_schema.columns
1206 WHERE table_schema = $1
1208 and column_name = $3
1209 )' INTO proceed USING table_schema, table_a, column_a;
1211 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1214 EXECUTE 'SELECT EXISTS (
1216 FROM information_schema.columns
1217 WHERE table_schema = $1
1219 and column_name = $3
1220 )' INTO proceed USING table_schema, table_b, column_b;
1222 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1225 EXECUTE 'ALTER TABLE '
1226 || quote_ident(table_b)
1227 || ' DROP COLUMN IF EXISTS ' || quote_ident(column_x);
1228 EXECUTE 'ALTER TABLE '
1229 || quote_ident(table_b)
1230 || ' ADD COLUMN ' || quote_ident(column_x) || ' BIGINT';
1232 IF btrim_desired THEN
1233 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1234 || ' SET ' || quote_ident(column_x) || ' = a.id FROM ' || quote_ident(table_a) || ' a'
1235 || ' WHERE BTRIM(a.' || quote_ident(column_a)
1236 || ') = BTRIM(b.' || quote_ident(column_b) || ')';
1238 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1239 || ' SET ' || quote_ident(column_x) || ' = a.id FROM ' || quote_ident(table_a) || ' a'
1240 || ' WHERE a.' || quote_ident(column_a)
1241 || ' = b.' || quote_ident(column_b);
1245 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1247 -- convenience function for linking two tables, but copying column w into column x instead of "id"
1248 -- e.g. select migration_tools.handle_link2(:'migschema','asset_copy','barcode','test_foo','l_barcode','id','x_acp_id',false);
1249 CREATE OR REPLACE FUNCTION migration_tools.handle_link2 (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,BOOLEAN) RETURNS VOID AS $$
1251 table_schema ALIAS FOR $1;
1252 table_a ALIAS FOR $2;
1253 column_a ALIAS FOR $3;
1254 table_b ALIAS FOR $4;
1255 column_b ALIAS FOR $5;
1256 column_w ALIAS FOR $6;
1257 column_x ALIAS FOR $7;
1258 btrim_desired ALIAS FOR $8;
1261 EXECUTE 'SELECT EXISTS (
1263 FROM information_schema.columns
1264 WHERE table_schema = $1
1266 and column_name = $3
1267 )' INTO proceed USING table_schema, table_a, column_a;
1269 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1272 EXECUTE 'SELECT EXISTS (
1274 FROM information_schema.columns
1275 WHERE table_schema = $1
1277 and column_name = $3
1278 )' INTO proceed USING table_schema, table_b, column_b;
1280 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1283 EXECUTE 'ALTER TABLE '
1284 || quote_ident(table_b)
1285 || ' DROP COLUMN IF EXISTS ' || quote_ident(column_x);
1286 EXECUTE 'ALTER TABLE '
1287 || quote_ident(table_b)
1288 || ' ADD COLUMN ' || quote_ident(column_x) || ' TEXT';
1290 IF btrim_desired THEN
1291 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1292 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1293 || ' WHERE BTRIM(a.' || quote_ident(column_a)
1294 || ') = BTRIM(b.' || quote_ident(column_b) || ')';
1296 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1297 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1298 || ' WHERE a.' || quote_ident(column_a)
1299 || ' = b.' || quote_ident(column_b);
1303 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1305 -- 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
1306 -- e.g. select migration_tools.handle_link3(:'migschema','asset_copy','barcode','test_foo','l_barcode','id','x_acp_id');
1307 CREATE OR REPLACE FUNCTION migration_tools.handle_link3 (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1309 table_schema ALIAS FOR $1;
1310 table_a ALIAS FOR $2;
1311 column_a ALIAS FOR $3;
1312 table_b ALIAS FOR $4;
1313 column_b ALIAS FOR $5;
1314 column_w ALIAS FOR $6;
1315 column_x ALIAS FOR $7;
1318 EXECUTE 'SELECT EXISTS (
1320 FROM information_schema.columns
1321 WHERE table_schema = $1
1323 and column_name = $3
1324 )' INTO proceed USING table_schema, table_a, column_a;
1326 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1329 EXECUTE 'SELECT EXISTS (
1331 FROM information_schema.columns
1332 WHERE table_schema = $1
1334 and column_name = $3
1335 )' INTO proceed USING table_schema, table_b, column_b;
1337 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1340 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1341 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1342 || ' WHERE a.' || quote_ident(column_a)
1343 || ' = b.' || quote_ident(column_b);
1346 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1348 CREATE OR REPLACE FUNCTION migration_tools.handle_link3_skip_null_or_empty_string (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1350 table_schema ALIAS FOR $1;
1351 table_a ALIAS FOR $2;
1352 column_a ALIAS FOR $3;
1353 table_b ALIAS FOR $4;
1354 column_b ALIAS FOR $5;
1355 column_w ALIAS FOR $6;
1356 column_x ALIAS FOR $7;
1359 EXECUTE 'SELECT EXISTS (
1361 FROM information_schema.columns
1362 WHERE table_schema = $1
1364 and column_name = $3
1365 )' INTO proceed USING table_schema, table_a, column_a;
1367 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1370 EXECUTE 'SELECT EXISTS (
1372 FROM information_schema.columns
1373 WHERE table_schema = $1
1375 and column_name = $3
1376 )' INTO proceed USING table_schema, table_b, column_b;
1378 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1381 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1382 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1383 || ' WHERE a.' || quote_ident(column_a)
1384 || ' = b.' || quote_ident(column_b)
1385 || ' AND NULLIF(a.' || quote_ident(column_w) || ','''') IS NOT NULL';
1388 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1390 CREATE OR REPLACE FUNCTION migration_tools.handle_link3_skip_null (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1392 table_schema ALIAS FOR $1;
1393 table_a ALIAS FOR $2;
1394 column_a ALIAS FOR $3;
1395 table_b ALIAS FOR $4;
1396 column_b ALIAS FOR $5;
1397 column_w ALIAS FOR $6;
1398 column_x ALIAS FOR $7;
1401 EXECUTE 'SELECT EXISTS (
1403 FROM information_schema.columns
1404 WHERE table_schema = $1
1406 and column_name = $3
1407 )' INTO proceed USING table_schema, table_a, column_a;
1409 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1412 EXECUTE 'SELECT EXISTS (
1414 FROM information_schema.columns
1415 WHERE table_schema = $1
1417 and column_name = $3
1418 )' INTO proceed USING table_schema, table_b, column_b;
1420 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1423 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1424 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1425 || ' WHERE a.' || quote_ident(column_a)
1426 || ' = b.' || quote_ident(column_b)
1427 || ' AND a.' || quote_ident(column_w) || ' IS NOT NULL';
1430 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1432 CREATE OR REPLACE FUNCTION migration_tools.handle_link3_skip_true (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1434 table_schema ALIAS FOR $1;
1435 table_a ALIAS FOR $2;
1436 column_a ALIAS FOR $3;
1437 table_b ALIAS FOR $4;
1438 column_b ALIAS FOR $5;
1439 column_w ALIAS FOR $6;
1440 column_x ALIAS FOR $7;
1443 EXECUTE 'SELECT EXISTS (
1445 FROM information_schema.columns
1446 WHERE table_schema = $1
1448 and column_name = $3
1449 )' INTO proceed USING table_schema, table_a, column_a;
1451 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1454 EXECUTE 'SELECT EXISTS (
1456 FROM information_schema.columns
1457 WHERE table_schema = $1
1459 and column_name = $3
1460 )' INTO proceed USING table_schema, table_b, column_b;
1462 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1465 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1466 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1467 || ' WHERE a.' || quote_ident(column_a)
1468 || ' = b.' || quote_ident(column_b)
1469 || ' AND a.' || quote_ident(column_w) || ' IS NOT TRUE';
1472 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1474 CREATE OR REPLACE FUNCTION migration_tools.handle_link3_skip_false (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1476 table_schema ALIAS FOR $1;
1477 table_a ALIAS FOR $2;
1478 column_a ALIAS FOR $3;
1479 table_b ALIAS FOR $4;
1480 column_b ALIAS FOR $5;
1481 column_w ALIAS FOR $6;
1482 column_x ALIAS FOR $7;
1485 EXECUTE 'SELECT EXISTS (
1487 FROM information_schema.columns
1488 WHERE table_schema = $1
1490 and column_name = $3
1491 )' INTO proceed USING table_schema, table_a, column_a;
1493 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1496 EXECUTE 'SELECT EXISTS (
1498 FROM information_schema.columns
1499 WHERE table_schema = $1
1501 and column_name = $3
1502 )' INTO proceed USING table_schema, table_b, column_b;
1504 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1507 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1508 || ' SET ' || quote_ident(column_x) || ' = a.' || quote_ident(column_w) || ' FROM ' || quote_ident(table_a) || ' a'
1509 || ' WHERE a.' || quote_ident(column_a)
1510 || ' = b.' || quote_ident(column_b)
1511 || ' AND a.' || quote_ident(column_w) || ' IS NOT FALSE';
1514 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1516 CREATE OR REPLACE FUNCTION migration_tools.handle_link3_concat_skip_null (TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1518 table_schema ALIAS FOR $1;
1519 table_a ALIAS FOR $2;
1520 column_a ALIAS FOR $3;
1521 table_b ALIAS FOR $4;
1522 column_b ALIAS FOR $5;
1523 column_w ALIAS FOR $6;
1524 column_x ALIAS FOR $7;
1527 EXECUTE 'SELECT EXISTS (
1529 FROM information_schema.columns
1530 WHERE table_schema = $1
1532 and column_name = $3
1533 )' INTO proceed USING table_schema, table_a, column_a;
1535 RAISE EXCEPTION '%.% missing column %', table_schema, table_a, column_a;
1538 EXECUTE 'SELECT EXISTS (
1540 FROM information_schema.columns
1541 WHERE table_schema = $1
1543 and column_name = $3
1544 )' INTO proceed USING table_schema, table_b, column_b;
1546 RAISE EXCEPTION '%.% missing column %', table_schema, table_b, column_b;
1549 EXECUTE 'UPDATE ' || quote_ident(table_b) || ' b'
1550 || ' SET ' || quote_ident(column_x) || ' = CONCAT_WS('' ; '',b.' || quote_ident(column_x) || ',a.' || quote_ident(column_w) || ') FROM ' || quote_ident(table_a) || ' a'
1551 || ' WHERE a.' || quote_ident(column_a)
1552 || ' = b.' || quote_ident(column_b)
1553 || ' AND NULLIF(a.' || quote_ident(column_w) || ','''') IS NOT NULL';
1556 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1558 -- convenience function for handling desired asset stat cats
1560 CREATE OR REPLACE FUNCTION migration_tools.vivicate_asset_sc_and_sce (TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1562 table_schema ALIAS FOR $1;
1563 table_name ALIAS FOR $2;
1564 field_suffix ALIAS FOR $3; -- for distinguishing between desired_sce1, desired_sce2, etc.
1565 org_shortname ALIAS FOR $4;
1573 SELECT 'desired_sc' || field_suffix INTO sc;
1574 SELECT 'desired_sce' || field_suffix INTO sce;
1576 EXECUTE 'SELECT EXISTS (
1578 FROM information_schema.columns
1579 WHERE table_schema = $1
1581 and column_name = $3
1582 )' INTO proceed USING table_schema, table_name, sc;
1584 RAISE EXCEPTION 'Missing column %', sc;
1586 EXECUTE 'SELECT EXISTS (
1588 FROM information_schema.columns
1589 WHERE table_schema = $1
1591 and column_name = $3
1592 )' INTO proceed USING table_schema, table_name, sce;
1594 RAISE EXCEPTION 'Missing column %', sce;
1597 SELECT id INTO org FROM actor.org_unit WHERE shortname = org_shortname;
1599 RAISE EXCEPTION 'Cannot find org by shortname';
1601 SELECT INTO org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( org );
1603 -- caller responsible for their own truncates though we try to prevent duplicates
1604 EXECUTE 'INSERT INTO asset_stat_cat (owner, name)
1609 ' || quote_ident(table_name) || '
1611 NULLIF(BTRIM('||sc||'),'''') IS NOT NULL
1615 WHERE owner = ANY ($2)
1616 AND name = BTRIM('||sc||')
1621 WHERE owner = ANY ($2)
1622 AND name = BTRIM('||sc||')
1625 USING org, org_list;
1627 EXECUTE 'INSERT INTO asset_stat_cat_entry (stat_cat, owner, value)
1632 WHERE owner = ANY ($2)
1633 AND BTRIM('||sc||') = BTRIM(name))
1636 WHERE owner = ANY ($2)
1637 AND BTRIM('||sc||') = BTRIM(name))
1642 ' || quote_ident(table_name) || '
1644 NULLIF(BTRIM('||sc||'),'''') IS NOT NULL
1645 AND NULLIF(BTRIM('||sce||'),'''') IS NOT NULL
1648 FROM asset.stat_cat_entry
1652 WHERE owner = ANY ($2)
1653 AND BTRIM('||sc||') = BTRIM(name)
1654 ) AND value = BTRIM('||sce||')
1655 AND owner = ANY ($2)
1659 FROM asset_stat_cat_entry
1663 WHERE owner = ANY ($2)
1664 AND BTRIM('||sc||') = BTRIM(name)
1665 ) AND value = BTRIM('||sce||')
1666 AND owner = ANY ($2)
1669 USING org, org_list;
1671 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1673 CREATE OR REPLACE FUNCTION migration_tools.handle_asset_sc_and_sce (TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1675 table_schema ALIAS FOR $1;
1676 table_name ALIAS FOR $2;
1677 field_suffix ALIAS FOR $3; -- for distinguishing between desired_sce1, desired_sce2, etc.
1678 org_shortname ALIAS FOR $4;
1686 SELECT 'desired_sc' || field_suffix INTO sc;
1687 SELECT 'desired_sce' || field_suffix INTO sce;
1688 EXECUTE 'SELECT EXISTS (
1690 FROM information_schema.columns
1691 WHERE table_schema = $1
1693 and column_name = $3
1694 )' INTO proceed USING table_schema, table_name, sc;
1696 RAISE EXCEPTION 'Missing column %', sc;
1698 EXECUTE 'SELECT EXISTS (
1700 FROM information_schema.columns
1701 WHERE table_schema = $1
1703 and column_name = $3
1704 )' INTO proceed USING table_schema, table_name, sce;
1706 RAISE EXCEPTION 'Missing column %', sce;
1709 SELECT id INTO org FROM actor.org_unit WHERE shortname = org_shortname;
1711 RAISE EXCEPTION 'Cannot find org by shortname';
1714 SELECT INTO org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( org );
1716 EXECUTE 'ALTER TABLE '
1717 || quote_ident(table_name)
1718 || ' DROP COLUMN IF EXISTS x_sc' || field_suffix;
1719 EXECUTE 'ALTER TABLE '
1720 || quote_ident(table_name)
1721 || ' ADD COLUMN x_sc' || field_suffix || ' INTEGER';
1722 EXECUTE 'ALTER TABLE '
1723 || quote_ident(table_name)
1724 || ' DROP COLUMN IF EXISTS x_sce' || field_suffix;
1725 EXECUTE 'ALTER TABLE '
1726 || quote_ident(table_name)
1727 || ' ADD COLUMN x_sce' || field_suffix || ' INTEGER';
1730 EXECUTE 'UPDATE ' || quote_ident(table_name) || '
1732 x_sc' || field_suffix || ' = id
1734 (SELECT id, name, owner FROM asset_stat_cat
1735 UNION SELECT id, name, owner FROM asset.stat_cat) u
1737 BTRIM(UPPER(u.name)) = BTRIM(UPPER(' || sc || '))
1738 AND u.owner = ANY ($1);'
1741 EXECUTE 'UPDATE ' || quote_ident(table_name) || '
1743 x_sce' || field_suffix || ' = id
1745 (SELECT id, stat_cat, owner, value FROM asset_stat_cat_entry
1746 UNION SELECT id, stat_cat, owner, value FROM asset.stat_cat_entry) u
1748 u.stat_cat = x_sc' || field_suffix || '
1749 AND BTRIM(UPPER(u.value)) = BTRIM(UPPER(' || sce || '))
1750 AND u.owner = ANY ($1);'
1753 EXECUTE 'SELECT migration_tools.assert(
1754 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_sc' || field_suffix || ' <> '''' AND x_sc' || field_suffix || ' IS NULL),
1755 ''Cannot find a desired stat cat'',
1756 ''Found all desired stat cats''
1759 EXECUTE 'SELECT migration_tools.assert(
1760 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_sce' || field_suffix || ' <> '''' AND x_sce' || field_suffix || ' IS NULL),
1761 ''Cannot find a desired stat cat entry'',
1762 ''Found all desired stat cat entries''
1766 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1768 DROP FUNCTION IF EXISTS migration_tools.btrim_lcolumns(TEXT,TEXT);
1769 CREATE OR REPLACE FUNCTION migration_tools.btrim_lcolumns(s_name TEXT, t_name TEXT) RETURNS BOOLEAN
1776 FOR c_name IN SELECT column_name FROM information_schema.columns WHERE
1778 AND table_schema = s_name
1779 AND (data_type='text' OR data_type='character varying')
1780 AND column_name like 'l_%'
1782 EXECUTE FORMAT('UPDATE ' || s_name || '.' || t_name || ' SET ' || c_name || ' = BTRIM(' || c_name || ')');
1789 DROP FUNCTION IF EXISTS migration_tools.btrim_columns(TEXT,TEXT);
1790 CREATE OR REPLACE FUNCTION migration_tools.btrim_columns(s_name TEXT, t_name TEXT) RETURNS BOOLEAN
1797 FOR c_name IN SELECT column_name FROM information_schema.columns WHERE
1799 AND table_schema = s_name
1800 AND (data_type='text' OR data_type='character varying')
1802 EXECUTE FORMAT('UPDATE ' || s_name || '.' || t_name || ' SET ' || c_name || ' = BTRIM(' || c_name || ')');
1809 DROP FUNCTION IF EXISTS migration_tools.null_empty_lcolumns(TEXT,TEXT);
1810 CREATE OR REPLACE FUNCTION migration_tools.null_empty_lcolumns(s_name TEXT, t_name TEXT) RETURNS BOOLEAN
1817 FOR c_name IN SELECT column_name FROM information_schema.columns WHERE
1819 AND table_schema = s_name
1820 AND (data_type='text' OR data_type='character varying')
1821 AND column_name like 'l_%'
1823 EXECUTE FORMAT('UPDATE ' || s_name || '.' || t_name || ' SET ' || c_name || ' = NULL WHERE ' || c_name || ' = '''' ');
1830 DROP FUNCTION IF EXISTS migration_tools.null_empty_columns(TEXT,TEXT);
1831 CREATE OR REPLACE FUNCTION migration_tools.null_empty_columns(s_name TEXT, t_name TEXT) RETURNS BOOLEAN
1838 FOR c_name IN SELECT column_name FROM information_schema.columns WHERE
1840 AND table_schema = s_name
1841 AND (data_type='text' OR data_type='character varying')
1843 EXECUTE FORMAT('UPDATE ' || s_name || '.' || t_name || ' SET ' || c_name || ' = NULL WHERE ' || c_name || ' = '''' ');
1851 -- convenience function for handling item barcode collisions in asset_copy_legacy
1853 CREATE OR REPLACE FUNCTION migration_tools.handle_asset_barcode_collisions(migration_schema TEXT) RETURNS VOID AS $function$
1858 internal_collision_count NUMERIC := 0;
1859 incumbent_collision_count NUMERIC := 0;
1861 FOR x_barcode IN SELECT barcode FROM asset_copy_legacy WHERE x_migrate GROUP BY 1 HAVING COUNT(*) > 1
1863 FOR x_id IN SELECT id FROM asset_copy WHERE barcode = x_barcode
1865 UPDATE asset_copy SET barcode = migration_schema || '_internal_collision_' || id || '_' || barcode WHERE id = x_id;
1866 GET DIAGNOSTICS row_count = ROW_COUNT;
1867 internal_collision_count := internal_collision_count + row_count;
1870 RAISE INFO '% internal collisions', internal_collision_count;
1871 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
1873 FOR x_id IN SELECT id FROM asset_copy_legacy WHERE barcode = x_barcode
1875 UPDATE asset_copy_legacy SET barcode = migration_schema || '_incumbent_collision_' || id || '_' || barcode WHERE id = x_id;
1876 GET DIAGNOSTICS row_count = ROW_COUNT;
1877 incumbent_collision_count := incumbent_collision_count + row_count;
1880 RAISE INFO '% incumbent collisions', incumbent_collision_count;
1882 $function$ LANGUAGE plpgsql;
1884 -- convenience function for handling patron barcode/usrname collisions in actor_usr_legacy
1885 -- this should be ran prior to populating actor_card
1887 CREATE OR REPLACE FUNCTION migration_tools.handle_actor_barcode_collisions(migration_schema TEXT) RETURNS VOID AS $function$
1892 internal_collision_count NUMERIC := 0;
1893 incumbent_barcode_collision_count NUMERIC := 0;
1894 incumbent_usrname_collision_count NUMERIC := 0;
1896 FOR x_barcode IN SELECT usrname FROM actor_usr_legacy WHERE x_migrate GROUP BY 1 HAVING COUNT(*) > 1
1898 FOR x_id IN SELECT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1900 UPDATE actor_usr_legacy SET usrname = migration_schema || '_internal_collision_' || id || '_' || usrname WHERE id = x_id;
1901 GET DIAGNOSTICS row_count = ROW_COUNT;
1902 internal_collision_count := internal_collision_count + row_count;
1905 RAISE INFO '% internal usrname/barcode collisions', internal_collision_count;
1908 SELECT a.barcode FROM actor.card a, actor_usr_legacy b WHERE x_migrate AND a.barcode = b.usrname
1910 FOR x_id IN SELECT DISTINCT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1912 UPDATE actor_usr_legacy SET usrname = migration_schema || '_incumbent_barcode_collision_' || id || '_' || usrname WHERE id = x_id;
1913 GET DIAGNOSTICS row_count = ROW_COUNT;
1914 incumbent_barcode_collision_count := incumbent_barcode_collision_count + row_count;
1917 RAISE INFO '% incumbent barcode collisions', incumbent_barcode_collision_count;
1920 SELECT a.usrname FROM actor.usr a, actor_usr_legacy b WHERE x_migrate AND a.usrname = b.usrname
1922 FOR x_id IN SELECT DISTINCT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1924 UPDATE actor_usr_legacy SET usrname = migration_schema || '_incumbent_usrname_collision_' || id || '_' || usrname WHERE id = x_id;
1925 GET DIAGNOSTICS row_count = ROW_COUNT;
1926 incumbent_usrname_collision_count := incumbent_usrname_collision_count + row_count;
1929 RAISE INFO '% incumbent usrname collisions (post barcode collision munging)', incumbent_usrname_collision_count;
1931 $function$ LANGUAGE plpgsql;
1933 -- alternate version: convenience function for handling item barcode collisions in asset_copy_legacy
1935 CREATE OR REPLACE FUNCTION migration_tools.handle_asset_barcode_collisions2(migration_schema TEXT) RETURNS VOID AS $function$
1940 internal_collision_count NUMERIC := 0;
1941 incumbent_collision_count NUMERIC := 0;
1943 FOR x_barcode IN SELECT barcode FROM asset_copy_legacy WHERE x_migrate GROUP BY 1 HAVING COUNT(*) > 1
1945 FOR x_id IN SELECT id FROM asset_copy WHERE barcode = x_barcode
1947 UPDATE asset_copy SET barcode = migration_schema || '_internal_collision_' || id || '_' || barcode WHERE id = x_id;
1948 GET DIAGNOSTICS row_count = ROW_COUNT;
1949 internal_collision_count := internal_collision_count + row_count;
1952 RAISE INFO '% internal collisions', internal_collision_count;
1953 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
1955 FOR x_id IN SELECT id FROM asset_copy_legacy WHERE barcode = x_barcode
1957 UPDATE asset_copy_legacy SET barcode = migration_schema || '_' || barcode WHERE id = x_id;
1958 GET DIAGNOSTICS row_count = ROW_COUNT;
1959 incumbent_collision_count := incumbent_collision_count + row_count;
1962 RAISE INFO '% incumbent collisions', incumbent_collision_count;
1964 $function$ LANGUAGE plpgsql;
1966 -- alternate version: convenience function for handling patron barcode/usrname collisions in actor_usr_legacy
1967 -- this should be ran prior to populating actor_card
1969 CREATE OR REPLACE FUNCTION migration_tools.handle_actor_barcode_collisions2(migration_schema TEXT) RETURNS VOID AS $function$
1974 internal_collision_count NUMERIC := 0;
1975 incumbent_barcode_collision_count NUMERIC := 0;
1976 incumbent_usrname_collision_count NUMERIC := 0;
1978 FOR x_barcode IN SELECT usrname FROM actor_usr_legacy WHERE x_migrate GROUP BY 1 HAVING COUNT(*) > 1
1980 FOR x_id IN SELECT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1982 UPDATE actor_usr_legacy SET usrname = migration_schema || '_internal_collision_' || id || '_' || usrname WHERE id = x_id;
1983 GET DIAGNOSTICS row_count = ROW_COUNT;
1984 internal_collision_count := internal_collision_count + row_count;
1987 RAISE INFO '% internal usrname/barcode collisions', internal_collision_count;
1990 SELECT a.barcode FROM actor.card a, actor_usr_legacy b WHERE x_migrate AND a.barcode = b.usrname
1992 FOR x_id IN SELECT DISTINCT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1994 UPDATE actor_usr_legacy SET usrname = migration_schema || '_' || usrname WHERE id = x_id;
1995 GET DIAGNOSTICS row_count = ROW_COUNT;
1996 incumbent_barcode_collision_count := incumbent_barcode_collision_count + row_count;
1999 RAISE INFO '% incumbent barcode collisions', incumbent_barcode_collision_count;
2002 SELECT a.usrname FROM actor.usr a, actor_usr_legacy b WHERE x_migrate AND a.usrname = b.usrname
2004 FOR x_id IN SELECT DISTINCT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
2006 UPDATE actor_usr_legacy SET usrname = migration_schema || '_' || usrname WHERE id = x_id;
2007 GET DIAGNOSTICS row_count = ROW_COUNT;
2008 incumbent_usrname_collision_count := incumbent_usrname_collision_count + row_count;
2011 RAISE INFO '% incumbent usrname collisions (post barcode collision munging)', incumbent_usrname_collision_count;
2013 $function$ LANGUAGE plpgsql;
2015 CREATE OR REPLACE FUNCTION migration_tools.is_circ_rule_safe_to_delete( test_matchpoint INTEGER ) RETURNS BOOLEAN AS $func$
2016 -- WARNING: Use at your own risk
2017 -- FIXME: not considering marc_type, marc_form, marc_bib_level, marc_vr_format, usr_age_lower_bound, usr_age_upper_bound, item_age
2019 item_object asset.copy%ROWTYPE;
2020 user_object actor.usr%ROWTYPE;
2021 test_rule_object config.circ_matrix_matchpoint%ROWTYPE;
2022 result_rule_object config.circ_matrix_matchpoint%ROWTYPE;
2023 safe_to_delete BOOLEAN := FALSE;
2024 m action.found_circ_matrix_matchpoint;
2025 n action.found_circ_matrix_matchpoint;
2026 -- ( success BOOL, matchpoint config.circ_matrix_matchpoint, buildrows INT[] )
2027 result_matchpoint INTEGER;
2029 SELECT INTO test_rule_object * FROM config.circ_matrix_matchpoint WHERE id = test_matchpoint;
2030 RAISE INFO 'testing rule: %', test_rule_object;
2032 INSERT INTO actor.usr (
2042 COALESCE(test_rule_object.grp, 2),
2043 'is_circ_rule_safe_to_delete_' || test_matchpoint || '_' || NOW()::text,
2048 COALESCE(test_rule_object.user_home_ou, test_rule_object.org_unit),
2049 COALESCE(test_rule_object.juvenile_flag, FALSE)
2052 SELECT INTO user_object * FROM actor.usr WHERE id = currval('actor.usr_id_seq');
2054 INSERT INTO asset.call_number (
2065 COALESCE(test_rule_object.copy_owning_lib,test_rule_object.org_unit),
2066 'is_circ_rule_safe_to_delete_' || test_matchpoint || '_' || NOW()::text,
2070 INSERT INTO asset.copy (
2082 'is_circ_rule_safe_to_delete_' || test_matchpoint || '_' || NOW()::text,
2083 COALESCE(test_rule_object.copy_circ_lib,test_rule_object.org_unit),
2085 currval('asset.call_number_id_seq'),
2087 COALESCE(test_rule_object.copy_location,1),
2090 COALESCE(test_rule_object.ref_flag,FALSE),
2091 test_rule_object.circ_modifier
2094 SELECT INTO item_object * FROM asset.copy WHERE id = currval('asset.copy_id_seq');
2096 SELECT INTO m * FROM action.find_circ_matrix_matchpoint(
2097 test_rule_object.org_unit,
2100 COALESCE(test_rule_object.is_renewal,FALSE)
2102 RAISE INFO ' action.find_circ_matrix_matchpoint(%,%,%,%) = (%,%,%)',
2103 test_rule_object.org_unit,
2106 COALESCE(test_rule_object.is_renewal,FALSE),
2112 -- disable the rule being tested to see if the outcome changes
2113 UPDATE config.circ_matrix_matchpoint SET active = FALSE WHERE id = (m.matchpoint).id;
2115 SELECT INTO n * FROM action.find_circ_matrix_matchpoint(
2116 test_rule_object.org_unit,
2119 COALESCE(test_rule_object.is_renewal,FALSE)
2121 RAISE INFO 'VS action.find_circ_matrix_matchpoint(%,%,%,%) = (%,%,%)',
2122 test_rule_object.org_unit,
2125 COALESCE(test_rule_object.is_renewal,FALSE),
2131 -- FIXME: We could dig deeper and see if the referenced config.rule_*
2132 -- entries are effectively equivalent, but for now, let's assume no
2133 -- duplicate rules at that level
2135 (m.matchpoint).circulate = (n.matchpoint).circulate
2136 AND (m.matchpoint).duration_rule = (n.matchpoint).duration_rule
2137 AND (m.matchpoint).recurring_fine_rule = (n.matchpoint).recurring_fine_rule
2138 AND (m.matchpoint).max_fine_rule = (n.matchpoint).max_fine_rule
2140 (m.matchpoint).hard_due_date = (n.matchpoint).hard_due_date
2142 (m.matchpoint).hard_due_date IS NULL
2143 AND (n.matchpoint).hard_due_date IS NULL
2147 (m.matchpoint).renewals = (n.matchpoint).renewals
2149 (m.matchpoint).renewals IS NULL
2150 AND (n.matchpoint).renewals IS NULL
2154 (m.matchpoint).grace_period = (n.matchpoint).grace_period
2156 (m.matchpoint).grace_period IS NULL
2157 AND (n.matchpoint).grace_period IS NULL
2161 (m.matchpoint).total_copy_hold_ratio = (n.matchpoint).total_copy_hold_ratio
2163 (m.matchpoint).total_copy_hold_ratio IS NULL
2164 AND (n.matchpoint).total_copy_hold_ratio IS NULL
2168 (m.matchpoint).available_copy_hold_ratio = (n.matchpoint).available_copy_hold_ratio
2170 (m.matchpoint).available_copy_hold_ratio IS NULL
2171 AND (n.matchpoint).available_copy_hold_ratio IS NULL
2175 SELECT limit_set, fallthrough
2176 FROM config.circ_matrix_limit_set_map
2177 WHERE active and matchpoint = (m.matchpoint).id
2179 SELECT limit_set, fallthrough
2180 FROM config.circ_matrix_limit_set_map
2181 WHERE active and matchpoint = (n.matchpoint).id
2185 RAISE INFO 'rule has same outcome';
2186 safe_to_delete := TRUE;
2188 RAISE INFO 'rule has different outcome';
2189 safe_to_delete := FALSE;
2192 RAISE EXCEPTION 'rollback the temporary changes';
2194 EXCEPTION WHEN OTHERS THEN
2196 RAISE INFO 'inside exception block: %, %', SQLSTATE, SQLERRM;
2197 RETURN safe_to_delete;
2200 $func$ LANGUAGE plpgsql;