changeset 255:c36bd39782c1

Added simple --drop to db setup script.
author Sascha Wilde <wilde@intevation.de>
date Fri, 27 Jul 2018 12:47:17 +0200
parents de6fdb316b8f
children 1ff9aec9326f
files schema/install-db.sh
diffstat 1 files changed, 37 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/schema/install-db.sh	Fri Jul 27 12:30:19 2018 +0200
+++ b/schema/install-db.sh	Fri Jul 27 12:47:17 2018 +0200
@@ -14,6 +14,7 @@
   -d, --db=NAME    connect to the database NAME
   -p, --port=PORT  connect do the postgresql cluster at PORT
   -D, --demo       install demo accounts and data
+      --drop       drop database and all roles
       --help       display this help and exit
 
 EOF
@@ -31,12 +32,12 @@
 db=gemma
 port=5432
 demo=0
-
+drop=0
 
 # Parse options:
 
 OPTS=`getopt \
-      -l help,demo,db:,port: \
+      -l help,demo,db:,port:,drop \
       -o Dd:p: -n "$ME" -- "$@"`
 [ $? -eq 0 ] || { usage ; exit 1 ; }
 
@@ -56,6 +57,10 @@
       demo=1
       shift 1
       ;;
+    --drop)
+      drop=1
+      shift 1
+      ;;
     --help)
       { usage ; exit 0 ; }
       ;;
@@ -69,16 +74,35 @@
 
 # Main ------------------------------------------------------------
 
-psql -p "$port" -f "$BASEDIR/roles.sql"
-createdb -p "$port" "$db"
-psql -p "$port" -d "$db" \
-     -f "$BASEDIR/gemma.sql" \
-     -f "$BASEDIR/manage_users.sql" \
-     -f "$BASEDIR/auth.sql"
+if [[ drop -eq 0 ]] ; then
+  # Default operation: create schema
+  psql -p "$port" -f "$BASEDIR/roles.sql"
+  createdb -p "$port" "$db"
+  psql -p "$port" -d "$db" \
+       -f "$BASEDIR/gemma.sql" \
+       -f "$BASEDIR/manage_users.sql" \
+       -f "$BASEDIR/auth.sql"
 
-if [[ $demo -eq 1 ]] ; then
-  psql -p "$port" -f "$BASEDIR/demo-data/responsibility_areas.sql" \
-       -d "$db"
-  psql -p "$port" -f "$BASEDIR/demo-data/roles.sql" \
-       -f "$BASEDIR/demo-data/users.sql" -d "$db"
+  if [[ $demo -eq 1 ]] ; then
+    psql -p "$port" -f "$BASEDIR/demo-data/responsibility_areas.sql" \
+         -d "$db"
+    psql -p "$port" -f "$BASEDIR/demo-data/roles.sql" \
+         -f "$BASEDIR/demo-data/users.sql" -d "$db"
+  fi
+else
+  # Evil mode: drop everything gemma
+  echo "Really drop database '$db' and alle gemma roles? [type 'yes']: "
+  read a
+  if [[ $a == "yes" ]] ; then
+    dropdb -p "$port" "$db"
+    for r in `psql -p 5433 -t -c '\du' | awk -F '|' \
+              '$3 ~/waterway_user|waterway_admin|sys_admin/ \
+               || $1 ~/waterway_user|waterway_admin|sys_admin/ \
+               {print $1}'`
+    do
+      dropuser -p "$port" "$r"
+    done
+  else
+    echo "No harm done."
+  fi
 fi