Buckets:
| """ | |
| Database layer for Zenith framework. | |
| Provides SQLAlchemy 2.0 integration with async support, session management, | |
| and transaction handling for the context system. | |
| """ | |
| import asyncio | |
| from collections.abc import AsyncGenerator | |
| from contextlib import asynccontextmanager | |
| from weakref import WeakKeyDictionary | |
| from sqlalchemy import MetaData, text | |
| from sqlalchemy.ext.asyncio import ( | |
| AsyncEngine, | |
| AsyncSession, | |
| async_sessionmaker, | |
| create_async_engine, | |
| ) | |
| from sqlalchemy.orm import DeclarativeBase | |
| # Naming conventions for database constraints | |
| convention = { | |
| "ix": "ix_%(column_0_label)s", | |
| "uq": "uq_%(table_name)s_%(column_0_name)s", | |
| "ck": "ck_%(table_name)s_%(constraint_name)s", | |
| "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", | |
| "pk": "pk_%(table_name)s", | |
| } | |
| metadata = MetaData(naming_convention=convention) | |
| class Base(DeclarativeBase): | |
| """Base class for all database models.""" | |
| metadata = metadata | |
| class Database: | |
| """ | |
| Database connection and session management with built-in optimizations. | |
| Provides async database operations with proper session handling, | |
| transaction support, and request-scoped connection reuse for | |
| 15-25% performance improvement. | |
| """ | |
| def __init__( | |
| self, | |
| url: str, | |
| echo: bool = False, | |
| pool_size: int = 20, | |
| max_overflow: int = 30, | |
| pool_timeout: int = 30, | |
| pool_recycle: int = 3600, | |
| ): | |
| """ | |
| Initialize database configuration (but NOT the engine). | |
| The engine is created lazily per event loop to avoid binding issues. | |
| Args: | |
| url: Database URL (postgresql+asyncpg://...) | |
| echo: Enable SQL logging | |
| pool_size: Connection pool size | |
| max_overflow: Additional connections beyond pool_size | |
| pool_timeout: Timeout for getting connection from pool | |
| pool_recycle: Connection lifetime in seconds | |
| """ | |
| self.url = url | |
| self.echo = echo | |
| self.pool_size = pool_size | |
| self.max_overflow = max_overflow | |
| self.pool_timeout = pool_timeout | |
| self.pool_recycle = pool_recycle | |
| # Store engines per event loop - this is the KEY FIX | |
| self._loop_engines: WeakKeyDictionary[ | |
| asyncio.AbstractEventLoop, AsyncEngine | |
| ] = WeakKeyDictionary() | |
| self._loop_sessions: WeakKeyDictionary[ | |
| asyncio.AbstractEventLoop, async_sessionmaker | |
| ] = WeakKeyDictionary() | |
| def engine(self) -> AsyncEngine: | |
| """ | |
| Get or create an engine for the current event loop. | |
| This is the fix - we create a separate engine per event loop instead | |
| of sharing one engine across all loops. | |
| """ | |
| loop = asyncio.get_event_loop() | |
| if loop not in self._loop_engines: | |
| # Create new engine for this event loop | |
| # SQLite doesn't support pool configuration | |
| if self.url.startswith("sqlite"): | |
| engine = create_async_engine( | |
| self.url, | |
| echo=self.echo, | |
| ) | |
| else: | |
| engine = create_async_engine( | |
| self.url, | |
| echo=self.echo, | |
| pool_size=self.pool_size, | |
| max_overflow=self.max_overflow, | |
| pool_timeout=self.pool_timeout, | |
| pool_recycle=self.pool_recycle, | |
| pool_pre_ping=True, # Verify connections before using | |
| ) | |
| self._loop_engines[loop] = engine | |
| return self._loop_engines[loop] | |
| def async_session(self) -> async_sessionmaker: | |
| """ | |
| Get or create a session maker for the current event loop. | |
| """ | |
| loop = asyncio.get_event_loop() | |
| if loop not in self._loop_sessions: | |
| # Create session maker for this event loop's engine | |
| session_maker = async_sessionmaker( | |
| bind=self.engine, # Uses the loop-specific engine | |
| class_=AsyncSession, | |
| expire_on_commit=False, | |
| ) | |
| self._loop_sessions[loop] = session_maker | |
| return self._loop_sessions[loop] | |
| async def session(self, scope: dict | None = None) -> AsyncGenerator[AsyncSession]: | |
| """ | |
| Create a database session with automatic request-scoped reuse. | |
| If called within a web request, reuses the request-scoped session | |
| for 15-25% performance improvement. Otherwise creates a new session. | |
| Args: | |
| scope: ASGI scope (automatically provided in web context) | |
| Usage: | |
| async with db.session() as session: | |
| user = User(name="Alice") | |
| session.add(user) | |
| await session.commit() | |
| """ | |
| # Check for request-scoped session first (optimization) | |
| if scope and "db_session" in scope: | |
| # Reuse existing request-scoped session | |
| yield scope["db_session"] | |
| return | |
| # Create new session | |
| async with self.async_session() as session: | |
| try: | |
| yield session | |
| await session.commit() | |
| except Exception: | |
| await session.rollback() | |
| raise | |
| finally: | |
| await session.close() | |
| async def request_scoped_session(self, scope: dict) -> AsyncGenerator[AsyncSession]: | |
| """ | |
| Create a request-scoped database session for web requests. | |
| This session is stored in the ASGI scope and reused across | |
| all database operations within the same HTTP request. | |
| Args: | |
| scope: ASGI scope dictionary | |
| Usage: | |
| # In middleware or dependency injection | |
| async with db.request_scoped_session(scope) as session: | |
| scope["db_session"] = session | |
| # Session available for entire request lifecycle | |
| """ | |
| if "db_session" in scope: | |
| # Session already exists for this request | |
| yield scope["db_session"] | |
| return | |
| async with self.async_session() as session: | |
| try: | |
| # Store session in request scope for reuse | |
| scope["db_session"] = session | |
| yield session | |
| await session.commit() | |
| except Exception: | |
| await session.rollback() | |
| raise | |
| finally: | |
| # Clean up scope | |
| scope.pop("db_session", None) | |
| await session.close() | |
| async def transaction(self) -> AsyncGenerator[AsyncSession]: | |
| """ | |
| Create a database transaction. | |
| Automatically rolls back on exception. | |
| Usage: | |
| async with db.transaction() as session: | |
| # All operations here are in a transaction | |
| user = User(name="Bob") | |
| session.add(user) | |
| # Commits automatically if no exception | |
| """ | |
| async with self.session() as session, session.begin(): | |
| yield session | |
| async def create_all(self) -> None: | |
| """Create all database tables.""" | |
| # Import SQLModel here to avoid circular imports | |
| from sqlmodel import SQLModel | |
| async with self.engine.begin() as conn: | |
| # Create tables for both Base models and SQLModel models | |
| await conn.run_sync(Base.metadata.create_all) | |
| await conn.run_sync(SQLModel.metadata.create_all) | |
| async def drop_all(self) -> None: | |
| """Drop all database tables. Use with caution!""" | |
| # Import SQLModel here to avoid circular imports | |
| from sqlmodel import SQLModel | |
| async with self.engine.begin() as conn: | |
| # Drop tables for both Base models and SQLModel models | |
| await conn.run_sync(SQLModel.metadata.drop_all) | |
| await conn.run_sync(Base.metadata.drop_all) | |
| async def close(self) -> None: | |
| """Close all database connections across all event loops.""" | |
| for engine in self._loop_engines.values(): | |
| await engine.dispose() | |
| self._loop_engines.clear() | |
| self._loop_sessions.clear() | |
| async def health_check(self) -> bool: | |
| """Check if database is accessible.""" | |
| try: | |
| async with self.session() as session: | |
| await session.execute(text("SELECT 1")) | |
| return True | |
| except Exception: | |
| return False | |
| def enable_tracing( | |
| self, | |
| slow_threshold_ms: float = 100.0, | |
| log_all_queries: bool = False, | |
| collect_stats: bool = True, | |
| ) -> "QueryTracer": | |
| """ | |
| Enable query tracing with slow query logging. | |
| Args: | |
| slow_threshold_ms: Threshold for slow query warnings (default 100ms) | |
| log_all_queries: Log all queries, not just slow ones | |
| collect_stats: Collect query statistics | |
| Returns: | |
| QueryTracer instance | |
| Example: | |
| db = Database("postgresql+asyncpg://...") | |
| tracer = db.enable_tracing(slow_threshold_ms=50) | |
| # Later, get stats | |
| stats = tracer.get_stats() | |
| """ | |
| from .tracing import QueryTracer | |
| tracer = QueryTracer( | |
| slow_threshold_ms=slow_threshold_ms, | |
| log_all_queries=log_all_queries, | |
| collect_stats=collect_stats, | |
| ) | |
| tracer.attach(self.engine) | |
| return tracer | |
| # Import migration system | |
| from .migrations import MigrationManager, create_migration_manager # noqa: E402 | |
| # Import Rails-like models | |
| from .models import QueryBuilder, ZenithModel # noqa: E402 | |
| # Import SQLModel integration | |
| from .sqlmodel import ( # noqa: E402 | |
| Field, | |
| Model, | |
| Relationship, | |
| SQLModel, | |
| SQLModelRepository, | |
| create_repository, | |
| ) | |
| # Import query tracing | |
| from .tracing import ( # noqa: E402 | |
| QueryStats, | |
| QueryTracer, | |
| disable_query_tracing, | |
| enable_query_tracing, | |
| get_query_stats, | |
| reset_query_stats, | |
| ) | |
| # Export commonly used components | |
| __all__ = [ | |
| "AsyncSession", | |
| "Base", | |
| "Database", | |
| "Field", | |
| "MigrationManager", | |
| "Model", | |
| "QueryBuilder", | |
| "QueryStats", | |
| "QueryTracer", | |
| "Relationship", | |
| # SQLModel components | |
| "SQLModel", | |
| "SQLModelRepository", | |
| "ZenithModel", | |
| "async_sessionmaker", | |
| "create_async_engine", | |
| "create_migration_manager", | |
| "create_repository", | |
| "disable_query_tracing", | |
| "enable_query_tracing", | |
| "get_query_stats", | |
| "reset_query_stats", | |
| ] | |
Xet Storage Details
- Size:
- 10.7 kB
- Xet hash:
- bd94c07da2440e15e6f07a36112737d35e6f61f0f623f1fa29904e5b97c849e9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.