major-domo-database/test_postgres.py
Cal Corum 79a559088a CLAUDE: Phase 1 PostgreSQL migration fixes complete
- Fixed 4 critical schema issues blocking migration
- Resolved integer overflow by converting Discord IDs to strings
- Fixed VARCHAR length limits for Google Photos URLs
- Made injury_count field nullable for NULL values
- Successfully migrating 7/30 tables (5,432+ records)

Issues resolved:
- CONSTRAINT-CURRENT-INJURY_COUNT-001: Made nullable
- DATA_QUALITY-PLAYER-NAME-001: Increased VARCHAR limits to 1000
- MIGRATION_LOGIC-TEAM-INTEGER-001: Discord IDs now strings
- MIGRATION_LOGIC-DRAFTDATA-INTEGER-001: Channel IDs now strings

New issues discovered for Phase 2:
- CONSTRAINT-CURRENT-BSTATCOUNT-001: NULL stats count
- CONSTRAINT-TEAM-AUTO_DRAFT-001: NULL auto draft flag

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-18 18:09:45 -05:00

103 lines
3.1 KiB
Python

#!/usr/bin/env python3
import os
import logging
from datetime import datetime
# Set environment variables for PostgreSQL
os.environ['DATABASE_TYPE'] = 'postgresql'
os.environ['POSTGRES_DB'] = 'sba_master'
os.environ['POSTGRES_USER'] = 'sba_admin'
os.environ['POSTGRES_PASSWORD'] = 'sba_dev_password_2024'
os.environ['POSTGRES_HOST'] = 'localhost'
os.environ['POSTGRES_PORT'] = '5432'
# Import after setting environment variables
from app.db_engine import db, Current, Team, Player, SbaPlayer, Manager, Division
logger = logging.getLogger(f'{__name__}.test_postgres')
def test_connection():
"""Test PostgreSQL connection"""
try:
logger.info("Testing PostgreSQL connection...")
db.connect()
logger.info("✓ Connected to PostgreSQL successfully")
# Test basic query
result = db.execute_sql("SELECT version();").fetchone()
logger.info(f"✓ PostgreSQL version: {result[0]}")
db.close()
return True
except Exception as e:
logger.error(f"✗ Connection failed: {e}")
return False
def test_schema_creation():
"""Test creating tables in PostgreSQL"""
try:
logger.info("Testing schema creation...")
db.connect()
# Create tables in dependency order
# First: tables with no dependencies
base_tables = [Current, Manager, Division, SbaPlayer]
db.create_tables(base_tables, safe=True)
# Second: tables that depend on base tables
dependent_tables = [Team, Player]
db.create_tables(dependent_tables, safe=True)
logger.info("✓ Test tables created successfully")
# Test table existence
all_test_tables = base_tables + dependent_tables
for table in all_test_tables:
table_name = table._meta.table_name
result = db.execute_sql("""
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = %s
);
""", (table_name,)).fetchone()
if result[0]:
logger.info(f"✓ Table '{table_name}' exists")
else:
logger.error(f"✗ Table '{table_name}' missing")
db.close()
return True
except Exception as e:
logger.error(f"✗ Schema creation failed: {e}")
return False
def main():
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger.info("Starting PostgreSQL migration test...")
# Test connection
if not test_connection():
logger.error("Connection test failed - stopping")
return False
# Test schema creation
if not test_schema_creation():
logger.error("Schema creation test failed - stopping")
return False
logger.info("✓ All PostgreSQL tests passed!")
return True
if __name__ == "__main__":
success = main()
exit(0 if success else 1)