- Replace old DDL structure with new numbered files (01-24) - Update migrations and seeds for new schema - Clean up deprecated files Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
162 lines
5.1 KiB
Bash
Executable File
162 lines
5.1 KiB
Bash
Executable File
#!/bin/bash
|
|
# ============================================================================
|
|
# ERP GENERIC - CREATE TEST DATABASE SCRIPT
|
|
# ============================================================================
|
|
# Description: Creates a test database for integration tests
|
|
# Usage: ./scripts/create-test-database.sh
|
|
# ============================================================================
|
|
|
|
set -e
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Script directory
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
DATABASE_DIR="$(dirname "$SCRIPT_DIR")"
|
|
DDL_DIR="$DATABASE_DIR/ddl"
|
|
|
|
# Load environment variables
|
|
if [ -f "$DATABASE_DIR/.env" ]; then
|
|
source "$DATABASE_DIR/.env"
|
|
fi
|
|
|
|
# Test database configuration (separate from main DB)
|
|
POSTGRES_HOST="${POSTGRES_HOST:-localhost}"
|
|
POSTGRES_PORT="${POSTGRES_PORT:-5432}"
|
|
POSTGRES_DB="${TEST_DB_NAME:-erp_generic_test}"
|
|
POSTGRES_USER="${POSTGRES_USER:-erp_admin}"
|
|
POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-erp_secret_2024}"
|
|
|
|
# Connection string
|
|
export PGPASSWORD="$POSTGRES_PASSWORD"
|
|
PSQL_CMD="psql -h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER"
|
|
|
|
echo -e "${BLUE}============================================${NC}"
|
|
echo -e "${BLUE} ERP GENERIC - TEST DATABASE CREATION${NC}"
|
|
echo -e "${BLUE}============================================${NC}"
|
|
echo ""
|
|
echo -e "Host: ${GREEN}$POSTGRES_HOST:$POSTGRES_PORT${NC}"
|
|
echo -e "Database: ${GREEN}$POSTGRES_DB${NC}"
|
|
echo -e "User: ${GREEN}$POSTGRES_USER${NC}"
|
|
echo ""
|
|
|
|
# Check if PostgreSQL is reachable
|
|
echo -e "${BLUE}[1/5] Checking PostgreSQL connection...${NC}"
|
|
if ! $PSQL_CMD -d postgres -c "SELECT 1" > /dev/null 2>&1; then
|
|
echo -e "${RED}Error: Cannot connect to PostgreSQL${NC}"
|
|
exit 1
|
|
fi
|
|
echo -e "${GREEN}PostgreSQL is reachable!${NC}"
|
|
|
|
# Drop test database if exists
|
|
echo -e "${BLUE}[2/5] Dropping existing test database if exists...${NC}"
|
|
$PSQL_CMD -d postgres -c "DROP DATABASE IF EXISTS $POSTGRES_DB;" 2>/dev/null || true
|
|
echo -e "${GREEN}Old test database dropped (if existed)${NC}"
|
|
|
|
# Create test database
|
|
echo -e "${BLUE}[3/5] Creating test database...${NC}"
|
|
$PSQL_CMD -d postgres -c "CREATE DATABASE $POSTGRES_DB WITH ENCODING='UTF8' LC_COLLATE='en_US.UTF-8' LC_CTYPE='en_US.UTF-8' TEMPLATE=template0;" 2>/dev/null || \
|
|
$PSQL_CMD -d postgres -c "CREATE DATABASE $POSTGRES_DB;"
|
|
echo -e "${GREEN}Test database '$POSTGRES_DB' created!${NC}"
|
|
|
|
# Execute DDL files in order
|
|
echo -e "${BLUE}[4/5] Executing DDL files...${NC}"
|
|
|
|
DDL_FILES=(
|
|
"00-prerequisites.sql"
|
|
"01-auth.sql"
|
|
"01-auth-extensions.sql"
|
|
"01-auth-mfa-email-verification.sql"
|
|
"02-core.sql"
|
|
"02-core-extensions.sql"
|
|
"03-analytics.sql"
|
|
"04-financial.sql"
|
|
"05-inventory.sql"
|
|
"05-inventory-extensions.sql"
|
|
"06-purchase.sql"
|
|
"07-sales.sql"
|
|
"08-projects.sql"
|
|
"09-system.sql"
|
|
"09-system-extensions.sql"
|
|
"10-billing.sql"
|
|
"11-crm.sql"
|
|
"12-hr.sql"
|
|
"13-audit.sql"
|
|
"14-reports.sql"
|
|
# MGN-020, MGN-021, MGN-022 - AI Agents, Messaging, Integrations
|
|
"15-ai-agents.sql"
|
|
"16-messaging.sql"
|
|
"17-integrations.sql"
|
|
# MGN-018, MGN-019 - Webhooks, Feature Flags (2026-01-13)
|
|
"19-webhooks.sql"
|
|
"20-feature-flags.sql"
|
|
)
|
|
|
|
TOTAL=${#DDL_FILES[@]}
|
|
CURRENT=0
|
|
|
|
for ddl_file in "${DDL_FILES[@]}"; do
|
|
CURRENT=$((CURRENT + 1))
|
|
filepath="$DDL_DIR/$ddl_file"
|
|
|
|
if [ -f "$filepath" ]; then
|
|
if $PSQL_CMD -d $POSTGRES_DB -f "$filepath" > /dev/null 2>&1; then
|
|
echo -e " [${CURRENT}/${TOTAL}] ${GREEN}✓${NC} $ddl_file"
|
|
else
|
|
echo -e " [${CURRENT}/${TOTAL}] ${RED}✗${NC} $ddl_file"
|
|
exit 1
|
|
fi
|
|
else
|
|
echo -e " [${CURRENT}/${TOTAL}] ${YELLOW}⊘${NC} $ddl_file (not found, skipping)"
|
|
fi
|
|
done
|
|
|
|
# Load test fixtures
|
|
echo -e "${BLUE}[5/5] Loading test fixtures...${NC}"
|
|
FIXTURES_FILE="$DATABASE_DIR/seeds/test/fixtures.sql"
|
|
if [ -f "$FIXTURES_FILE" ]; then
|
|
if $PSQL_CMD -d $POSTGRES_DB -f "$FIXTURES_FILE" > /dev/null 2>&1; then
|
|
echo -e " ${GREEN}✓${NC} Test fixtures loaded"
|
|
else
|
|
echo -e " ${RED}✗${NC} Error loading fixtures"
|
|
exit 1
|
|
fi
|
|
else
|
|
echo -e " ${YELLOW}⊘${NC} No fixtures file found (seeds/test/fixtures.sql)"
|
|
fi
|
|
|
|
echo ""
|
|
echo -e "${GREEN}============================================${NC}"
|
|
echo -e "${GREEN} TEST DATABASE CREATED SUCCESSFULLY!${NC}"
|
|
echo -e "${GREEN}============================================${NC}"
|
|
echo ""
|
|
echo -e "Connection string:"
|
|
echo -e "${BLUE}postgresql://$POSTGRES_USER:****@$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB${NC}"
|
|
echo ""
|
|
|
|
# Show statistics
|
|
echo -e "${BLUE}Database Statistics:${NC}"
|
|
$PSQL_CMD -d $POSTGRES_DB -c "
|
|
SELECT
|
|
schemaname AS schema,
|
|
COUNT(*) AS tables
|
|
FROM pg_tables
|
|
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
|
|
GROUP BY schemaname
|
|
ORDER BY schemaname;
|
|
"
|
|
|
|
echo ""
|
|
echo -e "${YELLOW}Environment variables for tests:${NC}"
|
|
echo " export TEST_DB_HOST=$POSTGRES_HOST"
|
|
echo " export TEST_DB_PORT=$POSTGRES_PORT"
|
|
echo " export TEST_DB_NAME=$POSTGRES_DB"
|
|
echo " export TEST_DB_USER=$POSTGRES_USER"
|
|
echo " export TEST_DB_PASSWORD=<your_password>"
|
|
echo ""
|