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.
79 lines
2.0 KiB
Bash
79 lines
2.0 KiB
Bash
5 years ago
|
#!/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
|
||
|
}
|
||
|
|
||
|
echo "Reading current state from database . . ."
|
||
|
|
||
|
FUNCTION_QUERY="SELECT 'DROP FUNCTION ' || ns.nspname || '.' || proname
|
||
|
|| '(' || oidvectortypes(proargtypes) || ');'
|
||
|
FROM pg_proc INNER JOIN pg_namespace ns ON (pg_proc.pronamespace = ns.oid)
|
||
|
WHERE ns.nspname = 'sos'
|
||
|
and proname not in ('uuid_generate_v1', 'uuid_generate_v1mc', 'uuid_generate_v3', 'uuid_generate_v5', 'uuid_generate_v4', 'uuid_nil', 'uuid_ns_dns', 'uuid_ns_oid', 'uuid_ns_url', 'uuid_ns_x500')
|
||
|
order by proname;"
|
||
|
|
||
|
|
||
|
VIEW_QUERY="SELECT 'DROP VIEW IF EXISTS ' || table_name || ' CASCADE;'
|
||
|
FROM information_schema.views
|
||
|
WHERE table_schema = 'sos';"
|
||
|
|
||
|
FUNCTION_DROP="$(PGPASSWORD="$DB_PASS" psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "$FUNCTION_QUERY" | grep "DROP FUNCTION")"
|
||
|
VIEW_DROP="$(PGPASSWORD="$DB_PASS" psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "$VIEW_QUERY" | grep "DROP VIEW")"
|
||
|
|
||
|
NUM_FUNCS=$(echo "$FUNCTION_DROP" | wc -l)
|
||
|
NUM_VIEWS=$(echo "$VIEW_DROP" | wc -l)
|
||
|
|
||
|
echo -e $RED"Warning: About to drop $NUM_FUNCS functions and $NUM_VIEWS views"$RESET
|
||
|
read -p "Continue? "
|
||
|
|
||
|
if [[ $REPLY =~ ^[Yy]$ ]]
|
||
|
then
|
||
|
:
|
||
|
else
|
||
|
echo "Cancelling"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
echo "Reinitializing views and functions . . ."
|
||
|
|
||
|
PGPASSWORD="$DB_PASS" psql -h $DB_HOST -U $DB_USER -d $DB_NAME \
|
||
|
--single-transaction \
|
||
|
-f <(echo "$FUNCTION_DROP") \
|
||
|
-f <(echo "$VIEW_DROP") \
|
||
|
-f $REPODIR/db/sql/2-views.sql \
|
||
|
-f $REPODIR/db/sql/3-functions.sql \
|
||
|
-f $REPODIR/db/sql/4-permissions.sql \
|
||
|
> /dev/null
|