change to the data directly automatically with mig env use
[migration-tools.git] / remove_ou_data / 09_remove_bibs_stage_2.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 vol_del_table ORGUNIT_volume_bibs
20 \set ECHO all
21 \timing
22
23 ALTER TABLE biblio.monograph_part DISABLE RULE protect_mono_part_delete;
24
25 BEGIN;
26
27 DELETE FROM authority.bib_linking WHERE bib IN 
28 (
29     SELECT record FROM esi.:vol_del_table x
30     WHERE NOT EXISTS (select 1 from asset.call_number where record = x.record)
31 );
32
33 DELETE FROM metabib.browse_entry_def_map WHERE source IN 
34 (
35     SELECT record FROM esi.:vol_del_table x
36     WHERE NOT EXISTS (select 1 from asset.call_number where record = x.record)
37 );
38
39 DELETE FROM acq.lineitem WHERE eg_bib_id IN 
40 (
41     SELECT record FROM esi.:vol_del_table x
42     WHERE NOT EXISTS (select 1 from asset.call_number where record = x.record)
43 );
44
45 DELETE FROM biblio.monograph_part WHERE record IN 
46 (
47     SELECT record FROM esi.:vol_del_table x
48     WHERE NOT EXISTS (select 1 from asset.call_number where record = x.record)
49 );
50
51 DELETE FROM biblio.peer_bib_copy_map WHERE peer_record IN 
52 (
53     SELECT record FROM esi.:vol_del_table x
54     WHERE NOT EXISTS (select 1 from asset.call_number where record = x.record)
55 );
56
57 DELETE FROM acq.lineitem WHERE queued_record IN (
58     SELECT id FROM vandelay.queued_bib_record WHERE imported_as IN
59     (
60         SELECT record FROM esi.:vol_del_table x
61         WHERE NOT EXISTS (select 1 from asset.call_number where record = x.record)
62     )
63 );
64
65 DELETE FROM acq.acq_lineitem_history WHERE queued_record IN (
66     SELECT id FROM vandelay.queued_bib_record WHERE imported_as IN
67     (
68         SELECT record FROM esi.:vol_del_table x
69         WHERE NOT EXISTS (select 1 from asset.call_number where record = x.record)
70     )
71 );
72
73 DELETE FROM vandelay.queued_bib_record WHERE imported_as IN 
74 (
75     SELECT record FROM esi.:vol_del_table x
76     WHERE NOT EXISTS (select 1 from asset.call_number where record = x.record)
77 );
78
79 DELETE FROM metabib.record_attr_vector_list WHERE source IN 
80 (
81     SELECT record FROM esi.:vol_del_table x
82     WHERE NOT EXISTS (select 1 from asset.call_number where record = x.record)
83 );
84
85 DELETE FROM metabib.record_sorter WHERE source IN 
86 (
87     SELECT record FROM esi.:vol_del_table x
88     WHERE NOT EXISTS (select 1 from asset.call_number where record = x.record)
89 );
90
91 DELETE FROM booking.resource_type WHERE record IN 
92 (
93     SELECT record FROM esi.:vol_del_table x
94     WHERE NOT EXISTS (select 1 from asset.call_number where record = x.record)
95 );
96
97 DELETE FROM metabib.display_entry WHERE record IN
98 (
99     SELECT record FROM esi.:vol_del_table x
100     WHERE NOT EXISTS (select 1 from asset.call_number where record = x.record)
101 );
102
103 DELETE FROM metabib.real_full_rec WHERE record IN
104 (
105     SELECT record FROM esi.:vol_del_table x
106     WHERE NOT EXISTS (select 1 from asset.call_number where record = x.record)
107 );
108
109 COMMIT;
110
111
112 ALTER TABLE biblio.monograph_part ENABLE RULE protect_mono_part_delete;