add missing update script for change to circ age stored procedure
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0533.schema.fix_age_circ.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0533'); -- gmc
4
5 CREATE OR REPLACE FUNCTION action.age_circ_on_delete () RETURNS TRIGGER AS $$
6 DECLARE
7 found char := 'N';
8 BEGIN
9
10     -- If there are any renewals for this circulation, don't archive or delete
11     -- it yet.   We'll do so later, when we archive and delete the renewals.
12
13     SELECT 'Y' INTO found
14     FROM action.circulation
15     WHERE parent_circ = OLD.id
16     LIMIT 1;
17
18     IF found = 'Y' THEN
19         RETURN NULL;  -- don't delete
20         END IF;
21
22     -- Archive a copy of the old row to action.aged_circulation
23
24     INSERT INTO action.aged_circulation
25         (id,usr_post_code, usr_home_ou, usr_profile, usr_birth_year, copy_call_number, copy_location,
26         copy_owning_lib, copy_circ_lib, copy_bib_record, xact_start, xact_finish, target_copy,
27         circ_lib, circ_staff, checkin_staff, checkin_lib, renewal_remaining, grace_period, due_date,
28         stop_fines_time, checkin_time, create_time, duration, fine_interval, recurring_fine,
29         max_fine, phone_renewal, desk_renewal, opac_renewal, duration_rule, recurring_fine_rule,
30         max_fine_rule, stop_fines, workstation, checkin_workstation, checkin_scan_time, parent_circ)
31       SELECT
32         id,usr_post_code, usr_home_ou, usr_profile, usr_birth_year, copy_call_number, copy_location,
33         copy_owning_lib, copy_circ_lib, copy_bib_record, xact_start, xact_finish, target_copy,
34         circ_lib, circ_staff, checkin_staff, checkin_lib, renewal_remaining, grace_period, due_date,
35         stop_fines_time, checkin_time, create_time, duration, fine_interval, recurring_fine,
36         max_fine, phone_renewal, desk_renewal, opac_renewal, duration_rule, recurring_fine_rule,
37         max_fine_rule, stop_fines, workstation, checkin_workstation, checkin_scan_time, parent_circ
38         FROM action.all_circulation WHERE id = OLD.id;
39
40     RETURN OLD;
41 END;
42 $$ LANGUAGE 'plpgsql';
43
44 COMMIT;