-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
33 lines (28 loc) · 873 Bytes
/
Copy pathdatabase.py
File metadata and controls
33 lines (28 loc) · 873 Bytes
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
"""
Database connection and initialization
"""
import os
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
from models import Base
# Get database URL from environment
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql+asyncpg://taskuser:taskpass@localhost:5432/taskdb")
# Create async engine
engine = create_async_engine(
DATABASE_URL,
echo=True, # Print SQL statements (helpful for learning!)
future=True
)
# Create session factory
async_session = async_sessionmaker(
engine,
class_=AsyncSession,
expire_on_commit=False
)
async def init_db():
"""Create all tables"""
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async def get_session():
"""Dependency to get a database session"""
async with async_session() as session:
yield session