Spaces:
Runtime error
Runtime error
File size: 1,471 Bytes
f3997d4 0c11134 42ae809 0c11134 42ae809 0c11134 42ae809 0c11134 f3997d4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | from sqlalchemy import create_engine, Column, String, Integer, Text, DateTime, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
from datetime import datetime
import uuid
from app.config.settings import settings
# Create SQLAlchemy engine
db_url = settings.DATABASE_URL
# Safety check for unencoded passwords (very common issue)
if "@" in db_url.split("://")[-1].split("@")[0] and not db_url.startswith("sqlite"):
print("⚠️ DATABASE_URL detected with unencoded '@' in password.")
# We don't fix it automatically to avoid messing up complex passwords,
# but we inform the user in the connection logic if it fails.
# Check if using SQLite (special handling for threads)
if db_url.startswith("sqlite"):
engine = create_engine(
db_url,
connect_args={"check_same_thread": False}
)
else:
# Postgres/Other
try:
engine = create_engine(db_url)
except Exception as e:
print(f"❌ Failed to create engine for {db_url.split('@')[-1]}")
raise e
# Session factory
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Base class for models
Base = declarative_base()
def get_db():
"""Dependency to get database session."""
db = SessionLocal()
try:
yield db
finally:
db.close()
def init_db():
"""Initialize database tables."""
Base.metadata.create_all(bind=engine)
|