Skip to content

Instantly share code, notes, and snippets.

@alanorth
Last active April 12, 2016 15:04
Show Gist options
  • Save alanorth/72a70aca856d76f24c127a6e67b3342b to your computer and use it in GitHub Desktop.
Save alanorth/72a70aca856d76f24c127a6e67b3342b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Moves DSpace metadatavalues from one field to another. Assumed to be running
# as the `postgres` Linux user. You MUST perform a full Discovery reindex after
# doing this, ie: index-discovery -bf
#
# Alan Orth, April, 2016
# Exit on first error
set -o errexit
# IDs of fields to move, in this format:
#
# old_field new_field
#
# fields are separated with tabs or spaces. Uses bash's `mapfile` to read into
# an array.
mapfile -t fields_to_move <<TO_MOVE
66 109 #dc.type
#72 202 #cg.title.journal
#76 203 #cg.subject.ilri
#82 208 #cg.subject.cpwf
#88 210 #cg.subject.ccafs
#106 215 #cg.subject.cifor
#108 217 #cg.subject.iwmi
TO_MOVE
# psql stuff
readonly DATABASE_NAME=dspacetest
readonly PSQL_BIN="/usr/bin/env psql"
# clean startup, and only print results
readonly PSQL_OPTS="--no-psqlrc --tuples-only --dbname $DATABASE_NAME"
migrate_field() {
local old_id=$1
local new_id=$2
local psql_cmd="UPDATE metadatavalue SET metadata_field_id=${new_id} WHERE metadata_field_id=${old_id}"
$PSQL_BIN $PSQL_OPTS --echo-queries --command "$psql_cmd" \
&& return 0 \
|| return 1
}
main() {
local row
for row in "${fields_to_move[@]}"
do
# make sure row isn't a comment
if [[ $row =~ ^[[:space:]]?# ]]; then
continue
fi
# call migrate_field() with format:
# migrate_field 66 109
migrate_field $row
# relax!
sleep 1
done
}
main
# vim: set expandtab:ts=4:sw=4:bs=2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment