Re_Backend/scripts/fresh-database-setup.bat

137 lines
4.1 KiB
Batchfile

@echo off
REM ############################################################################
REM Fresh Database Setup Script (Windows)
REM
REM Purpose: Complete fresh database setup for Royal Enfield Workflow System
REM
REM Prerequisites:
REM 1. PostgreSQL 16.x installed
REM 2. Redis installed and running
REM 3. Node.js 18+ installed
REM 4. Environment variables configured in .env
REM
REM Usage: scripts\fresh-database-setup.bat
REM ############################################################################
setlocal enabledelayedexpansion
echo.
echo ===============================================================
echo Royal Enfield Workflow System - Fresh Database Setup
echo ===============================================================
echo.
REM Load .env file
if exist .env (
echo [*] Loading environment variables...
for /f "usebackq tokens=1,2 delims==" %%a in (".env") do (
set "%%a=%%b"
)
) else (
echo [ERROR] .env file not found!
echo Please copy env.example to .env and configure it
pause
exit /b 1
)
REM Set default values if not in .env
if not defined DB_NAME set DB_NAME=royal_enfield_workflow
if not defined DB_USER set DB_USER=postgres
if not defined DB_HOST set DB_HOST=localhost
if not defined DB_PORT set DB_PORT=5432
echo.
echo WARNING: This will DROP the existing database!
echo Database: %DB_NAME%
echo Host: %DB_HOST%:%DB_PORT%
echo.
set /p CONFIRM="Are you sure you want to continue? (yes/no): "
if /i not "%CONFIRM%"=="yes" (
echo Setup cancelled.
exit /b 0
)
echo.
echo ===============================================================
echo Step 1: Dropping existing database (if exists)...
echo ===============================================================
echo.
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d postgres -c "DROP DATABASE IF EXISTS %DB_NAME%;" 2>nul
echo [OK] Old database dropped
echo.
echo ===============================================================
echo Step 2: Creating fresh database...
echo ===============================================================
echo.
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d postgres -c "CREATE DATABASE %DB_NAME% OWNER %DB_USER%;"
echo [OK] Fresh database created: %DB_NAME%
echo.
echo ===============================================================
echo Step 3: Installing PostgreSQL extensions...
echo ===============================================================
echo.
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -c "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";"
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -c "CREATE EXTENSION IF NOT EXISTS \"pg_trgm\";"
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -c "CREATE EXTENSION IF NOT EXISTS \"btree_gin\";"
echo [OK] PostgreSQL extensions installed
echo.
echo ===============================================================
echo Step 4: Running database migrations...
echo ===============================================================
echo.
call npm run migrate
echo [OK] All migrations completed
echo.
echo ===============================================================
echo Step 5: Seeding admin configuration...
echo ===============================================================
echo.
call npm run seed:config
echo [OK] Admin configuration seeded
echo.
echo ===============================================================
echo Step 6: Database verification...
echo ===============================================================
echo.
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -c "SELECT tablename FROM pg_tables WHERE schemaname = 'public' ORDER BY tablename;"
echo [OK] Database structure verified
echo.
echo ===============================================================
echo FRESH DATABASE SETUP COMPLETE!
echo ===============================================================
echo.
echo Next Steps:
echo 1. Assign admin role to your user:
echo psql -d %DB_NAME% -f scripts\assign-admin-user.sql
echo.
echo 2. Start the backend server:
echo npm run dev
echo.
echo 3. Access the application:
echo http://localhost:5000
echo.
echo Database is ready for production use!
echo.
pause