add tables for call number affixes
[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, undeleted patrons, all items for the OU, all loans
30 # for the OU's patrons and items, current hold requests, and patron billings.
31 #
32 # The script expects to be run on a box that can access the Evergreen database
33 # via psql and an OpenSRF stack.
34
35 baseo=u$1
36 workdir=$2
37 outdir=$3
38 dbconn=$4
39
40 export PATH=/openils/bin:$PATH
41
42 function select_bibs {
43     echo Selecting bib IDs for $baseou
44     psql $dbconn -A -t -vbaseou=$baseou > $workdir/bib_ids <<_END_
45 SELECT DISTINCT bre.id
46 FROM biblio.record_entry bre
47 JOIN asset.call_number acn ON (bre.id = acn.record)
48 WHERE NOT acn.deleted
49 AND acn.owning_lib IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou')
50 ORDER BY 1;
51 _END_
52 }
53
54 function export_marc_bibs {
55     echo Exporting bibs in MARC format
56     cat $workdir/bib_ids | marc_export --format USMARC --encoding UTF-8 > $outdir/${baseou}_bibs.mrc
57 }
58
59 function export_table_description {
60     local table=$1
61     local subdir=$2
62     psql $dbconn -c "\\d $table"   >  $subdir/$table.description
63     psql $dbconn -c "\\dt+ $table" >> $subdir/$table.description
64 }
65
66 function export_entire_table {
67     local table=$1
68     local subdir=$2
69     echo Exporting table $table
70     export_table_description $table $subdir
71     psql $dbconn -A -t -c "\\COPY $table TO $subdir/$table.tsv"
72 }
73
74 function export_table_ou_subset {
75     local table=$1
76     local limitcol=$2
77     local subdir=$3
78     local clause=$4
79     echo "Exporting table $table (limited to OU $baseou)"
80     export_table_description $table $subdir
81     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"
82 }
83
84 function export_table_item_subset {
85     local table=$1
86     local limitcol=$2
87     local subdir=$3
88     local clause=$4
89     echo "Exporting table $table (limited by items)"
90     export_table_description $table $subdir
91     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"
92 }
93
94 function export_table_patron_subset {
95     local table=$1
96     local limitcol=$2
97     local subdir=$3
98     local clause=$4
99     echo "Exporting table $table (limited by patrons)"
100     export_table_description $table $subdir
101     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"
102 }
103
104 function export_table_patron_item_subset {
105     local table=$1
106     local limitcol1=$2
107     local limitcol2=$3
108     local subdir=$4
109     local clause=$5
110     echo "Exporting table $table (limited by patrons and items)"
111     export_table_description $table $subdir
112     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"
113 }
114
115 function export_table_xact_subset {
116     local table=$1
117     local limitcol=$2
118     local subdir=$3
119     local clause=$4
120     echo "Exporting table $table (limited by xacts)"
121     export_table_description $table $subdir
122     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"
123 }
124
125 function export_items {
126     mkdir -p $outdir/items
127     export_entire_table config.copy_status $outdir/items
128     export_entire_table config.circ_modifier $outdir/items
129     export_entire_table config.floating_group $outdir/items
130     export_entire_table config.floating_group_member $outdir/items
131     export_entire_table config.rule_age_hold_protect $outdir/items
132     export_entire_table config.rule_circ_duration $outdir/items
133     export_entire_table config.rule_max_fine $outdir/items
134     export_entire_table config.rule_recurring_fine $outdir/items
135     export_entire_table asset.call_number_prefix $outdir/items
136     export_entire_table asset.call_number_suffix $outdir/items
137     export_entire_table asset.stat_cat $outdir/items
138     export_entire_table asset.stat_cat_entry $outdir/items
139     export_table_ou_subset actor.org_unit id $outdir/items
140     export_table_ou_subset asset.copy_location owning_lib $outdir/items
141     export_table_ou_subset asset.copy circ_lib $outdir/items
142     export_table_ou_subset asset.call_number owning_lib $outdir/items
143     export_table_item_subset asset.copy_note owning_copy $outdir/items
144     export_table_item_subset asset.stat_cat_entry_copy_map owning_copy $outdir/items
145 }
146
147 function export_patrons {
148     mkdir -p $outdir/patrons
149     export_entire_table config.net_access_level $outdir/patrons
150     export_entire_table config.standing_penalty $outdir/patrons
151     export_entire_table actor.stat_cat $outdir/patrons
152     export_entire_table actor.stat_cat_entry $outdir/patrons
153     export_table_ou_subset actor.usr home_ou $outdir/patrons "AND NOT deleted"
154     export_table_patron_subset actor.card usr $outdir/patrons
155     export_table_patron_subset actor.usr_setting usr $outdir/patrons
156     export_table_patron_subset actor.usr_standing_penalty usr $outdir/patrons
157     export_table_patron_subset actor.usr_note usr $outdir/patrons
158     export_table_patron_subset actor.usr_address usr $outdir/patrons
159     export_table_patron_subset actor.usr_saved_search owner $outdir/patrons
160     export_table_patron_subset actor.stat_cat_entry_usr_map target_usr $outdir/patrons
161 }
162
163 function export_loans {
164     mkdir -p $outdir/loans
165     export_table_patron_item_subset action.circulation usr target_copy $outdir/loans
166 }
167
168 function export_hold_requests {
169     mkdir -p $outdir/current_hold_requests
170     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())'
171 }
172
173 function export_billings {
174     mkdir -p $outdir/patron_bills
175     export_entire_table config.billing_type $outdir/patron_bills
176     export_table_xact_subset money.billing xact $outdir/patron_bills
177     export_table_xact_subset money.payment xact $outdir/patron_bills
178     export_table_patron_subset money.grocery usr $outdir/patron_bills
179 }
180
181 select_bibs
182 export_marc_bibs
183 export_items
184 export_patrons
185 export_loans
186 export_hold_requests
187 export_billings