mantimon-tcg/backend/app/db/__init__.py
Cal Corum 50684a1b11 Add database infrastructure with SQLAlchemy models and test suite
Phase 1 Database Implementation (DB-001 through DB-012):

Models:
- User: OAuth support (Google/Discord), premium subscriptions
- Collection: Card ownership with CardSource enum
- Deck: JSONB cards/energy_cards, validation state
- CampaignProgress: One-to-one with User, medals/NPCs as JSONB
- ActiveGame: In-progress games with GameType enum
- GameHistory: Completed games with EndReason enum, replay data

Infrastructure:
- Alembic migrations with sync psycopg2 (avoids async issues)
- Docker Compose for Postgres (5433) and Redis (6380)
- App config with Pydantic settings
- Redis client helper

Test Infrastructure:
- 68 database tests (47 model + 21 relationship)
- Async factory pattern for test data creation
- Sync TRUNCATE cleanup (solves pytest-asyncio event loop mismatch)
- Uses dev containers instead of testcontainers for reliability

Key technical decisions:
- passive_deletes=True for ON DELETE SET NULL relationships
- NullPool for test sessions (no connection reuse)
- expire_on_commit=False with manual expire() for relationship tests
2026-01-27 10:17:30 -06:00

34 lines
889 B
Python

"""Database module for Mantimon TCG.
This module provides the database infrastructure including:
- Async SQLAlchemy engine and session management
- Base model class with common columns
- Redis connection utilities
- All database models
Usage:
from app.db import get_session, Base
from app.db.models import User, Deck, Collection
async with get_session() as session:
user = await session.get(User, user_id)
Exports:
- get_session: Async context manager for database sessions
- get_engine: Get the async engine instance
- Base: Declarative base class for models
- init_db: Initialize database (create tables)
- close_db: Close database connections
"""
from app.db.base import Base
from app.db.session import close_db, get_engine, get_session, init_db
__all__ = [
"Base",
"get_session",
"get_engine",
"init_db",
"close_db",
]