You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.9 KiB
Bash
76 lines
1.9 KiB
Bash
#!/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 {
|
|
FILE=$1
|
|
USERNAME=$2
|
|
PASSWORD=$3
|
|
echo " $FILE"
|
|
PGPASSWORD=$PASSWORD psql -v ON_ERROR_STOP=1 -h $DB_HOST -U $USERNAME -d "$DB_NAME" -f $FILE > /dev/null
|
|
handleError "Could not execute file $FILE"
|
|
}
|
|
|
|
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
|
|
|
|
execFile $REPODIR/db/sql/0-setup.sql postgres $PG_PASS
|
|
execFile $REPODIR/db/sql/1-tables.sql $DB_USER $DB_PASS
|
|
execFile $REPODIR/db/sql/2-views.sql $DB_USER $DB_PASS
|
|
execFile $REPODIR/db/sql/3-functions.sql $DB_USER $DB_PASS
|
|
execFile $REPODIR/db/sql/4-permissions.sql postgres $PG_PASS
|