Files
dbis_core-lite/scripts/setup-test-db-docker.sh
2026-02-09 21:51:45 -08:00

136 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
# Docker-based test database setup for DBIS Core Lite
set -e
echo "🐳 DBIS Core Lite - Docker Test Database Setup"
echo "=============================================="
echo ""
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Check if Docker is available
if ! command -v docker &> /dev/null; then
echo -e "${RED}❌ Docker is not installed${NC}"
echo " Please install Docker or use manual PostgreSQL setup"
exit 1
fi
echo -e "${GREEN}✅ Docker found${NC}"
echo ""
# Check if docker-compose is available
if command -v docker-compose &> /dev/null; then
COMPOSE_CMD="docker-compose"
elif docker compose version &> /dev/null; then
COMPOSE_CMD="docker compose"
else
echo -e "${RED}❌ Docker Compose is not available${NC}"
exit 1
fi
echo -e "${GREEN}✅ Docker Compose found${NC}"
echo ""
# Start PostgreSQL container
echo "🚀 Starting PostgreSQL container..."
$COMPOSE_CMD -f docker-compose.test.yml up -d postgres-test
echo ""
echo "⏳ Waiting for PostgreSQL to be ready..."
sleep 5
# Wait for PostgreSQL to be healthy
MAX_WAIT=30
WAITED=0
while [ $WAITED -lt $MAX_WAIT ]; do
if docker exec dbis_core_test_db pg_isready -U postgres > /dev/null 2>&1; then
echo -e "${GREEN}✅ PostgreSQL is ready${NC}"
break
fi
echo -n "."
sleep 1
WAITED=$((WAITED + 1))
done
if [ $WAITED -ge $MAX_WAIT ]; then
echo -e "${RED}❌ PostgreSQL did not become ready in time${NC}"
exit 1
fi
echo ""
# Create test database
echo "📦 Creating test database..."
docker exec dbis_core_test_db psql -U postgres -c "CREATE DATABASE dbis_core_test;" 2>/dev/null || {
echo -e "${YELLOW}⚠️ Database may already exist${NC}"
}
echo -e "${GREEN}✅ Test database created${NC}"
echo ""
# Apply schema
echo "📋 Applying database schema..."
docker exec -i dbis_core_test_db psql -U postgres -d dbis_core_test < src/database/schema.sql > /dev/null 2>&1
echo -e "${GREEN}✅ Schema applied${NC}"
echo ""
# Update .env.test with Docker connection
TEST_DB_URL="postgresql://postgres:postgres@localhost:5434/dbis_core_test"
echo "📝 Updating .env.test with Docker connection..."
cat > .env.test << EOF
# Test Database Configuration (Docker)
TEST_DATABASE_URL=${TEST_DB_URL}
# Test Environment Variables
NODE_ENV=test
JWT_SECRET=test-secret-key-for-testing-only
EOF
echo -e "${GREEN}✅ .env.test updated${NC}"
echo ""
# Run migrations (if any)
echo "🔄 Running database migrations..."
export TEST_DATABASE_URL="${TEST_DB_URL}"
export DATABASE_URL="${TEST_DB_URL}"
if npm run migrate > /dev/null 2>&1; then
echo -e "${GREEN}✅ Migrations completed${NC}"
else
echo -e "${YELLOW}⚠️ Migrations completed (or none needed)${NC}"
fi
echo ""
# Verify tables
echo "🔍 Verifying database schema..."
TABLE_COUNT=$(docker exec dbis_core_test_db psql -U postgres -d dbis_core_test -t -c "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public';" 2>/dev/null | tr -d ' ')
if [ -n "$TABLE_COUNT" ] && [ "$TABLE_COUNT" -gt "0" ]; then
echo -e "${GREEN}✅ Database schema verified (${TABLE_COUNT} tables)${NC}"
else
echo -e "${YELLOW}⚠️ No tables found - please check schema${NC}"
fi
echo ""
echo -e "${GREEN}✅ Docker test database setup complete!${NC}"
echo ""
echo "📋 Connection Details:"
echo " Host: localhost"
echo " Port: 5434"
echo " Database: dbis_core_test"
echo " User: postgres"
echo " Password: postgres"
echo ""
echo "🚀 Next steps:"
echo " 1. Run tests: npm test"
echo " 2. Stop container: $COMPOSE_CMD -f docker-compose.test.yml down"
echo " 3. Start container: $COMPOSE_CMD -f docker-compose.test.yml up -d"
echo ""