· 6 years ago · Apr 20, 2019, 08:34 PM
1#!/bin/bash -e
2
3if [[ $# -lt 3 ]]; then
4 echo "Usage: $0 <manifest> <db config> <TENANT> [--clear-loan-audit]";
5 echo " e.g. $0 manifests/5kItems.manifest s3://fse-folio-eis-us-east-1-dev/db/postgres-conf.json fs00000000";
6 echo " $0 manifests/S3.manifest psql.conf diku";
7
8 exit 1;
9fi
10
11if [[ "${2}" == s3:* ]]; then
12 echo "aws s3 cp ${2} . | cut -d: -f3 | cut -d\ -f3"
13 DBCONF=`aws s3 cp ${2} . | cut -d: -f3 | cut -d\ -f3`
14else
15 DBCONF=${2}
16fi
17
18export TENANT=${3}
19
20if [[ "${4}" == "--clear-loan-audit" ]]; then
21 DO_CLEAR_LOAN_AUDIT=true;
22elif [[ -n ${4} ]]; then
23 echo "Unknown argument: ${4}";
24 exit 2;
25else
26 DO_CLEAR_LOAN_AUDIT=false;
27fi
28
29export WORK_DIR=/tmp/$(uuidgen)
30mkdir ${WORK_DIR}
31echo Working directory is ${WORK_DIR}
32
33export PGDATABASE=`cat $DBCONF | jq '.database' | cut -d\" -f2`
34export PGUSER=`cat $DBCONF | jq '.username' | cut -d\" -f2`
35export PGPASSWORD=`cat $DBCONF | jq '.password' | cut -d\" -f2`
36export PGHOST=`cat $DBCONF | jq '.host' | cut -d\" -f2`
37export PGPORT=`cat $DBCONF | jq '.port' | cut -d\" -f2`
38
39export USERS=`cat ${1} | jq '.users' | cut -d\" -f2`
40export USER_GROUPS=`cat ${1} | jq '.groups' | cut -d\" -f2`
41export USER_ADDRESSTYPES=`cat ${1} | jq '.addressTypes' | cut -d\" -f2`
42# export LOGINS=`cat ${1} | jq '.logins' | cut -d\" -f2`
43# export PERMS=`cat ${1} | jq '.permissions' | cut -d\" -f2`
44export MATERIALS=`cat ${1} | jq '.materials' | cut -d\" -f2`
45export LOAN_TYPES=`cat ${1} | jq '.loanTypes' | cut -d\" -f2`
46export ITEMS=`cat ${1} | jq '.items' | cut -d\" -f2`
47export CIRCULATION=`cat ${1} | jq '.circulation' | cut -d\" -f2`
48export REQUESTS=`jq -r '.requests' ${1}`
49export NOTES=`jq -r '.notes' ${1}`
50export INSTANCES=`jq -r '.instances' ${1}`
51export NOTIFICATIONS=`jq -r '.notifications' ${1}`
52# export SERVICE_POINTS=`jq -r '.servicePoints' ${1}`
53# export SERVICE_POINTS_USERS=`jq -r '.servicePointsUsers' ${1}`
54export LOC_LIBRARIES=`jq -r '.locLibraries' ${1}`
55export LOC_CAMPUSES=`jq -r '.locCampuses' ${1}`
56export LOC_INSTITUTIONS=`jq -r '.locInstitutions' ${1}`
57export LOCATIONS=`jq -r '.locations' ${1}`
58export IDENTIFIER_TYPES=`jq -r '.identifiertypes' ${1}`
59export CLASSIFICATION_TYPES=`jq -r '.classificationTypes' ${1}`
60export RECORD_HOLDINGS=`jq -r '.recordholdings' ${1}`
61export INSTANCE_TYPES=`jq -r '.instancetypes' ${1}`
62export CONTRIBUTOR_NAME_TYPES=`jq -r '.contributorNameTypes' ${1}`
63export CONTRIBUTOR_TYPES=`jq -r '.contributorTypes' ${1}`
64export INSTANCE_FORMATS=`jq -r '.instanceFormats' ${1}`
65export OWNERS=`jq -r '.owners' ${1}`
66export FEE_FINES=`jq -r '.feefines' ${1}`
67export FEE_FINES_ACCOUNTS=`jq -r '.feefinesAccounts' ${1}`
68export REQUEST_CANCELLATION_REASONS=`jq -r '.requestCancellationReasons' ${1}`
69export WORKFLOW_STATUS=`jq -r '.workflowStatus' ${1}`
70export RECEIPT_STATUS=`jq -r '.receiptStatus' ${1}`
71export S3DATASET=`jq -r '.s3dataset' ${1}`
72# export TEMPLATES=`jq -r '.template' ${1}`
73# export VALIDATION_RULES=`jq -r '.validation' ${1}`
74# export METADATA_REPLACEMENTS=(${INSTANCE_FORMATS} ${INSTANCE_TYPES} ${OWNERS} ${FEE_FINES} ${REQUEST_CANCELLATION_REASONS} ${USER_GROUPS} ${LOCATIONS} ${SERVICE_POINTS} )
75export METADATA_REPLACEMENTS=(${INSTANCE_FORMATS} ${INSTANCE_TYPES} ${OWNERS} ${FEE_FINES} ${REQUEST_CANCELLATION_REASONS} ${USER_GROUPS} ${LOCATIONS} )
76
77if [[ "${S3DATASET}" == s3:* ]]; then
78 echo "aws s3 cp ${S3DATASET} . | cut -d: -f3 | cut -d/ -f5 | cut -d ' ' -f1"
79 DATASET=`aws s3 cp ${S3DATASET} . | cut -d: -f3 | cut -d/ -f5 | cut -d ' ' -f1`
80 tar xfv ${DATASET}
81 DATASET=`echo ${S3DATASET} | cut -d/ -f4`
82 FILENAME=`echo ${S3DATASET} | cut -d/ -f5`
83 # Delete local tar file after extracting
84 if [ -f ${FILENAME} ]; then
85 rm -rf ${FILENAME}
86 fi
87fi
88
89# Update the names file institutional user. This user is the tenant name.
90# We alse make a backup of the names.tsv file so it can be restored later
91# and reused for other tenants.
92if [[ -e "${USERS}" ]]; then
93 sed -i.orig "s/{TENANT}/${TENANT}/g" ${USERS}
94fi
95
96# Update loan and due dates
97if [[ -e "${CIRCULATION}" ]]; then
98 export CIRCULATION_TEMPLATE=${CIRCULATION}
99 export CIRCULATION=${WORK_DIR}/circulation.tsv
100 cp ${CIRCULATION_TEMPLATE} ${CIRCULATION}
101
102 loanDate=`date '+%Y-%m-%dT%H:%M:%SZ' -d '-15 days' 2>/dev/null || date -v-15d '+%Y-%m-%dT%H:%M:%SZ'`
103 dueDate=`date '+%Y-%m-%dT%H:%M:%S.000+0000' -d '+15 days' 2>/dev/null || date -v+15d '+%Y-%m-%dT%H:%M:%S.000+0000'`
104
105 sed -i -e "s/{LOAN_DATE}/${loanDate}/g" ${CIRCULATION}
106 sed -i -e "s/{DUE_DATE}/${dueDate}/g" ${CIRCULATION}
107fi
108
109#populate metadata field
110for i in "${METADATA_REPLACEMENTS[@]}"
111 do
112 if [[ -e "$i" ]]; then
113 ADMIN_UUID=`head -1 ${USERS} | cut -d' ' -f1`
114 # ADMIN_UUID=`a27e49b4-3c78-4fa7-9ec4-2f73244452d9`
115 DATE=`date +%Y-%m-%dT%H:%M:%S.000+0000`
116 METADATA="\"metadata\":{\"createdDate\":\"${DATE}\",\"createdByUserId\":\"${ADMIN_UUID}\",\"updatedDate\":\"${DATE}\",\"updatedByUserId\":\"${ADMIN_UUID}\"}"
117 sed -i.orig "s/{METADATA}/${METADATA}/g" ${i}
118 fi
119done
120
121echo "================================================="
122echo "PGUSER: $PGUSER"
123echo "PGPASSWORD: $PGPASSWORD"
124echo "PGDATABASE: $PGDATABASE"
125echo "PGHOST: $PGHOST"
126echo "PGPORT: $PGPORT"
127echo "TENANT: $TENANT"
128echo ""
129# echo "USERS: $USERS"
130echo "USER_GROUPS: $USER_GROUPS"
131# echo "LOGINS: $LOGINS"
132# echo "PERMS: $PERMS"
133echo "MATERIALS: $MATERIALS"
134echo "LOAN_TYPES: $LOAN_TYPES"
135echo "ITEMS: $ITEMS"
136echo "CIRCULATION: $CIRCULATION_TEMPLATE"
137echo "REQUESTS: $REQUESTS"
138echo "NOTES: $NOTES"
139echo "INSTANCES: $INSTANCES"
140echo "NOTIFICATIONS: $NOTIFICATIONS"
141# echo "SERVICE_POINTS: $SERVICE_POINTS"
142# echo "SERVICE_POINTS_USERS: $SERVICE_POINTS_USERS"
143echo "LOC_LIBRARIES: $LOC_LIBRARIES"
144echo "LOC_CAMPUSES: $LOC_CAMPUSES"
145echo "LOC_INSTITUTIONS: $LOC_INSTITUTIONS"
146echo "LOCATIONS: $LOCATIONS"
147echo "IDENTIFIER_TYPES: $IDENTIFIER_TYPES"
148echo "CLASSIFICATION_TYPES: $CLASSIFICATION_TYPES"
149echo "RECORD_HOLDINGS: $RECORD_HOLDINGS"
150echo "INSTANCE_TYPES: $INSTANCE_TYPES"
151echo "CONTRIBUTOR_NAME_TYPES: $CONTRIBUTOR_NAME_TYPES"
152echo "CONTRIBUTOR_TYPES: $CONTRIBUTOR_TYPES"
153echo "INSTANCE_FORMATS: $INSTANCE_FORMATS"
154echo "OWNERS: $OWNERS"
155echo "FEE_FINES: $FEE_FINES"
156echo "FEE_FINES_ACCOUNTS: $FEE_FINES_ACCOUNTS"
157echo "REQUEST_CANCELLATION_REASONS: $REQUEST_CANCELLATION_REASONS"
158echo "WORKFLOW_STATUS: $WORKFLOW_STATUS"
159echo "RECEIPT_STATUS: $RECEIPT_STATUS"
160echo "S3DATASET: $S3DATASET"
161# echo "TEMPLATES: $TEMPLATES"
162# echo "VALIDATION_RULES: $VALIDATION_RULES"
163echo "=================================================="
164
165# ==================================================
166# clear and re-populate the database
167# ==================================================
168
169RUN_PSQL="psql -X --set AUTOCOMMIT=off --set ON_ERROR_STOP=on "
170
171#if file exists
172[ -e ${WORK_DIR}/delete.sql ] && rm ${WORK_DIR}/delete.sql
173
174# test "null" != "${ITEMS}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.item DROP CONSTRAINT IF EXISTS item_pkey;" >> ${WORK_DIR}/delete.sql
175# test "null" != "${ITEMS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.item_materialtypeid_idx_gin;" >> ${WORK_DIR}/delete.sql
176# test "null" != "${ITEMS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.item_status_name_idx_gin;" >> ${WORK_DIR}/delete.sql
177# test "null" != "${ITEMS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.item_barcode_idx_gin;" >> ${WORK_DIR}/delete.sql
178# test "null" != "${ITEMS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.item_title_idx_gin;" >> ${WORK_DIR}/delete.sql
179# test "null" != "${ITEMS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.item_id_idx;" >> ${WORK_DIR}/delete.sql
180# test "null" != "${ITEMS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.item_status_name_idx;" >> ${WORK_DIR}/delete.sql
181# test "null" != "${ITEMS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.item_barcode_idx;" >> ${WORK_DIR}/delete.sql
182# test "null" != "${ITEMS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.item_materialtypeid_idx;" >> ${WORK_DIR}/delete.sql
183# test "null" != "${ITEMS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.item_holdingsrecordid_idx;" >> ${WORK_DIR}/delete.sql
184
185# test "null" != "${INSTANCES}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.instance_source_marc DROP CONSTRAINT IF EXISTS instance_source_marc__id_fkey;" >> ${WORK_DIR}/delete.sql
186# test "null" != "${INSTANCES}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.instance_source_marc DROP CONSTRAINT IF EXISTS instance_source_marc__id_fkey1;" >> ${WORK_DIR}/delete.sql
187# test "null" != "${INSTANCES}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.instance_relationship DROP COLUMN IF EXISTS superinstanceid CASCADE;" >> ${WORK_DIR}/delete.sql
188# test "null" != "${INSTANCES}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.instance_relationship DROP COLUMN IF EXISTS subinstanceid CASCADE;" >> ${WORK_DIR}/delete.sql
189test "null" != "${INSTANCES}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.instance_relationship DROP CONSTRAINT IF EXISTS instance_relationship_superinstanceid_fkey CASCADE;" >> ${WORK_DIR}/delete.sql
190
191# test "null" != "${RECORD_HOLDINGS}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.holdings_record DROP CONSTRAINT IF EXISTS holdings_record_instanceid_fkey;" >> ${WORK_DIR}/delete.sql
192# test "null" != "${INSTANCES}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.instance DROP CONSTRAINT IF EXISTS instance_pkey;" >> ${WORK_DIR}/delete.sql
193# test "null" != "${INSTANCES}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.instance_title_idx_gin;" >> ${WORK_DIR}/delete.sql
194# test "null" != "${INSTANCES}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.instance_id_idx;" >> ${WORK_DIR}/delete.sql
195# test "null" != "${INSTANCES}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.instance_title_idx;" >> ${WORK_DIR}/delete.sql
196# test "null" != "${INSTANCES}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.instance_subjects_idx_gin;" >> ${WORK_DIR}/delete.sql
197# test "null" != "${INSTANCES}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.instance_classifications_idx_gin;" >> ${WORK_DIR}/delete.sql
198# test "null" != "${INSTANCES}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.instance_languages_idx_gin;" >> ${WORK_DIR}/delete.sql
199# test "null" != "${INSTANCES}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.instance_instancetypeid_idx_gin;" >> ${WORK_DIR}/delete.sql
200# test "null" != "${INSTANCES}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.instance_identifiers_idx_gin;" >> ${WORK_DIR}/delete.sql
201# test "null" != "${INSTANCES}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.instance_contributors_idx_gin;" >> ${WORK_DIR}/delete.sql
202# test "null" != "${INSTANCES}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.instance_publication_idx;" >> ${WORK_DIR}/delete.sql
203# test "null" != "${INSTANCES}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.instance_contributors_idx;" >> ${WORK_DIR}/delete.sql
204
205# test "null" != "${ITEMS}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.item DROP CONSTRAINT IF EXISTS item_holdingsrecordid_fkey;" >> ${WORK_DIR}/delete.sql
206# test "null" != "${RECORD_HOLDINGS}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.holdings_record DROP CONSTRAINT IF EXISTS holdings_record_pkey;" >> ${WORK_DIR}/delete.sql
207# test "null" != "${RECORD_HOLDINGS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.holdings_record_permanentlocationid_idx_gin;" >> ${WORK_DIR}/delete.sql
208# test "null" != "${RECORD_HOLDINGS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.holdings_record_instanceid_idx_gin;" >> ${WORK_DIR}/delete.sql
209# test "null" != "${RECORD_HOLDINGS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.holdings_record_permanentlocationid_idx;" >> ${WORK_DIR}/delete.sql
210# test "null" != "${RECORD_HOLDINGS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.holdings_record_instanceid_idx;" >> ${WORK_DIR}/delete.sql
211# test "null" != "${RECORD_HOLDINGS}" && echo "DROP INDEX IF EXISTS ${TENANT}_mod_inventory_storage.holdings_record_id_idx;" >> ${WORK_DIR}/delete.sql
212
213test "null" != "${CIRCULATION}" && echo "DELETE FROM ${TENANT}_mod_circulation_storage.loan WHERE true;" >> ${WORK_DIR}/delete.sql
214test "null" != "${CIRCULATION}" && test "${DO_CLEAR_LOAN_AUDIT}" = true && echo "DELETE FROM ${TENANT}_mod_circulation_storage.audit_loan WHERE true;" >> ${WORK_DIR}/delete.sql
215
216test "null" != "${REQUESTS}" && echo "DELETE FROM ${TENANT}_mod_circulation_storage.request WHERE true;" >> ${WORK_DIR}/delete.sql
217# test "null" != "${PERMS}" && echo "DELETE FROM ${TENANT}_mod_permissions.permissions_users WHERE true;" >> ${WORK_DIR}/delete.sql
218# test "null" != "${LOGINS}" && echo "DELETE FROM ${TENANT}_mod_login.auth_credentials WHERE true;" >> ${WORK_DIR}/delete.sql
219test "null" != "${USER_ADDRESSTYPES}" && echo "DELETE FROM ${TENANT}_mod_users.addresstype WHERE true;" >> ${WORK_DIR}/delete.sql
220# test "null" != "${SERVICE_POINTS_USERS}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.service_point_user WHERE true;" >> ${WORK_DIR}/delete.sql
221# test "null" != "${USERS}" && echo "DELETE FROM ${TENANT}_mod_users.users WHERE true;" >> ${WORK_DIR}/delete.sql
222test "null" != "${USER_GROUPS}" && echo "DELETE FROM ${TENANT}_mod_users.groups WHERE true;" >> ${WORK_DIR}/delete.sql
223test "null" != "${ITEMS}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.item WHERE true;" >> ${WORK_DIR}/delete.sql
224test "null" != "${MATERIALS}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.material_type WHERE true;" >> ${WORK_DIR}/delete.sql
225test "null" != "${LOAN_TYPES}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.loan_type WHERE true;" >> ${WORK_DIR}/delete.sql
226test "null" != "${RECORD_HOLDINGS}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.holdings_record WHERE true;" >> ${WORK_DIR}/delete.sql
227test "null" != "${INSTANCES}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.instance_relationship WHERE true;" >> ${WORK_DIR}/delete.sql
228test "null" != "${INSTANCES}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.instance WHERE true;" >> ${WORK_DIR}/delete.sql
229test "null" != "${LOCATIONS}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.location WHERE true;" >> ${WORK_DIR}/delete.sql
230test "null" != "${LOC_LIBRARIES}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.loclibrary WHERE true;" >> ${WORK_DIR}/delete.sql
231test "null" != "${LOC_CAMPUSES}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.loccampus WHERE true;" >> ${WORK_DIR}/delete.sql
232test "null" != "${LOC_INSTITUTIONS}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.locinstitution WHERE true;" >> ${WORK_DIR}/delete.sql
233# For now, let's drop the service point users table so the service points table
234# can be updated. We'll add service point users via another user story.
235# test "null" != "${SERVICE_POINTS}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.service_point WHERE true;" >> ${WORK_DIR}/delete.sql
236test "null" != "${IDENTIFIER_TYPES}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.identifier_type WHERE true;" >> ${WORK_DIR}/delete.sql
237test "null" != "${CLASSIFICATION_TYPES}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.classification_type WHERE true;" >> ${WORK_DIR}/delete.sql
238test "null" != "${INSTANCE_TYPES}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.instance_type WHERE true;" >> ${WORK_DIR}/delete.sql
239test "null" != "${NOTES}" && echo "DELETE FROM ${TENANT}_mod_notes.note_data WHERE true;" >> ${WORK_DIR}/delete.sql
240test "null" != "${NOTIFICATIONS}" && echo "DELETE FROM ${TENANT}_mod_notify.notify_data WHERE true;" >> ${WORK_DIR}/delete.sql
241test "null" != "${CONTRIBUTOR_NAME_TYPES}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.contributor_name_type WHERE true;" >> ${WORK_DIR}/delete.sql
242test "null" != "${CONTRIBUTOR_TYPES}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.contributor_type WHERE true;" >> ${WORK_DIR}/delete.sql
243test "null" != "${INSTANCE_FORMATS}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.instance_format WHERE true;" >> ${WORK_DIR}/delete.sql
244test "null" != "${FEE_FINES}" && echo "DELETE FROM ${TENANT}_mod_feesfines.feefines WHERE true;" >> ${WORK_DIR}/delete.sql
245test "null" != "${OWNERS}" && echo "DELETE FROM ${TENANT}_mod_feesfines.owners WHERE true;" >> ${WORK_DIR}/delete.sql
246test "null" != "${FEE_FINES_ACCOUNTS}" && echo "DELETE FROM ${TENANT}_mod_feesfines.accounts WHERE true;" >> ${WORK_DIR}/delete.sql
247test "null" != "${REQUEST_CANCELLATION_REASONS}" && echo "DELETE FROM ${TENANT}_mod_circulation_storage.cancellation_reason WHERE true;" >> ${WORK_DIR}/delete.sql
248test "null" != "${WORKFLOW_STATUS}" && echo "DELETE FROM ${TENANT}_mod_orders_storage.workflow_status WHERE true;" >> ${WORK_DIR}/delete.sql
249test "null" != "${RECEIPT_STATUS}" && echo "DELETE FROM ${TENANT}_mod_orders_storage.receipt_status WHERE true;" >> ${WORK_DIR}/delete.sql
250# test "null" != "${TEMPLATES}" && echo "DELETE FROM ${TENANT}_mod_template_engine.template WHERE true;" >> ${WORK_DIR}/delete.sql
251# test "null" != "${VALIDATION_RULES}" && echo "DELETE FROM ${TENANT}_mod_password_validator.validation_rules WHERE true;" >> ${WORK_DIR}/delete.sql
252
253# N.B.: for now, lets drop the MARC source data if INSTANCES is set. If there is
254# data in the DB, the FK to instance(_id) will potentially fail (definitely in a
255# vagrant box) when we add it back below, since the instance IDs might change.
256# At some point we may have actual MARC data that correspond to our instance
257# data and we can remove old/add new properly.
258test "null" != "${INSTANCES}" && echo "DELETE FROM ${TENANT}_mod_inventory_storage.instance_source_marc WHERE true;" >> ${WORK_DIR}/delete.sql
259echo "commit;" >> ${WORK_DIR}/delete.sql
260
261cat ${WORK_DIR}/delete.sql
262${RUN_PSQL} < ${WORK_DIR}/delete.sql
263
264test "null" != "${USER_GROUPS}" && psql -a -c "\copy ${TENANT}_mod_users.groups(id, jsonb) FROM '${USER_GROUPS}' DELIMITER E'\t'"
265test "null" != "${USER_ADDRESSTYPES}" && psql -a -c "\copy ${TENANT}_mod_users.addresstype(id, jsonb) FROM '${USER_ADDRESSTYPES}' DELIMITER E'\t'"
266# test "null" != "${USERS}" && psql -a -c "\copy ${TENANT}_mod_users.users(id, jsonb) FROM '${USERS}' DELIMITER E'\t'"
267# test "null" != "${LOGINS}" && psql -a -c "\copy ${TENANT}_mod_login.auth_credentials(_id, jsonb) FROM ${LOGINS} DELIMITER E'\t'"
268# test "null" != "${PERMS}" && psql -a -c "\copy ${TENANT}_mod_permissions.permissions_users(_id, jsonb) FROM '${PERMS}' DELIMITER E'\t'"
269# test "null" != "${SERVICE_POINTS}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.service_point(_id, jsonb) FROM '${SERVICE_POINTS}' DELIMITER E'\t'"
270test "null" != "${LOC_INSTITUTIONS}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.locinstitution(_id, jsonb) FROM '${LOC_INSTITUTIONS}' DELIMITER E'\t'"
271test "null" != "${LOC_CAMPUSES}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.loccampus(_id, jsonb) FROM '${LOC_CAMPUSES}' DELIMITER E'\t'"
272test "null" != "${LOC_LIBRARIES}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.loclibrary(_id, jsonb) FROM '${LOC_LIBRARIES}' DELIMITER E'\t'"
273test "null" != "${LOCATIONS}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.location(_id, jsonb) FROM '${LOCATIONS}' DELIMITER E'\t'"
274test "null" != "${CONTRIBUTOR_NAME_TYPES}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.contributor_name_type(_id, jsonb) FROM '${CONTRIBUTOR_NAME_TYPES}' DELIMITER E'\t'"
275test "null" != "${CONTRIBUTOR_TYPES}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.contributor_type(_id, jsonb) FROM '${CONTRIBUTOR_TYPES}' DELIMITER E'\t'"
276test "null" != "${INSTANCE_FORMATS}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.instance_format(_id, jsonb) FROM '${INSTANCE_FORMATS}' DELIMITER E'\t'"
277test "null" != "${INSTANCE_TYPES}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.instance_type(_id, jsonb) FROM '${INSTANCE_TYPES}' DELIMITER E'\t'"
278test "null" != "${IDENTIFIER_TYPES}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.identifier_type(_id, jsonb) FROM '${IDENTIFIER_TYPES}' DELIMITER E'\t'"
279test "null" != "${CLASSIFICATION_TYPES}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.classification_type(_id, jsonb) FROM '${CLASSIFICATION_TYPES}' DELIMITER E'\t'"
280test "null" != "${MATERIALS}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.material_type(_id, jsonb) FROM '${MATERIALS}' DELIMITER E'\t'"
281test "null" != "${LOAN_TYPES}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.loan_type(_id, jsonb) FROM '${LOAN_TYPES}' DELIMITER E'\t'"
282test "null" != "${INSTANCES}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.instance(_id, jsonb) FROM '${INSTANCES}' csv quote e'\x01' DELIMITER E'\t'"
283test "null" != "${RECORD_HOLDINGS}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.holdings_record(_id, jsonb, permanentLocationId) FROM '${RECORD_HOLDINGS}' DELIMITER E'\t'"
284test "null" != "${ITEMS}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.item(_id, jsonb, permanentloantypeid, temporaryloantypeid, materialTypeId) FROM '${ITEMS}' DELIMITER E'\t'"
285test "null" != "${CIRCULATION}" && psql -a -c "\copy ${TENANT}_mod_circulation_storage.loan(_id, jsonb) FROM '${CIRCULATION}' DELIMITER E'\t'"
286test "null" != "${REQUESTS}" && psql -a -c "\copy ${TENANT}_mod_circulation_storage.request(_id, jsonb) FROM '${REQUESTS}' DELIMITER E'\t'"
287test "null" != "${NOTES}" && psql -a -c "\copy ${TENANT}_mod_notes.note_data(id, jsonb) FROM '${NOTES}' DELIMITER E'\t'"
288test "null" != "${NOTIFICATIONS}" && psql -a -c "\copy ${TENANT}_mod_notify.notify_data(id, jsonb) FROM '${NOTIFICATIONS}' DELIMITER E'\t'"
289test "null" != "${OWNERS}" && psql -a -c "\copy ${TENANT}_mod_feesfines.owners(id, jsonb) FROM '${OWNERS}' DELIMITER E'\t'"
290test "null" != "${FEE_FINES}" && psql -a -c "\copy ${TENANT}_mod_feesfines.feefines(id, jsonb) FROM '${FEE_FINES}' DELIMITER E'\t'"
291test "null" != "${FEE_FINES_ACCOUNTS}" && psql -a -c "\copy ${TENANT}_mod_feesfines.accounts(id, jsonb) FROM '${FEE_FINES_ACCOUNTS}' DELIMITER E'\t'"
292test "null" != "${REQUEST_CANCELLATION_REASONS}" && psql -a -c "\copy ${TENANT}_mod_circulation_storage.cancellation_reason(_id, jsonb) FROM '${REQUEST_CANCELLATION_REASONS}' DELIMITER E'\t'"
293test "null" != "${WORKFLOW_STATUS}" && psql -a -c "\copy ${TENANT}_mod_orders_storage.workflow_status(id, jsonb) FROM '${WORKFLOW_STATUS}' DELIMITER E'\t'"
294test "null" != "${RECEIPT_STATUS}" && psql -a -c "\copy ${TENANT}_mod_orders_storage.receipt_status(id, jsonb) FROM '${RECEIPT_STATUS}' DELIMITER E'\t'"
295# test "null" != "${SERVICE_POINTS_USERS}" && psql -a -c "\copy ${TENANT}_mod_inventory_storage.service_point_user(_id, jsonb, creation_date, created_by, defaultservicepointid ) FROM '${SERVICE_POINTS_USERS}' DELIMITER E'\t'"
296# test "null" != "${TEMPLATES}" && psql -a -c "\copy ${TENANT}_mod_template_engine.template(_id, jsonb, creation_date, created_by ) FROM '${TEMPLATES}' DELIMITER E'\t'"
297# test "null" != "${VALIDATION_RULES}" && psql -a -c "\copy ${TENANT}_mod_password_validator.validation_rules(_id, jsonb, creation_date, created_by ) FROM '${VALIDATION_RULES}' DELIMITER E'\t'"
298
299#if file exists
300[ -e ${WORK_DIR}/create_index.sql ] && rm ${WORK_DIR}/create_index.sql
301
302# test "null" != "${ITEMS}" && echo "CREATE INDEX item_materialtypeid_idx_gin ON ${TENANT}_mod_inventory_storage.item USING gin (lower(f_unaccent((jsonb ->> 'materialTypeId'::text))) gin_trgm_ops);" >> ${WORK_DIR}/create_index.sql
303# test "null" != "${ITEMS}" && echo "CREATE INDEX item_status_name_idx_gin ON ${TENANT}_mod_inventory_storage.item USING gin (lower(f_unaccent(((jsonb -> 'status'::text) ->> 'name'::text))) gin_trgm_ops);" >> ${WORK_DIR}/create_index.sql
304# test "null" != "${ITEMS}" && echo "CREATE INDEX item_barcode_idx_gin ON ${TENANT}_mod_inventory_storage.item USING gin (lower(f_unaccent((jsonb ->> 'barcode'::text))) gin_trgm_ops);">> ${WORK_DIR}/create_index.sql
305# test "null" != "${ITEMS}" && echo "CREATE INDEX item_title_idx_gin ON ${TENANT}_mod_inventory_storage.item USING gin (lower(f_unaccent((jsonb ->> 'title'::text))) gin_trgm_ops);" >>${WORK_DIR}/create_index.sql
306# test "null" != "${ITEMS}" && echo "CREATE INDEX item_id_idx ON ${TENANT}_mod_inventory_storage.item USING btree (lower(f_unaccent(jsonb ->> 'id'::text)));" >> ${WORK_DIR}/create_index.sql
307# test "null" != "${ITEMS}" && echo "CREATE INDEX item_status_name_idx ON ${TENANT}_mod_inventory_storage.item USING btree (lower(f_unaccent(((jsonb -> 'status'::text) ->> 'name'::text))));" >> ${WORK_DIR}/create_index.sql
308# test "null" != "${ITEMS}" && echo "CREATE INDEX item_barcode_idx ON ${TENANT}_mod_inventory_storage.item USING btree (lower(f_unaccent((jsonb ->> 'barcode'::text))));" >> ${WORK_DIR}/create_index.sql
309# test "null" != "${ITEMS}" && echo "CREATE INDEX item_materialtypeid_idx ON ${TENANT}_mod_inventory_storage.item USING btree (lower(f_unaccent((jsonb ->> 'materialTypeId'::text))));" >> ${WORK_DIR}/create_index.sql
310# test "null" != "${ITEMS}" && echo "CREATE UNIQUE INDEX item_pkey ON ${TENANT}_mod_inventory_storage.item USING btree (_id);" >> ${WORK_DIR}/create_index.sql
311# test "null" != "${ITEMS}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.item ADD PRIMARY KEY USING INDEX item_pkey;" >> ${WORK_DIR}/create_index.sql
312# test "null" != "${ITEMS}" && echo "CREATE INDEX item_holdingsrecordid_idx ON ${TENANT}_mod_inventory_storage.item USING btree (lower(f_unaccent((jsonb ->> 'holdingsRecordId'::text))));" >> ${WORK_DIR}/create_index.sql
313
314# test "null" != "${INSTANCES}" && echo "CREATE INDEX instance_title_idx_gin ON ${TENANT}_mod_inventory_storage.instance USING gin (lower(f_unaccent((jsonb ->> 'title'::text))) gin_trgm_ops);" >> ${WORK_DIR}/create_index.sql
315# test "null" != "${INSTANCES}" && echo "CREATE INDEX instance_id_idx ON ${TENANT}_mod_inventory_storage.instance USING btree (lower(f_unaccent((jsonb ->> 'id'::text))));" >> ${WORK_DIR}/create_index.sql
316# test "null" != "${INSTANCES}" && echo "CREATE INDEX instance_title_idx ON ${TENANT}_mod_inventory_storage.instance USING btree (lower(f_unaccent((jsonb ->> 'title'::text))));" >> ${WORK_DIR}/create_index.sql
317# test "null" != "${INSTANCES}" && echo "CREATE UNIQUE INDEX instance_pkey ON ${TENANT}_mod_inventory_storage.instance USING btree (_id);" >> ${WORK_DIR}/create_index.sql
318# test "null" != "${INSTANCES}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.instance ADD PRIMARY KEY USING INDEX instance_pkey;" >> ${WORK_DIR}/create_index.sql
319# test "null" != "${INSTANCES}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.instance_source_marc ADD FOREIGN KEY (_id) REFERENCES ${TENANT}_mod_inventory_storage.instance;" >> ${WORK_DIR}/create_index.sql
320# test "null" != "${INSTANCES}" && echo "CREATE INDEX instance_subjects_idx_gin ON ${TENANT}_mod_inventory_storage.instance USING gin (lower(f_unaccent((jsonb ->> 'subjects'::text))) gin_trgm_ops);" >> ${WORK_DIR}/create_index.sql
321# test "null" != "${INSTANCES}" && echo "CREATE INDEX instance_classifications_idx_gin ON ${TENANT}_mod_inventory_storage.instance USING gin (lower(f_unaccent((jsonb ->> 'classifications'::text))) gin_trgm_ops);" >> ${WORK_DIR}/create_index.sql
322# test "null" != "${INSTANCES}" && echo "CREATE INDEX instance_languages_idx_gin ON ${TENANT}_mod_inventory_storage.instance USING gin (lower(f_unaccent((jsonb ->> 'languages'::text)))gin_trgm_ops);" >> ${WORK_DIR}/create_index.sql
323# test "null" != "${INSTANCES}" && echo "CREATE INDEX instance_instancetypeid_idx_gin ON ${TENANT}_mod_inventory_storage.instance USING gin (lower(f_unaccent((jsonb ->> 'instanceTypeId'::text))) gin_trgm_ops);" >> ${WORK_DIR}/create_index.sql
324# test "null" != "${INSTANCES}" && echo "CREATE INDEX instance_identifiers_idx_gin ON ${TENANT}_mod_inventory_storage.instance USING gin (lower(f_unaccent((jsonb ->> 'identifiers'::text))) gin_trgm_ops);" >> ${WORK_DIR}/create_index.sql
325# test "null" != "${INSTANCES}" && echo "CREATE INDEX instance_contributors_idx_gin ON ${TENANT}_mod_inventory_storage.instance USING gin (lower(f_unaccent((jsonb ->> 'contributors'::text))) gin_trgm_ops);" >> ${WORK_DIR}/create_index.sql
326# test "null" != "${INSTANCES}" && echo "CREATE INDEX instance_publication_idx ON ${TENANT}_mod_inventory_storage.instance USING btree (lower(f_unaccent((jsonb ->> 'publication'::text))));" >> ${WORK_DIR}/create_index.sql
327# test "null" != "${INSTANCES}" && echo "CREATE INDEX instance_contributors_idx ON ${TENANT}_mod_inventory_storage.instance USING btree (lower(f_unaccent((jsonb ->> 'contributors'::text))));" >> ${WORK_DIR}/create_index.sql
328# test "null" != "${INSTANCES}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.instance_relationship ADD COLUMN IF NOT EXISTS superinstanceid UUID REFERENCES ${TENANT}_mod_inventory_storage.instance;" >> ${WORK_DIR}/create_index.sql
329# test "null" != "${INSTANCES}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.instance_relationship ADD COLUMN IF NOT EXISTS subinstanceid UUID REFERENCES ${TENANT}_mod_inventory_storage.instance;" >> ${WORK_DIR}/create_index.sql
330
331# test "null" != "${RECORD_HOLDINGS}" && echo "CREATE INDEX holdings_record_permanentlocationid_idx_gin ON ${TENANT}_mod_inventory_storage.holdings_record USING gin (lower(f_unaccent((jsonb ->> 'permanentLocationId'::text))) gin_trgm_ops);" >> ${WORK_DIR}/create_index.sql
332# test "null" != "${RECORD_HOLDINGS}" && echo "CREATE INDEX holdings_record_instanceid_idx_gin ON ${TENANT}_mod_inventory_storage.holdings_record USING gin (lower(f_unaccent((jsonb ->>'instanceId'::text))) gin_trgm_ops);" >> ${WORK_DIR}/create_index.sql
333# test "null" != "${RECORD_HOLDINGS}" && echo "CREATE INDEX holdings_record_permanentlocationid_idx ON ${TENANT}_mod_inventory_storage.holdings_record USING btree (lower(f_unaccent((jsonb ->> 'permanentLocationId'::text))));" >> ${WORK_DIR}/create_index.sql
334# test "null" != "${RECORD_HOLDINGS}" && echo "CREATE INDEX holdings_record_instanceid_idx ON ${TENANT}_mod_inventory_storage.holdings_record USING btree (lower(f_unaccent((jsonb ->> 'instanceId'::text))));" >> ${WORK_DIR}/create_index.sql
335# test "null" != "${RECORD_HOLDINGS}" && echo "CREATE UNIQUE INDEX holdings_record_pkey ON ${TENANT}_mod_inventory_storage.holdings_record USING btree (_id);" >> ${WORK_DIR}/create_index.sql
336# test "null" != "${RECORD_HOLDINGS}" && echo "ALTER TABLE ${TENANT}_mod_inventory_storage.holdings_record ADD PRIMARY KEY USING INDEX holdings_record_pkey;" >> ${WORK_DIR}/create_index.sql
337# test "null" != "${RECORD_HOLDINGS}" && echo "CREATE INDEX holdings_record_id_idx ON ${TENANT}_mod_inventory_storage.holdings_record USING btree (lower(f_unaccent((jsonb ->> 'id'::text))));" >> ${WORK_DIR}/create_index.sql
338
339if [ -e ${WORK_DIR}/create_index.sql ]; then
340 echo "commit;" >> ${WORK_DIR}/create_index.sql
341 cat ${WORK_DIR}/create_index.sql
342 ${RUN_PSQL} < ${WORK_DIR}/create_index.sql
343fi
344
345# optimize postgres queries
346test "null" != "${ITEMS}" && psql -a -c "vacuum verbose analyze ${TENANT}_mod_inventory_storage.item;"
347test "null" != "${INSTANCES}" && psql -a -c "vacuum verbose analyze ${TENANT}_mod_inventory_storage.instance;"
348test "null" != "${RECORD_HOLDINGS}" && psql -a -c "vacuum verbose analyze ${TENANT}_mod_inventory_storage.holdings_record;"
349
350# Restore the original version (with the tenant placeholder)
351
352METADATA_REPLACEMENTS+=(${USERS})
353for i in "${METADATA_REPLACEMENTS[@]}"
354 do
355 [ -e ${i}.orig ] && mv ${i}.orig ${i}
356done
357
358# remove working directory
359rm -fr ${WORK_DIR}
360
361exit 0;