Files
ohm_streaming/app/database.py
T
root 87f245d3fc
CI / Test (Python 3.11) (pull_request) Has been cancelled
CI / Test (Python 3.12) (pull_request) Has been cancelled
CI / Lint (pull_request) Has been cancelled
CI / Type Check (pull_request) Has been cancelled
CI / Summary (pull_request) Has been cancelled
feat: flat design Sunset Glitch + download manager + settings + recommendations overhaul
- Sunset Glitch color palette applied to all templates
- Font Awesome icons throughout UI
- Download manager with parallel queue and progress tracking
- Settings page with dynamic configuration
- Recommendations router enhanced with scoring
- Local vendor libs (Alpine.js, HTMX) for offline support
- Auto test suite with screenshots
- Series releases list component
- New download model
2026-04-11 19:30:32 +00:00

71 lines
2.6 KiB
Python

"""Database configuration and session management using SQLModel"""
import os
from typing import Generator
from sqlalchemy import create_engine
from sqlmodel import SQLModel, Session, create_engine
from app.config import get_settings
settings = get_settings()
# Database URL can be overridden by environment variable DATABASE_URL
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./ohm_streaming.db")
# Create the engine
# connect_args={"check_same_thread": False} is required for SQLite and FastAPI
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
def create_db_and_tables():
"""Create the database and tables based on the models"""
# CRITICAL: Import ALL models here to ensure they are registered with SQLModel.metadata
from app.models.auth import UserTable
from app.models.watchlist import WatchlistItemTable, WatchlistSettingsTable
from app.models.favorites import FavoriteTable
from app.models.sonarr import SonarrMappingTable, SonarrConfigTable
from app.models.settings import AppSettingsTable
from app.models.download import DownloadTaskTable
SQLModel.metadata.create_all(engine)
# Add new columns to existing tables if they don't exist (SQLite workaround)
_ensure_columns(engine)
def _ensure_columns(engine):
"""Add new columns to app_settings table if they don't exist"""
from sqlalchemy import inspect, text
inspector = inspect(engine)
if 'app_settings' not in inspector.get_table_names():
return
existing = {col['name'] for col in inspector.get_columns('app_settings')}
new_columns = {
'recommendations_filter': 'TEXT DEFAULT "all"',
'releases_filter': 'TEXT DEFAULT "all"',
'anime_enabled': 'BOOLEAN DEFAULT 1',
'series_enabled': 'BOOLEAN DEFAULT 1',
'download_dir': 'TEXT DEFAULT "downloads"',
}
# Add is_admin to users table if missing
if 'users' in inspector.get_table_names():
user_cols = {col['name'] for col in inspector.get_columns('users')}
if 'is_admin' not in user_cols:
with engine.connect() as conn:
conn.execute(text('ALTER TABLE users ADD COLUMN is_admin BOOLEAN DEFAULT 0'))
conn.commit()
with engine.connect() as conn:
for col_name, col_def in new_columns.items():
if col_name not in existing:
conn.execute(text(f'ALTER TABLE app_settings ADD COLUMN {col_name} {col_def}'))
conn.commit()
def get_session() -> Generator[Session, None, None]:
"""Dependency for getting a database session"""
with Session(engine) as session:
yield session