Files
membership-be/check_schema_mismatches.py
2026-01-04 22:01:26 +07:00

59 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Check for schema mismatches between models.py and database
"""
import os
from sqlalchemy import create_engine, inspect
from dotenv import load_dotenv
from models import Base
load_dotenv()
# Connect to database
engine = create_engine(os.getenv('DATABASE_URL'))
inspector = inspect(engine)
print("=" * 80)
print("SCHEMA MISMATCH DETECTION")
print("=" * 80)
mismatches = []
# Check each model
for table_name, table in Base.metadata.tables.items():
print(f"\n📋 Checking table: {table_name}")
# Get columns from database
try:
db_columns = {col['name'] for col in inspector.get_columns(table_name)}
except Exception as e:
print(f" ❌ Table doesn't exist in database: {e}")
mismatches.append(f"{table_name}: Table missing in database")
continue
# Get columns from model
model_columns = {col.name for col in table.columns}
# Find missing columns
missing_in_db = model_columns - db_columns
extra_in_db = db_columns - model_columns
if missing_in_db:
print(f" ⚠️ Missing in DATABASE: {missing_in_db}")
mismatches.append(f"{table_name}: Missing in DB: {missing_in_db}")
if extra_in_db:
print(f" Extra in DATABASE (not in model): {extra_in_db}")
if not missing_in_db and not extra_in_db:
print(f" ✅ Schema matches!")
print("\n" + "=" * 80)
if mismatches:
print(f"❌ FOUND {len(mismatches)} MISMATCHES:")
for mismatch in mismatches:
print(f" - {mismatch}")
else:
print("✅ ALL SCHEMAS MATCH!")
print("=" * 80)