From 300839d5b762190ebd6755cfb5da87c47924c657 Mon Sep 17 00:00:00 2001 From: pvincent Date: Sat, 19 Oct 2024 10:08:13 +0400 Subject: [PATCH] db drop unless prod --- scripts/db-maria | 17 +++++++++++++++++ scripts/db-psql | 15 +++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/scripts/db-maria b/scripts/db-maria index 967ddae..1c5cb79 100755 --- a/scripts/db-maria +++ b/scripts/db-maria @@ -21,6 +21,10 @@ synopsis() { printf "\t restore \n" printf "\t ---------------------------\n" printf "\t rename \n" + + if [[ "$TARGET" != 'prod' ]]; then + printf "\t drop # unless PROD!\n" + fi } list() { @@ -163,10 +167,17 @@ rename() { console "drop database \\\`$DB_NAME\\\`" } +drop() { + console "drop user \\\`$DB_NAME\\\`" + console "drop database \\\`$DB_NAME\\\`" + echo "$DB_NAME dropped!" +} + # MAIN set -Eeuo pipefail # shellcheck source=/dev/null . "$MIAOU_BASEDIR/lib/functions.sh" +TARGET=$(grep -Es "^target:" /etc/miaou/defaults.yaml | cut -d ' ' -f2) [[ $# -lt 1 ]] && synopsis && exit 1 ACTION=$1 @@ -214,6 +225,12 @@ rename) NEW_NAME=$3 rename ;; +drop) + [[ $# -lt 2 ]] && synopsis && exit 1 + [[ "$TARGET" = 'prod' ]] && synopsis && exit 2 + DB_NAME=$2 + drop + ;; *) synopsis exit 1 diff --git a/scripts/db-psql b/scripts/db-psql index 3809009..43ca6c5 100755 --- a/scripts/db-psql +++ b/scripts/db-psql @@ -20,6 +20,9 @@ synopsis() { printf "\t restore [--yes]\n" printf "\t ---------------------------\n" printf "\t rename \n" + if [[ "$TARGET" != 'prod' ]]; then + printf "\t drop # unless PROD!\n" + fi } list() { @@ -134,11 +137,17 @@ rename() { console "psql -c \"ALTER DATABASE \\\"$DB_NAME\\\" RENAME TO \\\"$DB_NEW_NAME\\\" \"" console "psql -c \"ALTER USER \\\"$DB_NAME\\\" RENAME TO \\\"$DB_NEW_NAME\\\" \"" console "psql -c \"ALTER USER \\\"$DB_NEW_NAME\\\" PASSWORD '$DB_NEW_NAME' \"" +} +drop() { + console "dropdb $DB_NAME" + console "dropuser $DB_NAME" + echo "$DB_NAME dropped!" } # MAIN . "$MIAOU_BASEDIR/lib/init.sh" +TARGET=$(grep -Es "^target:" /etc/miaou/defaults.yaml | cut -d ' ' -f2) [[ $# -lt 1 ]] && synopsis && exit 1 ACTION=$1 @@ -193,6 +202,12 @@ rename) DB_NEW_NAME=$3 rename ;; +drop) + [[ $# -lt 2 ]] && synopsis && exit 1 + [[ "$TARGET" = 'prod' ]] && synopsis && exit 2 + DB_NAME=$2 + drop + ;; *) synopsis exit 1