Add interactive setup script with PostgreSQL user/database creation and uninstall script

This commit is contained in:
Paul Trowbridge 2026-03-28 00:59:41 -04:00
parent 3e2d56991c
commit 7abecc99ea
2 changed files with 162 additions and 20 deletions

View File

@ -10,53 +10,110 @@ echo "🚀 Dataflow Setup"
echo "=================" echo "================="
echo "" echo ""
# Check if .env exists # Load .env if it exists
if [ ! -f .env ]; then if [ -f .env ]; then
echo "📝 Creating .env from template..." export $(cat .env | grep -v '^#' | xargs)
cp .env.example .env
echo "⚠️ Please edit .env with your database credentials"
echo ""
read -p "Press enter to continue after editing .env..."
fi fi
# Load .env # Prompt for admin credentials
export $(cat .env | grep -v '^#' | xargs) 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 # Install dependencies
echo ""
echo "📦 Installing Node.js dependencies..." echo "📦 Installing Node.js dependencies..."
npm install npm install
echo "" echo ""
# Test database connection # Test admin connection
echo "🔍 Testing database connection..." echo "🔍 Testing PostgreSQL admin connection..."
if psql -U "$DB_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c '\q' 2>/dev/null; then 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" echo "✓ PostgreSQL connection successful"
else else
echo "✗ Cannot connect to PostgreSQL" echo "✗ Cannot connect to PostgreSQL"
echo " Please check your database credentials in .env" echo " Please check your admin credentials"
exit 1 exit 1
fi 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 # Create database if it doesn't exist
echo "" echo ""
echo "🗄️ Checking database..." echo "🗄️ Creating database..."
if psql -U "$DB_USER" -h "$DB_HOST" -p "$DB_PORT" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then 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" echo "✓ Database '$DB_NAME' exists"
else else
echo "Creating database '$DB_NAME'..." psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c "CREATE DATABASE $DB_NAME OWNER $APP_USER;"
psql -U "$DB_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c "CREATE DATABASE $DB_NAME;" echo "✓ Database '$DB_NAME' created"
echo "✓ Database created"
fi 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 # Deploy schema
echo "" echo ""
echo "📋 Deploying database schema..." 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 "✓ Schema deployed"
echo "" echo ""
echo "⚙️ Deploying database functions..." 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 "✓ Functions deployed"
echo "" echo ""

85
uninstall.sh Executable file
View File

@ -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 ""