mig-init: loop through sorted mig-sql/ directory
[migration-tools.git] / export_evergreen_library
1 #!/bin/bash
2
3 # Copyright 2014, Equinox Software, Inc.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 # Simple script to export records belonging to a specific Evergreen OU
20 # and its descendents.
21 #
22 # Usage: export_evergreen_library shortname scratch_dir output_dir db_conn_params
23 #
24 # For example:
25 #
26 # $ mkdir inter out
27 # $ ./export_evergreen_library SYS1 inter out '-U evergreen'
28 #
29 # The script emits MARC bibs, bibs + MFHDs, undeleted patrons, all items
30 # for the OU, all loans for the OU's patrons and items, current hold
31 # requests, and patron billings. It also exports all serials for the
32 # entire consortium.
33 #
34 # The script expects to be run on a box that can access the Evergreen database
35 # via psql and an OpenSRF stack.
36
37 baseou=$1
38 workdir=$2
39 outdir=$3
40 dbconn=$4
41
42 export PATH=/openils/bin:$PATH
43
44 function select_bibs {
45     echo Selecting bib IDs for $baseou
46     psql $dbconn -A -t -vbaseou=$baseou > $workdir/bib_ids <<_END_
47 SELECT DISTINCT bre.id
48 FROM biblio.record_entry bre
49 JOIN asset.call_number acn ON (bre.id = acn.record)
50 WHERE NOT acn.deleted
51 AND acn.owning_lib IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou')
52 ORDER BY 1;
53 _END_
54 }
55
56 function export_marc_bibs {
57     echo Exporting bibs in MARC format
58     cat $workdir/bib_ids | marc_export --format USMARC --encoding UTF-8 > $outdir/${baseou}_bibs.mrc
59 }
60
61 function select_bibs_with_mfhds {
62     echo Selecting MFHD IDs for $baseou
63     psql $dbconn -A -t -vbaseou=$baseou > $workdir/bibs_with_serial_ids <<_END_
64 SELECT DISTINCT sre.record
65 FROM serial.record_entry sre
66 WHERE NOT deleted
67 AND sre.owning_lib IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou')
68 ORDER BY 1;
69 _END_
70 }
71
72 function export_bibs_with_mfhds {
73     echo Exporting MFHDs in MARC format
74     if [[ ! -s $workdir/bibs_with_serial_ids ]];
75     then
76         # work-around for marc_export
77         cat /dev/null $outdir/${baseou}_bibs_and_mfhds
78     else
79         cat $workdir/bibs_with_serial_ids | marc_export --format USMARC --encoding UTF-8 --mfhd > $outdir/${baseou}_bibs_and_mfhds.mrc
80     fi
81 }
82
83 function export_table_description {
84     local table=$1
85     local subdir=$2
86     psql $dbconn -c "\\d $table"   >  $subdir/$table.description
87     psql $dbconn -c "\\dt+ $table" >> $subdir/$table.description
88 }
89
90 function export_entire_table {
91     local table=$1
92     local subdir=$2
93     echo Exporting table $table
94     export_table_description $table $subdir
95     psql $dbconn -A -t -c "\\COPY $table TO $subdir/$table.tsv"
96 }
97
98 function export_table_ou_subset {
99     local table=$1
100     local limitcol=$2
101     local subdir=$3
102     local clause=$4
103     echo "Exporting table $table (limited to OU $baseou)"
104     export_table_description $table $subdir
105     psql $dbconn -A -t -c "\\COPY (SELECT * FROM $table WHERE $limitcol IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou') $clause) TO $subdir/$table.tsv"
106 }
107
108 function export_table_item_subset {
109     local table=$1
110     local limitcol=$2
111     local subdir=$3
112     local clause=$4
113     echo "Exporting table $table (limited by items)"
114     export_table_description $table $subdir
115     psql $dbconn -A -t -c "\\COPY (SELECT * FROM $table WHERE $limitcol IN (SELECT id FROM asset.copy WHERE circ_lib IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou')) $clause) TO $subdir/$table.tsv"
116 }
117
118 function export_table_patron_subset {
119     local table=$1
120     local limitcol=$2
121     local subdir=$3
122     local clause=$4
123     echo "Exporting table $table (limited by patrons)"
124     export_table_description $table $subdir
125     psql $dbconn -A -t -c "\\COPY (SELECT * FROM $table WHERE $limitcol IN (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou') AND NOT deleted) $clause) TO $subdir/$table.tsv"
126 }
127
128 function export_table_patron_item_subset {
129     local table=$1
130     local limitcol1=$2
131     local limitcol2=$3
132     local subdir=$4
133     local clause=$5
134     echo "Exporting table $table (limited by patrons and items)"
135     export_table_description $table $subdir
136     psql $dbconn -A -t -c "\\COPY (SELECT * FROM $table WHERE $limitcol1 IN (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou') AND NOT deleted) AND $limitcol2 IN (SELECT id FROM asset.copy WHERE circ_lib IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou')) $clause) TO $subdir/$table.tsv"
137 }
138
139 function export_table_xact_subset {
140     local table=$1
141     local limitcol=$2
142     local subdir=$3
143     local clause=$4
144     echo "Exporting table $table (limited by xacts)"
145     export_table_description $table $subdir
146     psql $dbconn -A -t -c "\\COPY (SELECT * FROM $table WHERE $limitcol IN (SELECT id FROM money.billable_xact WHERE usr IN (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou') AND NOT deleted)) $clause) TO $subdir/$table.tsv"
147 }
148
149 function export_items {
150     mkdir -p $outdir/items
151     export_entire_table config.copy_status $outdir/items
152     export_entire_table config.circ_modifier $outdir/items
153     export_entire_table config.floating_group $outdir/items
154     export_entire_table config.floating_group_member $outdir/items
155     export_entire_table config.rule_age_hold_protect $outdir/items
156     export_entire_table config.rule_circ_duration $outdir/items
157     export_entire_table config.rule_max_fine $outdir/items
158     export_entire_table config.rule_recurring_fine $outdir/items
159     export_entire_table asset.call_number_prefix $outdir/items
160     export_entire_table asset.call_number_suffix $outdir/items
161     export_entire_table asset.stat_cat $outdir/items
162     export_entire_table asset.stat_cat_entry $outdir/items
163     export_table_ou_subset actor.org_unit id $outdir/items
164     export_table_ou_subset asset.copy_location owning_lib $outdir/items
165     export_table_ou_subset asset.copy circ_lib $outdir/items
166     export_table_ou_subset asset.call_number owning_lib $outdir/items
167     export_table_item_subset asset.copy_note owning_copy $outdir/items
168     export_table_item_subset asset.stat_cat_entry_copy_map owning_copy $outdir/items
169 }
170
171 function export_patrons {
172     mkdir -p $outdir/patrons
173     export_entire_table config.net_access_level $outdir/patrons
174     export_entire_table config.standing_penalty $outdir/patrons
175     export_entire_table actor.stat_cat $outdir/patrons
176     export_entire_table actor.stat_cat_entry $outdir/patrons
177     export_table_ou_subset actor.usr home_ou $outdir/patrons "AND NOT deleted"
178     export_table_patron_subset actor.card usr $outdir/patrons
179     export_table_patron_subset actor.usr_setting usr $outdir/patrons
180     export_table_patron_subset actor.usr_standing_penalty usr $outdir/patrons
181     export_table_patron_subset actor.usr_note usr $outdir/patrons
182     export_table_patron_subset actor.usr_address usr $outdir/patrons
183     export_table_patron_subset actor.usr_saved_search owner $outdir/patrons
184     export_table_patron_subset actor.stat_cat_entry_usr_map target_usr $outdir/patrons
185 }
186
187 function export_loans {
188     mkdir -p $outdir/loans
189     export_table_patron_item_subset action.circulation usr target_copy $outdir/loans
190 }
191
192 function export_hold_requests {
193     mkdir -p $outdir/current_hold_requests
194     export_table_patron_subset action.hold_request usr $outdir/current_hold_requests 'AND cancel_time IS NULL AND fulfillment_time IS NULL AND (expire_time IS NULL OR expire_time > NOW())'
195 }
196
197 function export_billings {
198     mkdir -p $outdir/patron_bills
199     export_entire_table config.billing_type $outdir/patron_bills
200     export_table_xact_subset money.billing xact $outdir/patron_bills
201     export_table_xact_subset money.payment xact $outdir/patron_bills
202     export_table_patron_subset money.grocery usr $outdir/patron_bills
203 }
204
205 function export_serials {
206     mkdir -p $outdir/serials
207     export_entire_table serial.basic_summary $outdir/serials
208     export_entire_table serial.index_summary $outdir/serials
209     export_entire_table serial.supplement_summary $outdir/serials
210     export_entire_table serial.caption_and_pattern $outdir/serials
211     export_entire_table serial.distribution $outdir/serials
212     export_entire_table serial.distribution_note $outdir/serials
213     export_entire_table serial.issuance $outdir/serials
214     export_entire_table serial.item $outdir/serials
215     export_entire_table serial.item_note $outdir/serials
216     export_entire_table serial.materialized_holding_code $outdir/serials
217     export_entire_table serial.stream $outdir/serials
218     export_entire_table serial.unit $outdir/serials
219     export_entire_table serial.routing_list_user $outdir/serials
220     export_entire_table serial.subscription $outdir/serials
221     export_entire_table serial.subscription_note $outdir/serials
222 }
223
224 select_bibs
225 export_marc_bibs
226 export_items
227 export_patrons
228 export_loans
229 export_hold_requests
230 export_billings
231
232 select_bibs_with_mfhds
233 export_bibs_with_mfhds
234 export_serials