cleaned up version of arguments
[migration-tools.git] / remove_ou_data / 07_remove_serials.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 serial.record_entry DISABLE RULE protect_mfhd_delete;
23
24 BEGIN;
25
26 DELETE FROM serial.basic_summary WHERE distribution IN 
27 (SELECT id FROM serial.distribution WHERE holding_lib IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del));
28
29 DELETE FROM serial.distribution_note WHERE distribution IN 
30 (SELECT id FROM serial.distribution WHERE holding_lib IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del));
31
32 DELETE FROM serial.index_summary WHERE distribution IN 
33 (SELECT id FROM serial.distribution WHERE holding_lib IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del));
34
35 DELETE FROM serial.item WHERE stream IN 
36 (SELECT id FROM serial.stream WHERE distribution IN 
37 (SELECT id FROM serial.distribution WHERE holding_lib IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)));
38
39 DELETE FROM serial.item_note WHERE id IN 
40 (SELECT id FROM serial.item WHERE stream IN (SELECT id FROM serial.stream WHERE distribution IN 
41 (SELECT id FROM serial.distribution WHERE holding_lib IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del))));
42
43 DELETE FROM serial.stream WHERE distribution IN 
44 (SELECT id FROM serial.distribution WHERE holding_lib IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del));
45
46 DELETE FROM serial.supplement_summary WHERE distribution IN 
47 (SELECT id FROM serial.distribution WHERE holding_lib IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del));
48
49 DELETE FROM serial.distribution WHERE holding_lib IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del);
50
51 UPDATE serial.record_entry SET editor = 1 WHERE editor IN
52 (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));
53
54 UPDATE serial.record_entry SET creator = 1 WHERE creator 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 UPDATE serial.record_entry SET owning_lib = 1 
58 WHERE owning_lib IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
59 AND id IN (SELECT record_entry FROM serial.distribution WHERE holding_lib NOT IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del));
60
61 DELETE FROM serial.record_entry WHERE owning_lib IN (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del);
62
63 COMMIT;
64
65 ALTER TABLE serial.record_entry ENABLE RULE protect_mfhd_delete;