#!/bin/bash set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Configuration DB_HOST="${DB_HOST:-localhost}" DB_PORT="${DB_PORT:-5432}" DB_USER="${DB_USER:-postgres}" DB_PASSWORD="${DB_PASSWORD:-postgres}" DB_NAME="attune_e2e" echo -e "${GREEN}=== Attune E2E Database Setup ===${NC}\n" # Check if PostgreSQL is running echo -e "${YELLOW}→${NC} Checking PostgreSQL connection..." if ! PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d postgres -c '\q' 2>/dev/null; then echo -e "${RED}✗${NC} Cannot connect to PostgreSQL at $DB_HOST:$DB_PORT" echo " Please ensure PostgreSQL is running and credentials are correct." exit 1 fi echo -e "${GREEN}✓${NC} PostgreSQL is running\n" # Drop existing E2E database if it exists echo -e "${YELLOW}→${NC} Checking for existing E2E database..." if PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -lqt | cut -d \| -f 1 | grep -qw $DB_NAME; then echo -e "${YELLOW}!${NC} Found existing database '$DB_NAME', dropping it..." # Force terminate all connections and drop in single transaction PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d postgres < pg_backend_pid(); DROP DATABASE IF EXISTS $DB_NAME; EOF echo -e "${GREEN}✓${NC} Dropped existing database" fi # Create E2E database echo -e "${YELLOW}→${NC} Creating E2E database '$DB_NAME'..." PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d postgres -c "CREATE DATABASE $DB_NAME;" 2>&1 if [ $? -eq 0 ]; then echo -e "${GREEN}✓${NC} Database created\n" else echo -e "${RED}✗${NC} Failed to create database" exit 1 fi # Create attune schema and set search_path echo -e "${YELLOW}→${NC} Creating attune schema..." PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME < /dev/null if [ $? -eq 0 ]; then echo -e "${GREEN}✓${NC} Runtimes seeded successfully\n" else echo -e "${RED}✗${NC} Runtime seeding failed" exit 1 fi # Verify database schema echo -e "${YELLOW}→${NC} Verifying database schema..." TABLES=$(PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -t -c "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'attune';") TABLES=$(echo $TABLES | tr -d ' ') if [ "$TABLES" -gt 0 ]; then echo -e "${GREEN}✓${NC} Found $TABLES tables in 'attune' schema" else echo -e "${RED}✗${NC} No tables found in 'attune' schema" exit 1 fi # List all tables echo -e "\n${YELLOW}Tables in attune schema:${NC}" PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "SELECT table_name FROM information_schema.tables WHERE table_schema = 'attune' ORDER BY table_name;" # Create default test user echo -e "\n${YELLOW}→${NC} Creating default test user..." # Generate a fresh password hash echo -e "${YELLOW} Generating password hash...${NC}" HASH=$(cd crates/common && cargo run --example hash_password TestPass123! 2>/dev/null | tail -1) if [ -z "$HASH" ]; then echo -e "${RED}✗${NC} Failed to generate password hash" exit 1 fi PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME <