From: Jason Etheridge Date: Mon, 26 Feb 2018 04:10:28 +0000 (-0500) Subject: migration_tools.handle_circmod X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=commitdiff_plain;h=6c7e457c6ccb5eead8e300e2ed45bddf0ce513fb migration_tools.handle_circmod for example: select migration_tools.handle_circmod(:'migschema','map_item_l_location_clean'); Signed-off-by: Jason Etheridge --- diff --git a/sql/base/base.sql b/sql/base/base.sql index d52b6fe..5dcc7a1 100644 --- a/sql/base/base.sql +++ b/sql/base/base.sql @@ -2770,3 +2770,52 @@ CREATE OR REPLACE FUNCTION migration_tools.handle_shelf (TEXT,TEXT,TEXT,INTEGER) END; $$ LANGUAGE PLPGSQL STRICT VOLATILE; +-- convenience functions for handling circmod maps + +CREATE OR REPLACE FUNCTION migration_tools.handle_circmod (TEXT,TEXT) RETURNS VOID AS $$ + DECLARE + table_schema ALIAS FOR $1; + table_name ALIAS FOR $2; + proceed BOOLEAN; + BEGIN + EXECUTE 'SELECT EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_schema = $1 + AND table_name = $2 + and column_name = ''desired_circmod'' + )' INTO proceed USING table_schema, table_name; + IF NOT proceed THEN + RAISE EXCEPTION 'Missing column desired_circmod'; + END IF; + + EXECUTE 'ALTER TABLE ' + || quote_ident(table_name) + || ' DROP COLUMN IF EXISTS x_circmod'; + EXECUTE 'ALTER TABLE ' + || quote_ident(table_name) + || ' ADD COLUMN x_circmod TEXT'; + + EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a' + || ' SET x_circmod = code FROM config.circ_modifier b' + || ' WHERE BTRIM(UPPER(a.desired_circmod)) = BTRIM(UPPER(b.code))'; + + EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a' + || ' SET x_circmod = code FROM config.circ_modifier b' + || ' WHERE BTRIM(UPPER(a.desired_circmod)) = BTRIM(UPPER(b.name))' + || ' AND x_circmod IS NULL'; + + EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a' + || ' SET x_circmod = code FROM config.circ_modifier b' + || ' WHERE BTRIM(UPPER(a.desired_circmod)) = BTRIM(UPPER(b.description))' + || ' AND x_circmod IS NULL'; + + EXECUTE 'SELECT migration_tools.assert( + NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_circmod <> '''' AND x_circmod IS NULL), + ''Cannot find a desired circulation modifier'', + ''Found all desired circulation modifiers'' + );'; + + END; +$$ LANGUAGE PLPGSQL STRICT VOLATILE; +