59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
#!/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)
|