Spotix-API / backend /app /db /database.py
Anish-530
Fix database URL driver normalization for psycopg2
96c41f2
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from app.core.config import settings
if settings.DATABASE_URL.startswith("sqlite"):
engine = create_engine(
settings.DATABASE_URL,
connect_args={"check_same_thread": False}
)
else:
db_url = settings.DATABASE_URL
# Normalize PaaS postgres:// or postgresql:// URLs to explicitly use the psycopg2 driver
if db_url.startswith("postgres://"):
db_url = db_url.replace("postgres://", "postgresql+psycopg2://", 1)
elif db_url.startswith("postgresql://"):
db_url = db_url.replace("postgresql://", "postgresql+psycopg2://", 1)
elif db_url.startswith("postgresql+psycopg://"):
db_url = db_url.replace("postgresql+psycopg://", "postgresql+psycopg2://", 1)
engine = create_engine(
db_url,
pool_pre_ping=True,
pool_recycle=300,
pool_size=10,
max_overflow=20
)
SessionLocal = sessionmaker(
autocommit=False,
autoflush=False,
bind=engine
)
Base = declarative_base()