]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0896.correct_copy_location.sql
LP#1744385: Additions and edits to release note entry
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0896.correct_copy_location.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('0896', :eg_version);
4
5 CREATE OR REPLACE FUNCTION asset.acp_location_fixer()
6 RETURNS TRIGGER AS $$
7 DECLARE
8     new_copy_location INT;
9 BEGIN
10     IF (TG_OP = 'UPDATE') THEN
11         IF NEW.location = OLD.location AND NEW.call_number = OLD.call_number AND NEW.circ_lib = OLD.circ_lib THEN
12             RETURN NEW;
13         END IF;
14     END IF;
15     SELECT INTO new_copy_location acpl.id FROM asset.copy_location acpl JOIN actor.org_unit_ancestors_distance((SELECT owning_lib FROM asset.call_number WHERE id = NEW.call_number)) aouad ON acpl.owning_lib = aouad.id WHERE name = (SELECT name FROM asset.copy_location WHERE id = NEW.location) ORDER BY distance LIMIT 1;
16     IF new_copy_location IS NULL THEN
17         SELECT INTO new_copy_location acpl.id FROM asset.copy_location acpl JOIN actor.org_unit_ancestors_distance(NEW.circ_lib) aouad ON acpl.owning_lib = aouad.id WHERE name = (SELECT name FROM asset.copy_location WHERE id = NEW.location) ORDER BY distance LIMIT 1;
18     END IF;
19     IF new_copy_location IS NOT NULL THEN
20         NEW.location = new_copy_location;
21     END IF;
22     RETURN NEW;
23 END;
24 $$ LANGUAGE plpgsql;
25
26 DROP TRIGGER IF EXISTS acp_location_fixer_trig ON asset.copy;
27
28 CREATE TRIGGER acp_location_fixer_trig
29     BEFORE INSERT OR UPDATE OF location, call_number, circ_lib ON asset.copy
30     FOR EACH ROW EXECUTE PROCEDURE asset.acp_location_fixer();
31
32 COMMIT;