Skip to content

Commit

Permalink
Merge pull request #1285 from libris/feature/cleanup-manage-storage
Browse files Browse the repository at this point in the history
Clean up manage-whelk-storage.sh
  • Loading branch information
olovy authored Aug 14, 2023
2 parents 7f11dd7 + 7f1f144 commit e942a8e
Showing 1 changed file with 5 additions and 76 deletions.
81 changes: 5 additions & 76 deletions librisxl-tools/scripts/manage-whelk-storage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,22 @@ SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )
TOOLDIR=$(dirname $SCRIPT_DIR)

RECREATE_DB=false
NUKE_DEFINITIONS=false
RUN_MIGRATIONS=false

usage () {
cat <<EOF
Usage: ./manage-whelk-storage.sh -n <NAME> [-h <POSTGRES HOST>]
[-e <ELASTICSEARCH HOST>] [-D <POSTGRES USER>]
[-C <CREATEDB USER>] [-R] [-N]
[-D <POSTGRES USER>] [-C <CREATEDB USER>] [-R] [-N]
-n, --whelk-name
Instance name. Used as database name and ElasticSearch index.
Instance name. Used as database name.
-h, --db-host
PostgreSQL host to connect to.
Defaults to localhost.
-e, --es-host
ElasticSearch host to connect to.
Defaults to localhost.
-D, --db-user
PostgreSQL database user.
Expand All @@ -40,38 +33,19 @@ Usage: ./manage-whelk-storage.sh -n <NAME> [-h <POSTGRES HOST>]
If unset, runs 'createdb' without 'sudo'.
-R, --recreate-db
If set, will drop and recreate PostgreSQL and ElasticSearch
databases indicated by -n/--whelk-name, and reload schema.
If set, will drop and recreate PostgreSQL database indicated
by -n/--whelk-name, and reload schema.
Unset by default.
-M, --run-migrations
If set, will run all migration scripts on PostgreSQL database
indicated by -n/--whelk-name
Unset by default.
-N, --nuke-definitions
If set, will delete all definitions data from PostgreSQL and
ElasticSearch.
Unset by default.
EOF
}

delete_definitions() {
psql -h $DBHOST $DBUSER_ARG $WHELKNAME -c \
"DELETE FROM lddb__identifiers WHERE id in (SELECT id from lddb where ( data#>'{@graph,0,inDataset}' @> '[{\"@id\":\"https://id.kb.se/dataset/definitions\"}]' OR data#>>'{@graph,1,@id}' in ('https://id.kb.se/vocab/', 'https://id.kb.se/vocab/context', 'https://id.kb.se/vocab/display')) and collection = 'definitions');"
psql -h $DBHOST $DBUSER_ARG $WHELKNAME -c \
"DELETE FROM lddb where ( data#>'{@graph,0,inDataset}' @> '[{\"@id\":\"https://id.kb.se/dataset/definitions\"}]' OR data#>>'{@graph,1,@id}' in ('https://id.kb.se/vocab/', 'https://id.kb.se/vocab/context', 'https://id.kb.se/vocab/display')) and collection = 'definitions';"
psql -h $DBHOST $DBUSER_ARG $WHELKNAME -c \
"DELETE FROM lddb__versions where ( data#>'{@graph,0,inDataset}' @> '[{\"@id\":\"https://id.kb.se/dataset/definitions\"}]' OR data#>>'{@graph,1,@id}' in ('https://id.kb.se/vocab/', 'https://id.kb.se/vocab/context', 'https://id.kb.se/vocab/display')) and collection = 'definitions' ;"

curl -XPOST http://$ESHOST:9200/$ESINDEX/_delete_by_query \
-H 'Content-Type: application/json' \
-d '{ "query": { "match": { "meta.inDataset.@id": "https://id.kb.se/dataset/definitions" } } }'
}

while [[ $# -gt 0 ]]
do
key="$1"
Expand All @@ -85,14 +59,6 @@ do
DBHOST="$2"
shift
;;
-e|--es-host)
ESHOST="$2"
shift
;;
-E|--es-index)
ESINDEX="$2"
shift
;;
-D|--db-user)
DBUSER="$2"
shift
Expand All @@ -104,9 +70,6 @@ do
-R|--recreate-db)
RECREATE_DB=true
;;
-N|--nuke-definitions)
NUKE_DEFINITIONS=true
;;
-M|--run-migrations)
RUN_MIGRATIONS=true
;;
Expand All @@ -123,14 +86,6 @@ if [ -z "$DBHOST" ]; then
DBHOST="localhost"
fi

if [ -z "$ESHOST" ]; then
ESHOST="localhost"
fi

if [ -z "$ESINDEX" ]; then
ESINDEX="${WHELKNAME}"
fi

if [ "$DBUSER" ]; then
DBUSER_ARG="-U ${DBUSER}"
fi
Expand All @@ -150,21 +105,6 @@ if [ "$RECREATE_DB" = true ]; then
do
psql -h $DBHOST $DBUSER_ARG $WHELKNAME < $MIGRATIONFILE
done

echo ""
echo "(Re)creating ElasticSearch database..."
echo ""

curl -XDELETE http://$ESHOST:9200/$ESINDEX

echo ""
echo ""
curl -XPUT http://$ESHOST:9200/$ESINDEX \
-d@$TOOLDIR/elasticsearch/libris_config.json \
--header "Content-Type: application/json"
echo ""
echo ""

fi

if [ "$RUN_MIGRATIONS" = true ]; then
Expand All @@ -181,15 +121,4 @@ if [ "$RUN_MIGRATIONS" = true ]; then

echo ""
echo "Version after: $(psql -qtA -h $DBHOST $DBUSER_ARG $WHELKNAME -c "SELECT version FROM lddb__schema")"
fi

if [ "$NUKE_DEFINITIONS" = true ]; then
echo ""
echo "Nuking definitions data..."
echo ""

delete_definitions

echo ""
echo ""
fi
fi

0 comments on commit e942a8e

Please sign in to comment.