#!/bin/bash prefix='\033[' RED=$prefix'0;31m' UNDIM=$prefix'22m' RESET=$prefix'0m' REPODIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && cd ../.. && pwd )" source .env if [ -z "$DB_HOST" ]; then echo "No DB_HOST found" exit 1 fi if [ -z "$DB_NAME" ]; then echo "No DB_NAME found" exit 1 fi if [ -z "$DB_USER" ]; then echo "No DB_USER found" exit 1 fi if [ -z "$DB_PASS" ]; then echo "No DB_PASS found" exit 1 fi function handleError { if [ $? -ne 0 ]; then echo -e "$UNDIM$RESET" echo -e $RED"ERROR:"$RESET $1 exit 1 fi } function execFile { echo " $1" PGPASSWORD=$PG_PASS psql -v ON_ERROR_STOP=1 -h $DB_HOST -U postgres -d "$DB_NAME" -f $1 > /dev/null handleError "Could not execute file $1" } echo -e $RED"This will delete ALL DATA in the database $DB_NAME"$RESET read -p "Continue? " if [[ $REPLY =~ ^[Yy]$ ]] then : else echo "Cancelling" exit 1 fi read -s -p "Enter postgres Password: " PG_PASS echo PGPASSWORD=$PG_PASS psql -v ON_ERROR_STOP=1 -h $DB_HOST -U postgres -c "drop database if exists $DB_NAME" > /dev/null handleError "Could not remove database $DB_NAME" PGPASSWORD=$PG_PASS psql -v ON_ERROR_STOP=1 -h $DB_HOST -U postgres -c "create database $DB_NAME" > /dev/null handleError "Could not create database $DB_NAME" PGPASSWORD=$PG_PASS psql -v ON_ERROR_STOP=1 -h $DB_HOST -U postgres -c "drop user if exists $DB_USER" > /dev/null PGPASSWORD=$PG_PASS psql -v ON_ERROR_STOP=1 -h $DB_HOST -U postgres -c "create user $DB_USER with encrypted password '$DB_PASS';" > /dev/null for file in $REPODIR/db/sql/*.sql; do execFile $file done