Skip to content

Instantly share code, notes, and snippets.

@alanorth
Last active August 30, 2023 06:31
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanorth/392c4660e8b022d99dfa to your computer and use it in GitHub Desktop.
Save alanorth/392c4660e8b022d99dfa to your computer and use it in GitHub Desktop.
Move DSpace collection(s) from one community to another. DSpace doesn't have a built-in tool to do this, so you have to use raw SQL commands.
#!/usr/bin/env bash
#
# Moves DSpace collections from one community to another. Takes a list of
# handles, then resolves their internal resource_id's and reassigns the
# community relationship. Assumed to be running as `postgres` Linux user.
#
# I don't think reindexing is required, because no metadata has changed,
# and therefore no search or browse indexes need to be updated. You might
# need to clear the Cocoon cache to see the updated breadcrumb trails.
#
# Alan Orth, March, 2016
# Exit on first error
set -o errexit
# Read handles to move into an array
# format:
#
# collection from_community to_community
#
# Handles are separated with tabs or spaces. Uses `mapfile` to read into
# an array.
mapfile -t items_to_move <<TO_MOVE
10568/3181 10568/2578 10568/27629
10568/1675 10568/312 10568/27629
TO_MOVE
# psql stuff
readonly DATABASE_NAME=dspace
readonly PSQL_BIN='/usr/bin/env psql'
# clean startup, and only print results
readonly PSQL_OPTS="--no-psqlrc --tuples-only --dbname $DATABASE_NAME"
# Get an internal resource id for a handle (community / collection)
get_resource_id() {
local handle=$1
local resource_id
local psql_cmd="SELECT resource_id FROM handle WHERE handle = '$handle'"
$PSQL_BIN $PSQL_OPTS --command "$psql_cmd" | sed -e '/^$/d' -e 's/^[ \t]*//' \
&& return 0 \
|| return 1
}
move_collection() {
local collection_id=$(get_resource_id $1)
local old_community_id=$(get_resource_id $2)
local new_community_id=$(get_resource_id $3)
local psql_cmd="UPDATE community2collection SET community_id=$new_community_id WHERE community_id=$old_community_id and collection_id=$collection_id"
$PSQL_BIN $PSQL_OPTS --echo-queries --command "$psql_cmd" \
&& return 0 \
|| return 1
}
main() {
local row
for row in "${items_to_move[@]}"
do
# make sure row isn't a comment
if [[ $row =~ ^[[:space:]]?# ]]; then
continue
fi
# call move_collection with format:
# move_collection 10658/123 10568/456 10568/789
move_collection $row
done
}
main
@woodyfon
Copy link

After need run the index-discovery -f

@alanorth
Copy link
Author

@woodyfon yes, though I always run index-discovery -b.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment