#!/bin/sh # this script prepare database and redis instance to run accpetance test # # NOTE: assumes existance of a "template_postgis" # NOTE2: use PG* environment variables to control who and where # # NOTE3: a side effect of the db preparation is the persistent creation # of two database roles which will be valid for the whole cluster # TODO: fix that # PREPARE_REDIS=yes PREPARE_PGSQL=yes DOWNLOAD_SQL_FILES=yes PG_PARALLEL=$(pg_config --version | (awk '{$2*=1000; if ($2 >= 9600) print 1; else print 0;}' 2> /dev/null || echo 0)) while [ -n "$1" ]; do OPTION=$(echo "$1" | tr -d '[:space:]') if [[ "$OPTION" == "--skip-pg" ]]; then PREPARE_PGSQL=no shift; continue elif [[ "$OPTION" == "--skip-redis" ]]; then PREPARE_REDIS=no shift; continue elif [[ "$OPTION" == "--no-sql-download" ]]; then DOWNLOAD_SQL_FILES=no shift; continue else shift; continue; fi done die() { msg=$1 echo "${msg}" >&2 exit 1 } # This is where postgresql connection parameters are read from TESTENV=../../config/environments/test.js if [ \! -r ${TESTENV} ]; then echo "Cannot read ${TESTENV}" >&2 exit 1 fi TESTUSERID=1 TESTUSER=`node -e "console.log(require('${TESTENV}').postgres_auth_user || '')"` if test -z "$TESTUSER"; then echo "Missing postgres_auth_user from ${TESTENV}" >&2 exit 1 fi TESTUSER=`echo ${TESTUSER} | sed "s/<%= user_id %>/${TESTUSERID}/"` TESTPASS=`node -e "console.log(require('${TESTENV}').postgres_auth_pass || 'test')"` # TODO: should postgres_auth_pass be optional ? if test -z "$TESTPASS"; then echo "Missing postgres_auth_pass from ${TESTENV}" >&2 exit 1 fi TESTPASS=`echo ${TESTPASS} | sed "s/<%= user_id %>/${TESTUSERID}/"` TEST_DB="${TESTUSER}_db" # NOTE: will be set by caller trough environment if test -z "$REDIS_PORT"; then REDIS_PORT=6333; fi PUBLICUSER=`node -e "console.log(require('${TESTENV}').postgres.user || 'xxx')"` PUBLICPASS=`node -e "console.log(require('${TESTENV}').postgres.password || 'xxx')"` echo "PUBLICUSER: ${PUBLICUSER}" echo "PUBLICPASS: ${PUBLICPASS}" echo "TESTUSER: ${TESTUSER}" echo "TESTPASS: ${TESTPASS}" # Sets the env variable POSTGIS_VERSION as Major * 10000 + Minor * 100 + Patch # For example, for 2.4.5 ~> 20405 auto_postgis_version() { local POSTGIS_STR=$(psql -c "Select default_version from pg_available_extensions WHERE name = 'postgis';" -t); local pg_version=$(echo $POSTGIS_STR | awk -F '.' '{print $1 * 10000 + $2 * 100 + $3}') echo $pg_version } if [ -z "$POSTGIS_VERSION" ]; then export POSTGIS_VERSION=$(auto_postgis_version) echo "POSTGIS_VERSION: ${POSTGIS_VERSION}" fi if test x"$PREPARE_PGSQL" = xyes; then echo "preparing postgres..." dropdb "${TEST_DB}" createdb -Ttemplate_postgis -EUTF8 "${TEST_DB}" || die "Could not create test database" LOCAL_SQL_SCRIPTS='analysis_catalog windshaft.test gadm4 countries_null_values ported/populated_places_simple_reduced cdb_analysis_check cdb_invalidate_varnish' REMOTE_SQL_SCRIPTS='CDB_QueryStatements CDB_QueryTables CDB_CartodbfyTable CDB_TableMetadata CDB_ForeignTable CDB_UserTables CDB_ColumnNames CDB_ZoomFromScale CDB_OverviewsSupport CDB_Overviews CDB_QuantileBins CDB_JenksBins CDB_HeadsTailsBins CDB_EqualIntervalBins CDB_Hexagon CDB_XYZ CDB_EstimateRowCount CDB_RectangleGrid' if test x"$DOWNLOAD_SQL_FILES" = xyes; then CURL_ARGS="" for i in ${REMOTE_SQL_SCRIPTS} do CURL_ARGS="${CURL_ARGS}\"https://github.com/CartoDB/cartodb-postgresql/raw/master/scripts-available/$i.sql\" -o sql/$i.sql " done echo "Downloading and updating: ${REMOTE_SQL_SCRIPTS}" echo ${CURL_ARGS} | xargs curl -L -s fi psql -c "CREATE EXTENSION IF NOT EXISTS plpythonu;" ${TEST_DB} ALL_SQL_SCRIPTS="${REMOTE_SQL_SCRIPTS} ${LOCAL_SQL_SCRIPTS}" for i in ${ALL_SQL_SCRIPTS} do # Strip PARALLEL labels for PostgreSQL releases before 9.6 if [ $PG_PARALLEL -eq 0 ]; then TMPFILE=$(mktemp /tmp/$(basename $0).XXXXXXXX) sed -e 's/PARALLEL \= [A-Z]*,/''/g' \ -e 's/PARALLEL [A-Z]*/''/g' sql/$i.sql > $TMPFILE mv $TMPFILE sql/$i.sql fi cat sql/${i}.sql | sed -e 's/cartodb\./public./g' -e "s/''cartodb''/''public''/g" | sed "s/:PUBLICUSER/${PUBLICUSER}/" | sed "s/:PUBLICPASS/${PUBLICPASS}/" | sed "s/:TESTUSER/${TESTUSER}/" | sed "s/:TESTPASS/${TESTPASS}/" | PGOPTIONS='--client-min-messages=WARNING' psql -q -v ON_ERROR_STOP=1 ${TEST_DB} > /dev/null || exit 1 done fi if test x"$PREPARE_REDIS" = xyes; then echo "preparing redis..." cat <