add removing usr circ history to remove_ou scripts
[migration-tools.git] / remove_ou_data / 14_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
25 BEGIN;
26
27 DELETE FROM actor.org_address WHERE org_unit IN
28 (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del);
29
30 UPDATE actor.org_unit SET ill_address = NULL, holds_address = NULL, mailing_address = NULL, billing_address = NULL
31 WHERE id IN
32 (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del);
33
34 UPDATE action.circulation SET copy_location = 1
35 WHERE copy_location IN (SELECT id FROM asset.copy_location WHERE owning_lib IN 
36 (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del));
37 DELETE FROM asset.copy_location WHERE owning_lib IN
38 (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del);
39
40 DELETE FROM money.billing
41 WHERE btype IN (
42     SELECT id FROM config.billing_type
43     WHERE owner IN
44     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
45 );
46
47 DELETE FROM action_trigger.event
48 WHERE event_def IN (
49     SELECT id FROM action_trigger.event_definition
50     WHERE owner IN
51     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
52 );
53 DELETE FROM action_trigger.environment
54 WHERE event_def IN (
55     SELECT id FROM action_trigger.event_definition
56     WHERE owner IN
57     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
58 );
59 DELETE FROM action_trigger.event_params
60 WHERE event_def IN (
61     SELECT id FROM action_trigger.event_definition
62     WHERE owner IN
63     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
64 );
65 DELETE FROM action_trigger.event_definition
66     WHERE owner IN
67     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del);
68
69 COMMIT;
70 DROP INDEX money.tmp_idx;
71 ALTER TABLE action.circulation ENABLE TRIGGER action_circulation_target_copy_trig;