Spaces:
Sleeping
Sleeping
File size: 1,316 Bytes
c19c7bf | 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | """
Database setup and session management.
Uses SQLAlchemy with SQLite for local dev, Postgres-compatible schema.
"""
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Session
from pathlib import Path
import logging
from app.config import settings
logger = logging.getLogger(__name__)
# Database path
DB_DIR = settings.DATA_DIR / "billing"
DB_DIR.mkdir(parents=True, exist_ok=True)
DATABASE_URL = f"sqlite:///{DB_DIR / 'billing.db'}"
# Create engine
engine = create_engine(
DATABASE_URL,
connect_args={"check_same_thread": False}, # SQLite specific
echo=False # Set to True for SQL query logging
)
# Session factory
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Base class for models
Base = declarative_base()
def get_db() -> Session:
"""Get database session (dependency for FastAPI)."""
db = SessionLocal()
try:
yield db
finally:
db.close()
def init_db():
"""Initialize database tables."""
Base.metadata.create_all(bind=engine)
logger.info("Database tables created/verified")
def drop_db():
"""Drop all tables (use with caution!)."""
Base.metadata.drop_all(bind=engine)
logger.warning("All database tables dropped")
|