From 7abecc99ea7cf0ddd7205f726b01429b75887544 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Sat, 28 Mar 2026 00:59:41 -0400 Subject: [PATCH] Add interactive setup script with PostgreSQL user/database creation and uninstall script --- setup.sh | 97 +++++++++++++++++++++++++++++++++++++++++----------- uninstall.sh | 85 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 20 deletions(-) create mode 100755 uninstall.sh diff --git a/setup.sh b/setup.sh index e72d08d..7ef008e 100755 --- a/setup.sh +++ b/setup.sh @@ -10,53 +10,110 @@ echo "🚀 Dataflow Setup" echo "=================" echo "" -# Check if .env exists -if [ ! -f .env ]; then - echo "📝 Creating .env from template..." - cp .env.example .env - echo "⚠️ Please edit .env with your database credentials" - echo "" - read -p "Press enter to continue after editing .env..." +# Load .env if it exists +if [ -f .env ]; then + export $(cat .env | grep -v '^#' | xargs) fi -# Load .env -export $(cat .env | grep -v '^#' | xargs) +# Prompt for admin credentials +echo "📋 PostgreSQL Admin Credentials" +echo " (Used to create user and database)" +echo "" +read -p "Admin username [postgres]: " ADMIN_USER +ADMIN_USER=${ADMIN_USER:-postgres} +read -s -p "Admin password: " ADMIN_PASS +echo "" +echo "" + +# Prompt for app user and database +echo "📋 Application Credentials" +echo "" +read -p "App username [dataflow]: " APP_USER +APP_USER=${APP_USER:-dataflow} +read -s -p "App password: " APP_PASS +echo "" +read -p "Database name [dataflow]: " DB_NAME +DB_NAME=${DB_NAME:-dataflow} +read -p "Database host [localhost]: " DB_HOST +DB_HOST=${DB_HOST:-localhost} +read -p "Database port [5432]: " DB_PORT +DB_PORT=${DB_PORT:-5432} +echo "" + +# Prompt for API configuration +echo "📋 API Configuration" +echo "" +read -p "API port [3000]: " API_PORT +API_PORT=${API_PORT:-3000} +read -p "Node environment [development]: " NODE_ENV +NODE_ENV=${NODE_ENV:-development} +echo "" # Install dependencies +echo "" echo "📦 Installing Node.js dependencies..." npm install echo "" -# Test database connection -echo "🔍 Testing database connection..." -if psql -U "$DB_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c '\q' 2>/dev/null; then +# Test admin connection +echo "🔍 Testing PostgreSQL admin connection..." +export PGPASSWORD="$ADMIN_PASS" +if psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c '\q' 2>/dev/null; then echo "✓ PostgreSQL connection successful" else echo "✗ Cannot connect to PostgreSQL" - echo " Please check your database credentials in .env" + echo " Please check your admin credentials" exit 1 fi +# Create user if it doesn't exist +echo "" +echo "👤 Creating PostgreSQL user..." +if psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -tc "SELECT 1 FROM pg_roles WHERE rolname='$APP_USER'" | grep -q 1; then + echo "✓ User '$APP_USER' already exists" +else + psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c "CREATE USER $APP_USER WITH PASSWORD '$APP_PASS';" + echo "✓ User '$APP_USER' created" +fi + # Create database if it doesn't exist echo "" -echo "🗄️ Checking database..." -if psql -U "$DB_USER" -h "$DB_HOST" -p "$DB_PORT" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then +echo "🗄️ Creating database..." +if psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then echo "✓ Database '$DB_NAME' exists" else - echo "Creating database '$DB_NAME'..." - psql -U "$DB_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c "CREATE DATABASE $DB_NAME;" - echo "✓ Database created" + psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c "CREATE DATABASE $DB_NAME OWNER $APP_USER;" + echo "✓ Database '$DB_NAME' created" fi +unset PGPASSWORD + +# Save credentials to .env +echo "" +echo "💾 Saving credentials to .env..." +cat > .env << EOF +# Database Configuration +DB_HOST=$DB_HOST +DB_PORT=$DB_PORT +DB_NAME=$DB_NAME +DB_USER=$APP_USER +DB_PASSWORD=$APP_PASS + +# API Configuration +API_PORT=$API_PORT +NODE_ENV=$NODE_ENV +EOF +echo "✓ Credentials saved" + # Deploy schema echo "" echo "📋 Deploying database schema..." -psql -U "$DB_USER" -h "$DB_HOST" -p "$DB_PORT" -d "$DB_NAME" -f database/schema.sql +psql -U "$APP_USER" -h "$DB_HOST" -p "$DB_PORT" -d "$DB_NAME" -f database/schema.sql echo "✓ Schema deployed" echo "" echo "⚙️ Deploying database functions..." -psql -U "$DB_USER" -h "$DB_HOST" -p "$DB_PORT" -d "$DB_NAME" -f database/functions.sql +psql -U "$APP_USER" -h "$DB_HOST" -p "$DB_PORT" -d "$DB_NAME" -f database/functions.sql echo "✓ Functions deployed" echo "" diff --git a/uninstall.sh b/uninstall.sh new file mode 100755 index 0000000..3c38e39 --- /dev/null +++ b/uninstall.sh @@ -0,0 +1,85 @@ +#!/bin/bash +# +# Dataflow Uninstall Script +# Removes database user, database, and optionally .env +# + +echo "⚠️ Dataflow Uninstall" +echo "=====================" +echo "" + +# Load .env if it exists +if [ -f .env ]; then + export $(cat .env | grep -v '^#' | xargs) +fi + +DB_NAME=${DB_NAME:-dataflow} +DB_USER=${DB_USER:-dataflow} + +echo "⚠️ This will permanently delete:" +echo " - Database: $DB_NAME" +echo " - User: $DB_USER" +echo "" +read -p "Type 'delete' to confirm: " CONFIRM +if [ "$CONFIRM" != "delete" ]; then + echo "Cancelled." + exit 0 +fi + +# Prompt for admin credentials +echo "" +echo "📋 PostgreSQL Admin Credentials" +echo "" +read -p "Admin username [postgres]: " ADMIN_USER +ADMIN_USER=${ADMIN_USER:-postgres} +read -s -p "Admin password: " ADMIN_PASS +echo "" + +DB_HOST=${DB_HOST:-localhost} +DB_PORT=${DB_PORT:-5432} +DB_NAME=${DB_NAME:-dataflow} +DB_USER=${DB_USER:-dataflow} + +# Test admin connection +echo "" +echo "🔍 Testing PostgreSQL admin connection..." +export PGPASSWORD="$ADMIN_PASS" +if ! psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c '\q' 2>/dev/null; then + echo "✗ Cannot connect to PostgreSQL" + exit 1 +fi +echo "✓ Connected" + +# Drop database +echo "" +echo "🗄️ Dropping database..." +psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c "DROP DATABASE IF EXISTS $DB_NAME;" 2>/dev/null || true +echo "✓ Database dropped" + +# Drop user +echo "" +echo "👤 Dropping user..." +psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c "DROP USER IF EXISTS $DB_USER;" 2>/dev/null || true +echo "✓ User dropped" + +unset PGPASSWORD + +# Optionally remove .env +echo "" +read -p "Remove .env file? [y/N]: " REMOVE_ENV +if [ "$REMOVE_ENV" == "y" ] || [ "$REMOVE_ENV" == "Y" ]; then + rm -f .env + echo "✓ .env removed" +fi + +# Optionally remove node_modules +echo "" +read -p "Remove node_modules? [y/N]: " REMOVE_MODULES +if [ "$REMOVE_MODULES" == "y" ] || [ "$REMOVE_MODULES" == "Y" ]; then + rm -rf node_modules + echo "✓ node_modules removed" +fi + +echo "" +echo "✅ Uninstall complete!" +echo ""