add vacuum analyze script for database maintenance in remove_ou scripts
[migration-tools.git] / remove_ou_data / 05_remove_billings.sql
1 -- Copyright 2015, Equinox Software, Inc.
2 -- Author: Galen Charlton <gmc@esilibrary.com>
3 --
4 -- This program is free software; you can redistribute it and/or
5 -- modify it under the terms of the GNU General Public License
6 -- as published by the Free Software Foundation; either version 2
7 -- of the License, or (at your option) any later version.
8 --
9 -- This program is distributed in the hope that it will be useful,
10 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
11 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 -- GNU General Public License for more details.
13 --
14 -- You should have received a copy of the GNU General Public License
15 -- along with this program; if not, write to the Free Software
16 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18 \set ou_to_del ''''EXAMPLE''''
19 \set ECHO all
20 \timing
21
22 ALTER TABLE money.cash_payment DISABLE TRIGGER mat_summary_del_tgr;
23 ALTER TABLE money.goods_payment DISABLE TRIGGER mat_summary_del_tgr;
24 ALTER TABLE money.credit_payment DISABLE TRIGGER mat_summary_del_tgr;
25 ALTER TABLE money.check_payment DISABLE TRIGGER mat_summary_del_tgr;
26 ALTER TABLE money.credit_card_payment DISABLE TRIGGER mat_summary_del_tgr;
27 ALTER TABLE money.forgive_payment DISABLE TRIGGER mat_summary_del_tgr;
28 ALTER TABLE money.payment DISABLE TRIGGER mat_summary_del_tgr;
29 ALTER TABLE money.billing DISABLE TRIGGER mat_summary_del_tgr;
30 ALTER TABLE money.grocery DISABLE TRIGGER mat_summary_remove_tgr;
31
32 BEGIN;
33
34 DELETE FROM money.credit_payment
35 WHERE xact IN (
36 SELECT usr FROM money.billable_xact WHERE usr IN 
37     (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del))
38 );
39
40 DELETE FROM money.credit_card_payment
41 WHERE xact IN (
42 SELECT usr FROM money.billable_xact WHERE usr IN 
43     (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del))
44 );
45
46 DELETE FROM money.check_payment
47 WHERE xact IN (
48 SELECT usr FROM money.billable_xact WHERE usr IN 
49     (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del))
50 );
51
52 DELETE FROM money.cash_payment
53 WHERE xact IN (
54 SELECT usr FROM money.billable_xact WHERE usr IN 
55     (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del))
56 );
57
58 DELETE FROM money.goods_payment
59 WHERE xact IN (
60 SELECT usr FROM money.billable_xact WHERE usr IN 
61     (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del))
62 );
63
64 DELETE FROM money.work_payment
65 WHERE xact IN (
66 SELECT usr FROM money.billable_xact WHERE usr IN 
67     (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del))
68 );
69
70 DELETE FROM money.forgive_payment
71 WHERE xact IN (
72 SELECT usr FROM money.billable_xact WHERE usr IN 
73     (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del))
74 );
75
76 DELETE FROM money.bnm_desk_payment
77 WHERE xact IN (
78 SELECT usr FROM money.billable_xact WHERE usr IN 
79     (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del))
80 );
81
82 DELETE FROM money.payment
83 WHERE xact IN (
84 SELECT usr FROM money.billable_xact WHERE usr IN 
85     (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del))
86 );
87
88 DELETE FROM money.billing
89 WHERE xact IN (
90 SELECT usr FROM money.billable_xact WHERE usr IN 
91     (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del))
92 );
93
94 DELETE FROM money.grocery WHERE usr IN
95 (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del));
96
97 DELETE FROM money.billable_xact WHERE usr IN
98 (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del));
99
100 DELETE FROM money.materialized_billable_xact_summary WHERE usr IN
101 (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del));
102 COMMIT;
103
104 ALTER TABLE money.cash_payment ENABLE TRIGGER mat_summary_del_tgr;
105 ALTER TABLE money.goods_payment ENABLE TRIGGER mat_summary_del_tgr;
106 ALTER TABLE money.credit_payment ENABLE TRIGGER mat_summary_del_tgr;
107 ALTER TABLE money.check_payment ENABLE TRIGGER mat_summary_del_tgr;
108 ALTER TABLE money.credit_card_payment ENABLE TRIGGER mat_summary_del_tgr;
109 ALTER TABLE money.forgive_payment ENABLE TRIGGER mat_summary_del_tgr;
110 ALTER TABLE money.payment ENABLE TRIGGER mat_summary_del_tgr;
111 ALTER TABLE money.billing ENABLE TRIGGER mat_summary_del_tgr;
112 ALTER TABLE money.grocery ENABLE TRIGGER mat_summary_remove_tgr;