"""Backfill Scene.duration_sec z live playback_source, gdzie Scene NULL. Tube'y zapisują duration na playback_source, a nie na Scene → 74% grywalnego katalogu miało Scene.duration_sec=NULL → mobilny filtr `min_duration_sec=60` (Scene.duration_sec >= 60; NULL >= 60 = false) chował te sceny mimo że są grywalne i długość jest znana (bug-report 2026-06-01 Safira Yakkuza: 119 w ulubionych, 14 po wejściu). Propagacja forward jest w resolverze (`_effective_duration`); ten skrypt nadrabia istniejące. Idempotentny — ustawia tylko wiersze z NULL. Użycie: python scripts/backfill_scene_duration_from_playback.py """ from __future__ import annotations import logging import sys from sqlalchemy import text from app.db import session_scope logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") log = logging.getLogger("backfill_duration") _SQL = text( """ UPDATE scenes sc SET duration_sec = sub.d FROM ( SELECT scene_id, max(duration_sec) AS d FROM playback_sources WHERE dead_at IS NULL AND duration_sec IS NOT NULL GROUP BY scene_id ) sub WHERE sc.id = sub.scene_id AND sc.duration_sec IS NULL """ ) def main() -> int: with session_scope() as session: res = session.execute(_SQL) log.info("DONE: backfilled Scene.duration_sec for %d scenes", res.rowcount or 0) return 0 if __name__ == "__main__": sys.exit(main())