Spaces:
Running
Running
File size: 1,084 Bytes
aa27d2d 0edd56d 96c41f2 0edd56d 96c41f2 0edd56d aa27d2d | 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 | 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() |