add event output removal
[migration-tools.git] / remove_ou_data / 15_remove_ou_extras.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 create index tmp_idx on money.billing(btype);
23 ALTER TABLE action.circulation DISABLE TRIGGER action_circulation_target_copy_trig;
24 ALTER TABLE asset.copy_location DISABLE RULE protect_copy_location_delete;
25
26 BEGIN;
27
28 DELETE FROM actor.org_address WHERE org_unit IN
29 (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del);
30
31 UPDATE actor.org_unit SET ill_address = NULL, holds_address = NULL, mailing_address = NULL, billing_address = NULL
32 WHERE id IN
33 (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del);
34
35 UPDATE action.circulation SET copy_location = 1
36 WHERE copy_location IN (SELECT id FROM asset.copy_location WHERE owning_lib IN 
37 (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del));
38 DELETE FROM asset.copy_location WHERE owning_lib IN
39 (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del);
40
41 DELETE FROM money.account_adjustment WHERE billing IN 
42     (SELECT id FROM money.billing WHERE btype IN (
43         SELECT id FROM config.billing_type
44         WHERE owner IN
45         (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
46 ));
47
48 DELETE FROM money.billing
49 WHERE btype IN (
50     SELECT id FROM config.billing_type
51     WHERE owner IN
52     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
53 );
54
55 DELETE FROM action_trigger.event_output WHERE id IN (
56 SELECT template_output FROM action_trigger.event 
57 WHERE  event_def IN (
58     SELECT id FROM action_trigger.event_definition
59     WHERE owner IN
60     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
61 ));
62
63 DELETE FROM action_trigger.event_output WHERE id IN (
64 SELECT async_output FROM action_trigger.event 
65 WHERE  event_def IN (
66     SELECT id FROM action_trigger.event_definition
67     WHERE owner IN
68     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
69 ));
70
71 DELETE FROM action_trigger.event_output WHERE id IN (
72 SELECT error_output FROM action_trigger.event 
73 WHERE  event_def IN (
74     SELECT id FROM action_trigger.event_definition
75     WHERE owner IN
76     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
77 ));
78
79 DELETE FROM action_trigger.event
80 WHERE event_def IN (
81     SELECT id FROM action_trigger.event_definition
82     WHERE owner IN
83     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
84 );
85 DELETE FROM action_trigger.environment
86 WHERE event_def IN (
87     SELECT id FROM action_trigger.event_definition
88     WHERE owner IN
89     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
90 );
91 DELETE FROM action_trigger.event_params
92 WHERE event_def IN (
93     SELECT id FROM action_trigger.event_definition
94     WHERE owner IN
95     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
96 );
97
98 DELETE FROM action_trigger.event_definition
99     WHERE owner IN
100     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del);
101
102 COMMIT;
103 DROP INDEX money.tmp_idx;
104 ALTER TABLE asset.copy_location ENABLE RULE protect_copy_location_delete;
105 ALTER TABLE action.circulation ENABLE TRIGGER action_circulation_target_copy_trig;