update
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import aiosqlite
|
||||
from config import DB_PATH
|
||||
from config import DB_PATH, STORAGE_DIR
|
||||
from paths import final_paths
|
||||
|
||||
CREATE_GUIDES = """
|
||||
CREATE TABLE IF NOT EXISTS guides (
|
||||
@@ -10,8 +11,6 @@ CREATE TABLE IF NOT EXISTS guides (
|
||||
status TEXT NOT NULL DEFAULT 'queued',
|
||||
progress TEXT,
|
||||
error_msg TEXT,
|
||||
html_path TEXT,
|
||||
pdf_path TEXT,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
)
|
||||
@@ -68,6 +67,20 @@ async def init_db():
|
||||
"WHERE status IN ('queued', 'generating')"
|
||||
)
|
||||
await db.commit()
|
||||
await _migrate_uuid_filenames(db)
|
||||
|
||||
|
||||
async def _migrate_uuid_filenames(db: aiosqlite.Connection) -> None:
|
||||
cursor = await db.execute("SELECT id, topic, format FROM guides WHERE status = 'done'")
|
||||
rows = await cursor.fetchall()
|
||||
for guide_id, topic, format_name in rows:
|
||||
final_html, final_pdf = final_paths(topic, format_name)
|
||||
old_html = STORAGE_DIR / "html" / f"{guide_id}.html"
|
||||
old_pdf = STORAGE_DIR / "pdf" / f"{guide_id}.pdf"
|
||||
if old_html.exists() and not final_html.exists():
|
||||
old_html.rename(final_html)
|
||||
if old_pdf.exists() and not final_pdf.exists():
|
||||
old_pdf.rename(final_pdf)
|
||||
|
||||
|
||||
async def close_db():
|
||||
@@ -85,8 +98,8 @@ def _row_to_dict(row, cursor):
|
||||
async def create_guide(guide: dict) -> dict:
|
||||
db = await get_db()
|
||||
await db.execute(
|
||||
"""INSERT INTO guides (id, topic, format, instructions, status, progress, html_path, pdf_path, created_at, updated_at)
|
||||
VALUES (:id, :topic, :format, :instructions, :status, :progress, :html_path, :pdf_path, :created_at, :updated_at)""",
|
||||
"""INSERT INTO guides (id, topic, format, instructions, status, progress, created_at, updated_at)
|
||||
VALUES (:id, :topic, :format, :instructions, :status, :progress, :created_at, :updated_at)""",
|
||||
guide,
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
Reference in New Issue
Block a user