129 lines
4.6 KiB
Bash
Executable File
129 lines
4.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Script to set up test database for DBIS Core Lite
|
|
|
|
set -e
|
|
|
|
echo "🔧 Setting up test database for DBIS Core Lite"
|
|
echo "================================================"
|
|
echo ""
|
|
|
|
# Colors
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
RED='\033[0;31m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Default values
|
|
DB_USER="${POSTGRES_USER:-postgres}"
|
|
DB_PASSWORD="${POSTGRES_PASSWORD:-postgres}"
|
|
DB_HOST="${POSTGRES_HOST:-localhost}"
|
|
DB_PORT="${POSTGRES_PORT:-5432}"
|
|
TEST_DB_NAME="dbis_core_test"
|
|
|
|
# Test database URL
|
|
TEST_DATABASE_URL="postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${TEST_DB_NAME}"
|
|
|
|
echo "📋 Configuration:"
|
|
echo " Database: ${TEST_DB_NAME}"
|
|
echo " User: ${DB_USER}"
|
|
echo " Host: ${DB_HOST}:${DB_PORT}"
|
|
echo ""
|
|
|
|
# Check if PostgreSQL is accessible
|
|
echo "🔍 Checking PostgreSQL connection..."
|
|
if ! PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d postgres -c "SELECT 1" > /dev/null 2>&1; then
|
|
echo -e "${RED}❌ Cannot connect to PostgreSQL${NC}"
|
|
echo " Please ensure PostgreSQL is running and credentials are correct"
|
|
exit 1
|
|
fi
|
|
echo -e "${GREEN}✅ PostgreSQL connection successful${NC}"
|
|
echo ""
|
|
|
|
# Check if test database exists
|
|
echo "🔍 Checking if test database exists..."
|
|
if PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -lqt 2>/dev/null | cut -d \| -f 1 | grep -qw "${TEST_DB_NAME}"; then
|
|
echo -e "${YELLOW}⚠️ Test database '${TEST_DB_NAME}' already exists${NC}"
|
|
read -p "Do you want to drop and recreate it? (y/N): " -n 1 -r
|
|
echo
|
|
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
echo "🗑️ Dropping existing test database..."
|
|
PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d postgres -c "DROP DATABASE IF EXISTS ${TEST_DB_NAME};" > /dev/null 2>&1
|
|
echo -e "${GREEN}✅ Database dropped${NC}"
|
|
else
|
|
echo "⏭️ Keeping existing database"
|
|
fi
|
|
fi
|
|
|
|
# Create test database if it doesn't exist
|
|
if ! PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -lqt 2>/dev/null | cut -d \| -f 1 | grep -qw "${TEST_DB_NAME}"; then
|
|
echo "📦 Creating test database '${TEST_DB_NAME}'..."
|
|
PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d postgres -c "CREATE DATABASE ${TEST_DB_NAME};" > /dev/null 2>&1
|
|
echo -e "${GREEN}✅ Test database created${NC}"
|
|
else
|
|
echo -e "${GREEN}✅ Test database already exists${NC}"
|
|
fi
|
|
echo ""
|
|
|
|
# Run migrations
|
|
echo "🔄 Running database migrations..."
|
|
export DATABASE_URL="${TEST_DATABASE_URL}"
|
|
if npm run migrate > /dev/null 2>&1; then
|
|
echo -e "${GREEN}✅ Migrations completed successfully${NC}"
|
|
else
|
|
echo -e "${YELLOW}⚠️ Migrations may have failed or already applied${NC}"
|
|
echo " Checking database schema..."
|
|
fi
|
|
echo ""
|
|
|
|
# Verify tables exist
|
|
echo "🔍 Verifying database schema..."
|
|
TABLES=$(PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${TEST_DB_NAME}" -t -c "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public';" 2>/dev/null | tr -d ' ')
|
|
if [ -n "$TABLES" ] && [ "$TABLES" -gt 0 ]; then
|
|
echo -e "${GREEN}✅ Database schema verified (${TABLES} tables found)${NC}"
|
|
|
|
# List tables
|
|
echo ""
|
|
echo "📊 Tables in test database:"
|
|
PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${TEST_DB_NAME}" -c "\dt" 2>/dev/null || echo " (Unable to list tables)"
|
|
else
|
|
echo -e "${RED}❌ No tables found in test database${NC}"
|
|
echo " Please check migrations"
|
|
exit 1
|
|
fi
|
|
echo ""
|
|
|
|
# Set environment variable in .env.test if it exists, or create it
|
|
ENV_FILE=".env.test"
|
|
if [ -f "$ENV_FILE" ]; then
|
|
echo "📝 Updating ${ENV_FILE}..."
|
|
if grep -q "TEST_DATABASE_URL" "$ENV_FILE"; then
|
|
sed -i "s|^TEST_DATABASE_URL=.*|TEST_DATABASE_URL=${TEST_DATABASE_URL}|" "$ENV_FILE"
|
|
else
|
|
echo "TEST_DATABASE_URL=${TEST_DATABASE_URL}" >> "$ENV_FILE"
|
|
fi
|
|
echo -e "${GREEN}✅ ${ENV_FILE} updated${NC}"
|
|
else
|
|
echo "📝 Creating ${ENV_FILE}..."
|
|
cat > "$ENV_FILE" << EOF
|
|
# Test Database Configuration
|
|
TEST_DATABASE_URL=${TEST_DATABASE_URL}
|
|
|
|
# Test Environment
|
|
NODE_ENV=test
|
|
JWT_SECRET=test-secret-key-for-testing-only
|
|
EOF
|
|
echo -e "${GREEN}✅ ${ENV_FILE} created${NC}"
|
|
fi
|
|
echo ""
|
|
|
|
echo -e "${GREEN}✅ Test database setup complete!${NC}"
|
|
echo ""
|
|
echo "📋 Next steps:"
|
|
echo " 1. Run tests with: npm test"
|
|
echo " 2. Or run specific test suite: npm test -- tests/unit"
|
|
echo ""
|
|
echo "💡 Tip: The TEST_DATABASE_URL is set in ${ENV_FILE}"
|
|
echo " Make sure to load it in your test environment"
|
|
|