Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE OR REPLACE FUNCTION csharp.migrate_call_numbers(source_ou INT, dest_ou INT) RETURNS INT AS $func$
- DECLARE
- source_org actor.org_unit%ROWTYPE;
- dest_org actor.org_unit%ROWTYPE;
- source_cn asset.call_number%ROWTYPE;
- target_cn asset.call_number%ROWTYPE;
- hold action.hold_request%ROWTYPE;
- BEGIN
- SELECT INTO source_org *
- FROM actor.org_unit
- WHERE id = source_ou;
- SELECT INTO dest_org *
- FROM actor.org_unit
- WHERE id = dest_ou;
- -- Find call numbers attached to the source org ...
- FOR source_cn IN
- SELECT *
- FROM asset.call_number
- WHERE owning_lib = source_org.id
- AND NOT deleted
- LOOP
- SELECT INTO target_cn *
- FROM asset.call_number
- WHERE label = source_cn.label
- AND prefix = source_cn.prefix
- AND suffix = source_cn.suffix
- AND owning_lib = dest_org.id
- AND record = source_cn.record
- AND NOT deleted;
- -- ... and if there's a conflicting one on the target ...
- IF FOUND THEN
- -- ... move the copies to that, and ...
- UPDATE asset.copy
- SET call_number = target_cn.id
- WHERE call_number = source_cn.id;
- -- ... move V holds to the move-target call number
- FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
- UPDATE action.hold_request
- SET target = target_cn.id
- WHERE id = hold.id;
- END LOOP;
- UPDATE asset.call_number SET deleted = TRUE WHERE id = source_cn.id;
- -- ... if not ...
- ELSE
- -- ... just move the call number to the target org
- UPDATE asset.call_number
- SET owning_lib = dest_org.id
- WHERE id = source_cn.id;
- END IF;
- END LOOP;
- RETURN 1;
- END;
- $func$ LANGUAGE plpgsql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement