File size: 2,489 Bytes
921d377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
"""
Defensive DDL for the live-play subsystem.

Rather than editing ``store.py``'s canonical ``_DDL`` list, we
apply one extra ``CREATE TABLE IF NOT EXISTS`` from inside
the subpackage so the base interactive schema stays untouched.
Every playback entry-point calls ``ensure_playback_schema()``
once per session; the guard flag makes repeat calls a no-op.

Table: ``ix_scene_queue``
  id           TEXT PRIMARY KEY
  session_id   TEXT NOT NULL
  turn_id      TEXT DEFAULT ''           chat turn that produced this job
  status       TEXT NOT NULL DEFAULT 'pending'  pending | rendering | ready | failed
  job_id       TEXT DEFAULT ''           opaque id returned by the render backend
  asset_id     TEXT DEFAULT ''           final file id once status='ready'
  prompt       TEXT NOT NULL             scene prompt fed to the renderer
  duration_sec INTEGER DEFAULT 5
  error        TEXT DEFAULT ''           populated only on status='failed'
  created_at   DATETIME DEFAULT CURRENT_TIMESTAMP
  updated_at   DATETIME DEFAULT CURRENT_TIMESTAMP
"""
from __future__ import annotations

from .. import store


_DDL = [
    """
    CREATE TABLE IF NOT EXISTS ix_scene_queue (
        id            TEXT PRIMARY KEY,
        session_id    TEXT NOT NULL,
        turn_id       TEXT DEFAULT '',
        status        TEXT NOT NULL DEFAULT 'pending',
        job_id        TEXT DEFAULT '',
        asset_id      TEXT DEFAULT '',
        prompt        TEXT NOT NULL,
        duration_sec  INTEGER DEFAULT 5,
        error         TEXT DEFAULT '',
        created_at    DATETIME DEFAULT CURRENT_TIMESTAMP,
        updated_at    DATETIME DEFAULT CURRENT_TIMESTAMP
    )
    """,
    "CREATE INDEX IF NOT EXISTS idx_ix_scene_queue_session ON ix_scene_queue(session_id)",
    "CREATE INDEX IF NOT EXISTS idx_ix_scene_queue_status  ON ix_scene_queue(status)",
]

_INITIALIZED = False


def ensure_playback_schema() -> None:
    """Idempotent: creates the ix_scene_queue table on first call."""
    global _INITIALIZED
    if _INITIALIZED:
        return
    store.ensure_schema()
    with store._conn() as con:
        cur = con.cursor()
        for stmt in _DDL:
            cur.execute(stmt)
        con.commit()
    _INITIALIZED = True


def _reset_for_tests() -> None:
    """Clear the initialized flag — only the playback test suite
    calls this, after tearing down its temp DB path so the next
    test re-creates the table on the fresh file."""
    global _INITIALIZED
    _INITIALIZED = False