80 lines
2.3 KiB
Python
80 lines
2.3 KiB
Python
"""Diagnostic script to check database connection and configuration."""
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
|
|
print("="*70)
|
|
print("FAMILY HUB - DATABASE DIAGNOSTIC")
|
|
print("="*70)
|
|
print()
|
|
|
|
# Add parent directory to path
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
|
|
print(f"Current Working Directory: {os.getcwd()}")
|
|
print(f"Script Location: {Path(__file__).parent.absolute()}")
|
|
print()
|
|
|
|
try:
|
|
from app.core.config import settings
|
|
from app.core.database import engine, SessionLocal
|
|
from app.models.user import User
|
|
|
|
print("✓ Successfully imported app modules")
|
|
print()
|
|
|
|
print("DATABASE CONFIGURATION:")
|
|
print(f" DATABASE_URL: {settings.DATABASE_URL}")
|
|
print(f" Database Engine: {engine.url}")
|
|
print()
|
|
|
|
# Check if database file exists
|
|
db_path = str(engine.url).replace("sqlite:///", "")
|
|
if db_path.startswith("./"):
|
|
db_path = os.path.join(os.getcwd(), db_path[2:])
|
|
|
|
print(f" Resolved DB Path: {db_path}")
|
|
print(f" Database Exists: {os.path.exists(db_path)}")
|
|
|
|
if os.path.exists(db_path):
|
|
file_size = os.path.getsize(db_path)
|
|
print(f" Database Size: {file_size:,} bytes")
|
|
print()
|
|
|
|
# Try to connect and query
|
|
print("ATTEMPTING DATABASE CONNECTION:")
|
|
db = SessionLocal()
|
|
try:
|
|
user_count = db.query(User).count()
|
|
print(f" ✅ Connection successful!")
|
|
print(f" Total users in database: {user_count}")
|
|
print()
|
|
|
|
if user_count > 0:
|
|
print("USERS IN DATABASE:")
|
|
users = db.query(User).all()
|
|
for user in users:
|
|
print(f" - {user.username} ({user.full_name})")
|
|
print(f" Email: {user.email}")
|
|
print(f" Admin: {user.is_admin}, Active: {user.is_active}")
|
|
print(f" Password Hash: {user.hashed_password[:50]}...")
|
|
print()
|
|
else:
|
|
print(" ⚠️ No users found in database!")
|
|
print(" You may need to run: python init_db.py")
|
|
|
|
except Exception as e:
|
|
print(f" ❌ Database connection failed: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
finally:
|
|
db.close()
|
|
|
|
except Exception as e:
|
|
print(f"❌ Failed to import modules: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
print()
|
|
print("="*70)
|