change to the data directly automatically with mig env use
[migration-tools.git] / remove_ou_data / 06_remove_copies.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 asset.copy DISABLE RULE protect_copy_delete;
23 ALTER TABLE asset.copy DISABLE TRIGGER audit_asset_copy_update_trigger;
24 CREATE INDEX tmp_import_as ON vandelay.import_item(imported_as);
25
26 BEGIN;
27
28 DELETE FROM asset.copy_alert WHERE copy IN (
29   SELECT id FROM asset.copy WHERE circ_lib IN
30     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
31 );
32
33
34 -- NOTE: no FK
35 DELETE FROM asset.opac_visible_copies WHERE copy_id IN (
36   SELECT id FROM asset.copy WHERE circ_lib IN
37     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
38 );
39 -- NOTE: no FK
40 DELETE FROM asset.copy_part_map WHERE target_copy IN (
41   SELECT id FROM asset.copy WHERE circ_lib IN
42     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
43 );
44
45 -- NOTE: no FK
46 DELETE FROM asset.stat_cat_entry_copy_map WHERE owning_copy IN (
47   SELECT id FROM asset.copy WHERE circ_lib IN
48     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
49 );
50
51 DELETE FROM asset.copy_note WHERE owning_copy IN (
52   SELECT id FROM asset.copy WHERE circ_lib IN
53     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
54 );
55
56 DELETE FROM vandelay.import_item WHERE imported_as IN (
57   SELECT id FROM asset.copy WHERE circ_lib IN
58     (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del)
59 );
60
61 DELETE FROM asset.copy WHERE circ_lib IN
62 (SELECT (actor.org_unit_descendants(id)).id from actor.org_unit where shortname = :ou_to_del);
63
64 DELETE FROM asset.latest_inventory WHERE copy IN 
65     (SELECT li.copy FROM asset.latest_inventory li LEFT JOIN asset.copy acp ON acp.id = li.copy WHERE acp.id IS NULL)
66 ;
67
68 COMMIT;
69
70 DROP INDEX vandelay.tmp_import_as;
71 ALTER TABLE asset.copy ENABLE RULE protect_copy_delete;
72 ALTER TABLE asset.copy ENABLE TRIGGER audit_asset_copy_update_trigger;