|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Test database connection script |
| 4 | +Tests if we can connect to Supabase PostgreSQL database |
| 5 | +""" |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import psycopg2 |
| 9 | +from psycopg2 import Error |
| 10 | + |
| 11 | +# Database configuration |
| 12 | +DB_CONFIG = { |
| 13 | + 'host': os.getenv('DB_HOST', 'localhost'), |
| 14 | + 'user': os.getenv('DB_USER', 'postgres'), |
| 15 | + 'password': os.getenv('DB_PASSWORD'), |
| 16 | + 'database': os.getenv('DB_NAME', 'postgres'), |
| 17 | + 'port': int(os.getenv('DB_PORT', '5432')) |
| 18 | +} |
| 19 | + |
| 20 | +def test_connection(): |
| 21 | + """Test database connection""" |
| 22 | + print("=" * 60) |
| 23 | + print("Testing Supabase PostgreSQL Connection") |
| 24 | + print("=" * 60) |
| 25 | + |
| 26 | + # Check environment variables |
| 27 | + print("\n📋 Environment Variables:") |
| 28 | + print(f" DB_HOST: {DB_CONFIG['host']}") |
| 29 | + print(f" DB_PORT: {DB_CONFIG['port']}") |
| 30 | + print(f" DB_USER: {DB_CONFIG['user']}") |
| 31 | + print(f" DB_NAME: {DB_CONFIG['database']}") |
| 32 | + print(f" DB_PASSWORD: {'*' * len(DB_CONFIG['password']) if DB_CONFIG['password'] else 'NOT SET'}") |
| 33 | + |
| 34 | + if not DB_CONFIG['password']: |
| 35 | + print("\n❌ ERROR: DB_PASSWORD environment variable is not set!") |
| 36 | + print(" Set it with: export DB_PASSWORD='your-password'") |
| 37 | + return False |
| 38 | + |
| 39 | + # Test connection |
| 40 | + print("\n🔌 Attempting to connect...") |
| 41 | + try: |
| 42 | + # Add SSL mode for Supabase connections |
| 43 | + connection = psycopg2.connect(**DB_CONFIG, sslmode='require') |
| 44 | + print("✅ Connection successful!") |
| 45 | + |
| 46 | + # Test a simple query |
| 47 | + cursor = connection.cursor() |
| 48 | + cursor.execute("SELECT version();") |
| 49 | + version = cursor.fetchone() |
| 50 | + print(f"✅ PostgreSQL version: {version[0][:50]}...") |
| 51 | + |
| 52 | + # Check if player table exists |
| 53 | + cursor.execute(""" |
| 54 | + SELECT EXISTS ( |
| 55 | + SELECT FROM information_schema.tables |
| 56 | + WHERE table_name = 'player' |
| 57 | + ); |
| 58 | + """) |
| 59 | + table_exists = cursor.fetchone()[0] |
| 60 | + |
| 61 | + if table_exists: |
| 62 | + cursor.execute("SELECT COUNT(*) FROM player;") |
| 63 | + player_count = cursor.fetchone()[0] |
| 64 | + print(f"✅ Player table exists with {player_count} players") |
| 65 | + else: |
| 66 | + print("⚠️ Player table does not exist yet (this is OK if database is new)") |
| 67 | + |
| 68 | + cursor.close() |
| 69 | + connection.close() |
| 70 | + print("\n✅ All tests passed!") |
| 71 | + return True |
| 72 | + |
| 73 | + except Error as e: |
| 74 | + print(f"\n❌ Connection failed: {e}") |
| 75 | + print("\n💡 Troubleshooting:") |
| 76 | + print(" 1. Check that DB_HOST is correct (should start with 'db.')") |
| 77 | + print(" 2. Verify DB_PASSWORD is correct") |
| 78 | + print(" 3. Ensure your IP is allowed in Supabase (Settings → Database → Connection Pooling)") |
| 79 | + print(" 4. Check that the database is running in Supabase dashboard") |
| 80 | + return False |
| 81 | + except Exception as e: |
| 82 | + print(f"\n❌ Unexpected error: {e}") |
| 83 | + return False |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + success = test_connection() |
| 87 | + sys.exit(0 if success else 1) |
| 88 | + |
0 commit comments