#!/bin/bash # ================================================================================================ # Migration Test Suite Runner # PostgreSQL 16.10 + TimescaleDB 2.22.1 # Runs all migration tests in sequence and reports results # ================================================================================================ 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 # Database connection (override with environment variables) DB_HOST="${DB_HOST:-localhost}" DB_PORT="${DB_PORT:-5432}" DB_NAME="${DB_NAME:-foxhunt_trading}" DB_USER="${DB_USER:-foxhunt_admin}" PSQL_CMD="psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME" # Test result tracking TOTAL_TESTS=0 PASSED_TESTS=0 FAILED_TESTS=0 WARNINGS=0 echo -e "${BLUE}================================================================================================${NC}" echo -e "${BLUE}Migration Test Suite - PostgreSQL 16.10 + TimescaleDB 2.22.1${NC}" echo -e "${BLUE}================================================================================================${NC}" echo "" echo -e "Database: ${YELLOW}$DB_NAME${NC} on ${YELLOW}$DB_HOST:$DB_PORT${NC}" echo -e "User: ${YELLOW}$DB_USER${NC}" echo "" # Function to run a test file and parse results run_test() { local test_file=$1 local test_name=$(basename "$test_file" .sql) echo -e "${BLUE}Running: ${YELLOW}$test_name${NC}" echo "-----------------------------------" # Run test and capture output OUTPUT=$($PSQL_CMD -f "$test_file" 2>&1 || true) # Count PASS/FAIL/WARNING messages local pass_count=$(echo "$OUTPUT" | grep -c "PASS:" || true) local fail_count=$(echo "$OUTPUT" | grep -c "FAIL:" || true) local warn_count=$(echo "$OUTPUT" | grep -c "WARNING:" || true) TOTAL_TESTS=$((TOTAL_TESTS + pass_count + fail_count)) PASSED_TESTS=$((PASSED_TESTS + pass_count)) FAILED_TESTS=$((FAILED_TESTS + fail_count)) WARNINGS=$((WARNINGS + warn_count)) # Display results if [ $fail_count -gt 0 ]; then echo -e "${RED}✗ FAILED${NC} - $fail_count test(s) failed" echo "$OUTPUT" | grep "FAIL:" | sed "s/^/${RED} /${NC}" else echo -e "${GREEN}✓ PASSED${NC} - $pass_count test(s) passed" fi if [ $warn_count -gt 0 ]; then echo -e "${YELLOW}⚠ WARNINGS${NC} - $warn_count warning(s)" echo "$OUTPUT" | grep "WARNING:" | sed "s/^/${YELLOW} /${NC}" fi # Show NOTICE messages for additional info echo "$OUTPUT" | grep "NOTICE:" | sed "s/NOTICE:/ ℹ /" || true echo "" } # Check database connectivity echo -e "${BLUE}Checking database connectivity...${NC}" if $PSQL_CMD -c "SELECT version();" > /dev/null 2>&1; then echo -e "${GREEN}✓ Connected successfully${NC}" # Check PostgreSQL version PG_VERSION=$($PSQL_CMD -t -c "SELECT version();" | head -1) echo -e " PostgreSQL: ${YELLOW}$PG_VERSION${NC}" # Check TimescaleDB version TSDB_VERSION=$($PSQL_CMD -t -c "SELECT extversion FROM pg_extension WHERE extname = 'timescaledb';" 2>/dev/null || echo "Not installed") if [ "$TSDB_VERSION" != "Not installed" ]; then echo -e " TimescaleDB: ${YELLOW}$TSDB_VERSION${NC}" else echo -e "${RED} ⚠ TimescaleDB not detected${NC}" fi else echo -e "${RED}✗ Failed to connect to database${NC}" echo "Please check connection settings and try again." exit 1 fi echo "" # Run test suites in order echo -e "${BLUE}================================================================================================${NC}" echo -e "${BLUE}Running Test Suites${NC}" echo -e "${BLUE}================================================================================================${NC}" echo "" # Test 1: Trading Events (Migration 001) if [ -f "migrations/tests/test_trading_events.sql" ]; then run_test "migrations/tests/test_trading_events.sql" fi # Test 2: Risk Events (Migrations 002-003) if [ -f "migrations/tests/test_risk_events.sql" ]; then run_test "migrations/tests/test_risk_events.sql" fi # Test 3: Compliance Views (Migration 004) if [ -f "migrations/tests/test_compliance_views.sql" ]; then run_test "migrations/tests/test_compliance_views.sql" fi # Test 4: Configuration Schema (Migration 007) if [ -f "migrations/tests/test_configuration_schema.sql" ]; then run_test "migrations/tests/test_configuration_schema.sql" fi # Test 5: Auth Schema (Migration 015) if [ -f "migrations/tests/test_auth_schema.sql" ]; then run_test "migrations/tests/test_auth_schema.sql" fi # Test 6: TimescaleDB Features if [ -f "migrations/tests/test_timescaledb_features.sql" ]; then run_test "migrations/tests/test_timescaledb_features.sql" fi # Test 7: Schema Validation if [ -f "migrations/tests/test_schema_validation.sql" ]; then run_test "migrations/tests/test_schema_validation.sql" fi # Summary echo -e "${BLUE}================================================================================================${NC}" echo -e "${BLUE}Test Summary${NC}" echo -e "${BLUE}================================================================================================${NC}" echo "" echo -e "Total Tests: ${YELLOW}$TOTAL_TESTS${NC}" echo -e "Passed: ${GREEN}$PASSED_TESTS${NC}" echo -e "Failed: ${RED}$FAILED_TESTS${NC}" echo -e "Warnings: ${YELLOW}$WARNINGS${NC}" echo "" # Calculate success rate if [ $TOTAL_TESTS -gt 0 ]; then SUCCESS_RATE=$(awk "BEGIN {printf \"%.1f\", ($PASSED_TESTS / $TOTAL_TESTS) * 100}") echo -e "Success Rate: ${YELLOW}$SUCCESS_RATE%${NC}" echo "" if [ $FAILED_TESTS -eq 0 ]; then echo -e "${GREEN}✓ All tests passed!${NC}" exit 0 else echo -e "${RED}✗ Some tests failed. Review output above for details.${NC}" exit 1 fi else echo -e "${YELLOW}⚠ No tests were run${NC}" exit 1 fi