Spaces:
Running
Running

Refactor application structure: rename 'models' directory to 'db_models', update import paths across multiple files, and enhance Dockerfile to create and populate the new directory, improving organization and clarity in the codebase.
df50c8c
"""Models package initialization.""" | |
from app.db_models.database import Base, Audiobook, AudiobookChunk, TextChunk, AudiobookStatus | |
__all__ = [ | |
'Base', | |
'Audiobook', | |
'AudiobookChunk', | |
'TextChunk', | |
'AudiobookStatus' | |
] | |
""" | |
Database configuration and session management. | |
""" | |
import os | |
from sqlalchemy import create_engine | |
from sqlalchemy.orm import sessionmaker | |
from sqlalchemy.ext.declarative import declarative_base | |
# Get database URL from environment or use SQLite as default | |
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///app/storage/audiobooks.db") | |
# Create engine with connection pool settings | |
engine = create_engine( | |
DATABASE_URL, | |
pool_size=5, | |
max_overflow=10, | |
pool_timeout=30, | |
pool_recycle=1800, | |
echo=False # Set to True for SQL query logging | |
) | |
# Create session factory | |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
def get_db(): | |
"""Get database session.""" | |
db = SessionLocal() | |
try: | |
yield db | |
finally: | |
db.close() | |
# Import models to ensure they are registered with SQLAlchemy | |
from .database import Base, Audiobook, Voice, AudioCache | |
# Create all tables | |
def init_db(): | |
"""Initialize database tables.""" | |
Base.metadata.create_all(bind=engine) | |
__all__ = [ | |
'Base', | |
'Audiobook', | |
'Voice', | |
'AudioCache', | |
'get_db', | |
'init_db', | |
'engine', | |
'SessionLocal' | |
] |