AhmadYarAI's picture
Done the configuration
cd8b88c
raw
history blame contribute delete
759 Bytes
import os
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from dotenv import load_dotenv
load_dotenv()
# 1. Get the URL
DATABASE_URL = os.getenv("DATABASE_URL")
# 2. FIX: Convert postgres:// to postgresql:// for SQLAlchemy
if DATABASE_URL and DATABASE_URL.startswith("postgres://"):
DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://", 1)
# 3. Create the engine
engine = create_engine(
DATABASE_URL,
pool_pre_ping=True,
pool_recycle=300,
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()