diff --git a/.gitlab-ci.yaml b/.gitlab-ci.yaml index 93e4a76ad6bbd1ac99308fce503ff94f6b179a3a..53735df689141a4a29632dce72255e83b565a146 100644 --- a/.gitlab-ci.yaml +++ b/.gitlab-ci.yaml @@ -49,6 +49,7 @@ stages: - echo "${CI_RUNNER_ID}" > hive-sync-runner-id.txt - ./scripts/ci/wait-for-postgres.sh "$RUNNER_POSTGRES_HOST" "$RUNNER_POSTGRES_PORT" - export POSTGRES_MAJOR_VERSION=$(./scripts/ci/get-postgres-version.sh) + - ./scripts/ci/drop-db.sh - ./scripts/ci/create-db.sh - ./scripts/ci/hive-sync.sh - ./scripts/ci/collect-db-stats.sh diff --git a/scripts/ci/create-db.sh b/scripts/ci/create-db.sh index 0a006f0bf93ec6eb0956dd49024095830c6e6b0d..6f3eacc175911e73c01701e0ba9fe7cfd6f69f8e 100755 --- a/scripts/ci/create-db.sh +++ b/scripts/ci/create-db.sh @@ -28,10 +28,6 @@ BEGIN END \$$; --- We drop database to enable retry of CI job. -\echo Dropping database ${HIVEMIND_DB_NAME} -DROP DATABASE IF EXISTS ${HIVEMIND_DB_NAME}; - \echo Creating database ${HIVEMIND_DB_NAME} CREATE DATABASE ${HIVEMIND_DB_NAME} TEMPLATE ${TEMPLATE} OWNER ${RUNNER_POSTGRES_APP_USER}; diff --git a/scripts/ci/drop-db.sh b/scripts/ci/drop-db.sh new file mode 100755 index 0000000000000000000000000000000000000000..f3d928b5e1958f711bdb73d6ab757b14f5095549 --- /dev/null +++ b/scripts/ci/drop-db.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# set -euo pipefail + +drop_db() { + + echo "Dropping database ${HIVEMIND_DB_NAME}" + + PGPASSWORD=${RUNNER_POSTGRES_ADMIN_USER_PASSWORD} dropdb \ + --if-exists \ + --username "${RUNNER_POSTGRES_ADMIN_USER}" \ + --host ${RUNNER_POSTGRES_HOST} \ + --port ${RUNNER_POSTGRES_PORT} \ + ${HIVEMIND_DB_NAME} + + RESULT=$? + + if [[ ! $RESULT -eq 0 ]]; then + cat << EOF +ERROR: cannot drop database ${HIVEMIND_DB_NAME}. +Most often the reason is that database is used by other sessions. +This can happen on Gitlab CI server, when jobs are picked by multiple, +concurrent runners and database name is not unique on subsequent +pipelines. If this is the case, please cancel any pending pipelines +running for your branch or for your merge request, or wait until they +finish. Then retry this pipeline. +Exiting with error at this moment. +EOF + exit $RESULT + else + echo "Database ${HIVEMIND_DB_NAME} has been dropped successfully" + fi + +} + +drop_db