3 # Copyright 2014, Equinox Software, Inc.
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.
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.
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.
19 # Simple script to export records belonging to a specific Evergreen OU
20 # and its descendents.
22 # Usage: export_evergreen_library shortname scratch_dir output_dir db_conn_params
27 # $ ./export_evergreen_library SYS1 inter out '-U evergreen'
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.
32 # The script expects to be run on a box that can access the Evergreen database
33 # via psql and an OpenSRF stack.
40 export PATH=/openils/bin:$PATH
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)
49 AND acn.owning_lib IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou')
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
59 function export_table_description {
62 psql $dbconn -c "\\d $table" > $subdir/$table.description
63 psql $dbconn -c "\\dt+ $table" >> $subdir/$table.description
66 function export_entire_table {
69 echo Exporting table $table
70 export_table_description $table $subdir
71 psql $dbconn -A -t -c "\\COPY $table TO $subdir/$table.tsv"
74 function export_table_ou_subset {
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"
84 function export_table_item_subset {
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"
94 function export_table_patron_subset {
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"
104 function export_table_patron_item_subset {
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"
115 function export_table_xact_subset {
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"
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
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
163 function export_loans {
164 mkdir -p $outdir/loans
165 export_table_patron_item_subset action.circulation usr target_copy $outdir/loans
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())'
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