File size: 1,407 Bytes
df50c8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
"""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'
]