From c336a3bab91186be1b998bba67e57d0797bd87ba Mon Sep 17 00:00:00 2001 From: Priyansh Date: Tue, 19 Aug 2025 14:13:01 +0530 Subject: fix foreign key constraints in anime entities --- scripts/resetdb.postgres.sh | 80 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100755 scripts/resetdb.postgres.sh (limited to 'scripts') diff --git a/scripts/resetdb.postgres.sh b/scripts/resetdb.postgres.sh new file mode 100755 index 0000000..0160aeb --- /dev/null +++ b/scripts/resetdb.postgres.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +DEFAULT_USER=$(whoami) + +read -p "Enter database name [metachan]: " DB_NAME +DB_NAME=${DB_NAME:-metachan} + +read -p "Enter database owner username [$DEFAULT_USER]: " DB_OWNER +DB_OWNER=${DB_OWNER:-$DEFAULT_USER} + +read -s -p "Enter password for $DB_OWNER (press Enter for no password): " DB_PASSWORD +echo + +if [ -z "$DB_PASSWORD" ]; then + PSQL_CMD="psql -d postgres" + CONNECTION_INFO="Connecting as current user without password" +else + export PGPASSWORD="$DB_PASSWORD" + PSQL_CMD="psql -U $DB_OWNER -d postgres" + CONNECTION_INFO="Connecting as $DB_OWNER with password" +fi + +DB_EXISTS=$($PSQL_CMD -t -c "SELECT 1 FROM pg_database WHERE datname='$DB_NAME';" 2>/dev/null | xargs) + +if [ "$DB_EXISTS" = "1" ]; then + ACTION="reset" + OPERATION="Database reset" +else + ACTION="create" + OPERATION="Database creation" +fi + +echo +echo "=== $OPERATION Configuration ===" +echo "Database name: $DB_NAME" +echo "Owner: $DB_OWNER" +echo "Connection: $CONNECTION_INFO" +if [ "$ACTION" = "reset" ]; then + echo "Status: Database exists and will be reset" +else + echo "Status: Database does not exist and will be created" +fi +echo + +read -p "Proceed with database $ACTION? [Y/n]: " CONFIRM +if [[ $CONFIRM =~ ^[Nn]$ ]]; then + echo "Operation cancelled." + exit 0 +fi + +if [ "$ACTION" = "reset" ]; then + echo "Resetting database '$DB_NAME'..." +else + echo "Creating database '$DB_NAME'..." +fi + +$PSQL_CMD -c "DROP DATABASE IF EXISTS $DB_NAME;" 2>/dev/null +if $PSQL_CMD -c "CREATE DATABASE $DB_NAME OWNER $DB_OWNER;" 2>/dev/null; then + if [ "$ACTION" = "reset" ]; then + echo "✅ Database '$DB_NAME' successfully reset with owner '$DB_OWNER'" + else + echo "✅ Database '$DB_NAME' successfully created with owner '$DB_OWNER'" + fi + echo + echo "Database connection DSN:" + if [ -z "$DB_PASSWORD" ]; then + DSN="postgresql://$DB_OWNER@localhost:5432/$DB_NAME?sslmode=disable" + else + DSN="postgresql://$DB_OWNER:$DB_PASSWORD@localhost:5432/$DB_NAME?sslmode=disable" + fi + echo "$DSN" +else + echo "❌ Error: Failed to $ACTION database. Please check:" + echo " - PostgreSQL is running" + echo " - User '$DB_OWNER' exists and has necessary privileges" + echo " - No active connections to database '$DB_NAME'" + exit 1 +fi + +unset PGPASSWORD \ No newline at end of file -- cgit v1.2.3