Spaces:
Running
Running
File size: 1,026 Bytes
23d1df7 09ecaf7 23d1df7 bc8e3e9 23d1df7 09ecaf7 01c5951 23d1df7 4117d7e 36535c6 bc8e3e9 36535c6 a0bb0ee 4117d7e 01c5951 23d1df7 4117d7e e9f41d8 4117d7e 23d1df7 09ecaf7 23d1df7 09ecaf7 23d1df7 |
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 |
import os
import logging
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from .config import settings
logger = logging.getLogger(__name__)
raw_db_url = settings.DATABASE_URL
if raw_db_url.startswith("psql '") and raw_db_url.endswith("'"):
raw_db_url = raw_db_url[6:-1]
# Convert postgresql:// to postgresql+psycopg:// for psycopg v3
if raw_db_url.startswith("postgresql://") and not raw_db_url.startswith("postgresql+psycopg://"):
raw_db_url = raw_db_url.replace("postgresql://", "postgresql+psycopg://")
if "sslmode=" not in raw_db_url and "localhost" not in raw_db_url and "127.0.0.1" not in raw_db_url:
raw_db_url = f"{raw_db_url}{'&' if '?' in raw_db_url else '?'}sslmode=require"
logger.debug(f"database url: {raw_db_url}")
engine = create_engine(
raw_db_url,
pool_pre_ping=True,
pool_recycle=300,
)
SessionLocal = sessionmaker(
autocommit=False,
autoflush=False,
bind=engine,
)
Base = declarative_base()
|