This commit is contained in:
team3
2026-06-12 17:46:30 +02:00
parent 0ba708dc54
commit a7fd345bb6
8 changed files with 136 additions and 77 deletions

View File

@@ -47,14 +47,15 @@ CREATE TABLE IF NOT EXISTS elements (
)
"""
CREATE_VERTIEFUNGEN = """
CREATE TABLE IF NOT EXISTS vertiefungen (
CREATE_BAUSTEIN_TEXTE = """
CREATE TABLE IF NOT EXISTS baustein_texte (
topic TEXT NOT NULL,
baustein TEXT NOT NULL,
art TEXT NOT NULL,
md TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
PRIMARY KEY (topic, baustein)
PRIMARY KEY (topic, baustein, art)
)
"""
@@ -89,12 +90,20 @@ async def init_db():
await db.execute(CREATE_PROGRESS)
await db.execute(CREATE_TOPICS)
await db.execute(CREATE_ELEMENTS)
await db.execute(CREATE_VERTIEFUNGEN)
await db.execute(CREATE_BAUSTEIN_TEXTE)
await db.execute(CREATE_BAUSTEIN_PROGRESS)
try: # Migration für Bestands-DBs ohne step-Spalte
await db.execute("ALTER TABLE guides ADD COLUMN step INTEGER")
except aiosqlite.OperationalError:
pass
# Migration: alte vertiefungen-Tabelle → baustein_texte (Bestand = lange Form, art 'deepdive')
cursor = await db.execute("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'vertiefungen'")
if await cursor.fetchone():
await db.execute(
"INSERT OR IGNORE INTO baustein_texte (topic, baustein, art, md, created_at, updated_at) "
"SELECT topic, baustein, 'deepdive', md, created_at, updated_at FROM vertiefungen"
)
await db.execute("DROP TABLE vertiefungen")
await db.execute(
"UPDATE guides SET status = 'error', progress = NULL, error_msg = 'Server-Neustart' "
"WHERE status IN ('queued', 'generating')"
@@ -288,33 +297,37 @@ def _now() -> str:
return datetime.now(timezone.utc).isoformat()
async def get_vertiefung(topic: str, baustein: str) -> str | None:
async def get_vertiefung(topic: str, baustein: str, art: str) -> str | None:
db = await get_db()
cursor = await db.execute(
"SELECT md FROM vertiefungen WHERE topic = ? AND baustein = ?", (topic, baustein)
"SELECT md FROM baustein_texte WHERE topic = ? AND baustein = ? AND art = ?",
(topic, baustein, art),
)
row = await cursor.fetchone()
return row[0] if row else None
async def set_vertiefung(topic: str, baustein: str, md: str) -> None:
async def set_vertiefung(topic: str, baustein: str, art: str, md: str) -> None:
db = await get_db()
now = _now()
await db.execute(
"""INSERT INTO vertiefungen (topic, baustein, md, created_at, updated_at)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(topic, baustein) DO UPDATE SET md = excluded.md, updated_at = excluded.updated_at""",
(topic, baustein, md, now, now),
"""INSERT INTO baustein_texte (topic, baustein, art, md, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(topic, baustein, art) DO UPDATE SET md = excluded.md, updated_at = excluded.updated_at""",
(topic, baustein, art, md, now, now),
)
await db.commit()
async def list_vertiefungen(topic: str) -> set[str]:
"""Baustein-Titel, zu denen eine Vertiefung existiert."""
async def list_vertiefungen(topic: str) -> dict[str, set[str]]:
"""Baustein-Titel → vorhandene Text-Arten ('vertiefung'/'deepdive')."""
db = await get_db()
cursor = await db.execute("SELECT baustein FROM vertiefungen WHERE topic = ?", (topic,))
cursor = await db.execute("SELECT baustein, art FROM baustein_texte WHERE topic = ?", (topic,))
rows = await cursor.fetchall()
return {row[0] for row in rows}
out: dict[str, set[str]] = {}
for baustein, art in rows:
out.setdefault(baustein, set()).add(art)
return out
async def list_baustein_progress(topic: str) -> list[dict]:
@@ -377,6 +390,6 @@ async def list_baustein_absolviert_all() -> dict[str, set[str]]:
async def delete_baustein_daten(topic: str) -> None:
db = await get_db()
await db.execute("DELETE FROM vertiefungen WHERE topic = ?", (topic,))
await db.execute("DELETE FROM baustein_texte WHERE topic = ?", (topic,))
await db.execute("DELETE FROM baustein_progress WHERE topic = ?", (topic,))
await db.commit()