4 --SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
6 \qecho Migrating aged billing and payment data. This might take a while.
8 CREATE TABLE money.aged_payment (LIKE money.payment INCLUDING INDEXES);
9 ALTER TABLE money.aged_payment ADD COLUMN payment_type TEXT NOT NULL;
10 CREATE TABLE money.aged_billing (LIKE money.billing INCLUDING INDEXES);
12 INSERT INTO money.aged_payment
13 SELECT mp.* FROM money.payment_view mp
14 JOIN action.aged_circulation circ ON (circ.id = mp.xact);
16 INSERT INTO money.aged_billing
17 SELECT mb.* FROM money.billing mb
18 JOIN action.aged_circulation circ ON (circ.id = mb.xact);
20 DELETE FROM money.payment WHERE id IN (
21 SELECT mp.id FROM money.payment mp
22 JOIN action.aged_circulation circ ON (circ.id = mp.xact)
25 DELETE FROM money.billing WHERE id IN (
26 SELECT mb.id FROM money.billing mb
27 JOIN action.aged_circulation circ ON (circ.id = mb.xact)
31 CREATE OR REPLACE FUNCTION action.age_circ_on_delete () RETURNS TRIGGER AS $$
36 -- If there are any renewals for this circulation, don't archive or delete
37 -- it yet. We'll do so later, when we archive and delete the renewals.
40 FROM action.circulation
41 WHERE parent_circ = OLD.id
45 RETURN NULL; -- don't delete
48 -- Archive a copy of the old row to action.aged_circulation
50 INSERT INTO action.aged_circulation
51 (id,usr_post_code, usr_home_ou, usr_profile, usr_birth_year, copy_call_number, copy_location,
52 copy_owning_lib, copy_circ_lib, copy_bib_record, xact_start, xact_finish, target_copy,
53 circ_lib, circ_staff, checkin_staff, checkin_lib, renewal_remaining, grace_period, due_date,
54 stop_fines_time, checkin_time, create_time, duration, fine_interval, recurring_fine,
55 max_fine, phone_renewal, desk_renewal, opac_renewal, duration_rule, recurring_fine_rule,
56 max_fine_rule, stop_fines, workstation, checkin_workstation, checkin_scan_time, parent_circ)
58 id,usr_post_code, usr_home_ou, usr_profile, usr_birth_year, copy_call_number, copy_location,
59 copy_owning_lib, copy_circ_lib, copy_bib_record, xact_start, xact_finish, target_copy,
60 circ_lib, circ_staff, checkin_staff, checkin_lib, renewal_remaining, grace_period, due_date,
61 stop_fines_time, checkin_time, create_time, duration, fine_interval, recurring_fine,
62 max_fine, phone_renewal, desk_renewal, opac_renewal, duration_rule, recurring_fine_rule,
63 max_fine_rule, stop_fines, workstation, checkin_workstation, checkin_scan_time, parent_circ
64 FROM action.all_circulation WHERE id = OLD.id;
66 -- Migrate billings and payments to aged tables
68 INSERT INTO money.aged_billing
69 SELECT * FROM money.billing WHERE xact = OLD.id;
71 INSERT INTO money.aged_payment
72 SELECT * FROM money.payment_view WHERE xact = OLD.id;
74 DELETE FROM money.billing WHERE xact = OLD.id;
75 DELETE FROM money.payment WHERE xact = OLD.id;
79 $$ LANGUAGE 'plpgsql';