- Added ASYNCPG_DATABASE_URL constant in database.py - Updated jobs.py to import from database module - Updated worker.py to import from database module - Removed duplicate URL parsing logic from both files
23 lines
629 B
Python
23 lines
629 B
Python
import os
|
|
|
|
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
|
from sqlalchemy.orm import DeclarativeBase
|
|
|
|
DATABASE_URL = os.getenv(
|
|
"DATABASE_URL", "postgresql+asyncpg://postgres:postgres@localhost:5432/arbret"
|
|
)
|
|
|
|
# asyncpg needs postgresql:// instead of postgresql+asyncpg://
|
|
ASYNCPG_DATABASE_URL = DATABASE_URL.replace("postgresql+asyncpg://", "postgresql://")
|
|
|
|
engine = create_async_engine(DATABASE_URL)
|
|
async_session = async_sessionmaker(engine, expire_on_commit=False)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
async def get_db():
|
|
async with async_session() as session:
|
|
yield session
|