#!/bin/bash ############################################################################### # Fresh Database Setup Script # # Purpose: Complete fresh database setup for Royal Enfield Workflow System # # Prerequisites: # 1. PostgreSQL 16.x installed # 2. Redis installed and running # 3. Node.js 18+ installed # 4. Environment variables configured in .env # # Usage: # chmod +x scripts/fresh-database-setup.sh # ./scripts/fresh-database-setup.sh # # What this script does: # 1. Drops existing database (if exists) # 2. Creates fresh database # 3. Runs all migrations in order # 4. Seeds admin configuration # 5. Creates initial admin user # 6. Verifies setup ############################################################################### set -e # Exit on error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Load environment variables if [ -f .env ]; then echo -e "${BLUE}📋 Loading environment variables...${NC}" export $(cat .env | grep -v '^#' | xargs) else echo -e "${RED}❌ .env file not found!${NC}" echo -e "${YELLOW}Please copy env.example to .env and configure it${NC}" exit 1 fi # Database variables DB_NAME="${DB_NAME:-royal_enfield_workflow}" DB_USER="${DB_USER:-postgres}" DB_HOST="${DB_HOST:-localhost}" DB_PORT="${DB_PORT:-5432}" echo -e "${BLUE}╔═══════════════════════════════════════════════════════════════╗${NC}" echo -e "${BLUE}║ Royal Enfield Workflow System - Fresh Database Setup ║${NC}" echo -e "${BLUE}╚═══════════════════════════════════════════════════════════════╝${NC}" echo "" echo -e "${YELLOW}⚠️ WARNING: This will DROP the existing database!${NC}" echo -e "${YELLOW} Database: ${DB_NAME}${NC}" echo -e "${YELLOW} Host: ${DB_HOST}:${DB_PORT}${NC}" echo "" read -p "Are you sure you want to continue? (yes/no): " -r echo "" if [[ ! $REPLY =~ ^[Yy]es$ ]]; then echo -e "${YELLOW}Setup cancelled.${NC}" exit 0 fi echo -e "${BLUE}════════════════════════════════════════════════════════════════${NC}" echo -e "${BLUE}Step 1: Dropping existing database (if exists)...${NC}" echo -e "${BLUE}════════════════════════════════════════════════════════════════${NC}" psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d postgres -c "DROP DATABASE IF EXISTS $DB_NAME;" || true echo -e "${GREEN}✅ Old database dropped${NC}" echo "" echo -e "${BLUE}════════════════════════════════════════════════════════════════${NC}" echo -e "${BLUE}Step 2: Creating fresh database...${NC}" echo -e "${BLUE}════════════════════════════════════════════════════════════════${NC}" psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d postgres -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;" echo -e "${GREEN}✅ Fresh database created: $DB_NAME${NC}" echo "" echo -e "${BLUE}════════════════════════════════════════════════════════════════${NC}" echo -e "${BLUE}Step 3: Installing PostgreSQL extensions...${NC}" echo -e "${BLUE}════════════════════════════════════════════════════════════════${NC}" psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME <