From 4f988959ce5f67be6bc79ad780d0d8661a6f8f81 Mon Sep 17 00:00:00 2001 From: retoor Date: Tue, 24 Jun 2025 16:58:50 +0200 Subject: [PATCH] Update FTS. --- main.py | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 2658e54..90bdcd6 100644 --- a/main.py +++ b/main.py @@ -77,10 +77,43 @@ app.mount("/static", StaticFiles(directory=UPLOAD_DIR), name="static") if pathlib.Path(FRONTEND_DIR).exists(): app.mount("/assets", StaticFiles(directory=FRONTEND_DIR), name="assets") +async def ensure_tables_exist(): + async with db_session() as db: + # Check if 'notes' table exists + if 'notes' not in db.tables: + db.query(""" + CREATE TABLE notes ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT, + body TEXT, + created_at TEXT, + updated_at TEXT + ) + """) + # Check if 'tags' table exists + if 'tags' not in db.tables: + db.query(""" + CREATE TABLE tags ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT UNIQUE + ) + """) + # Check if 'note_tags' table exists + if 'note_tags' not in db.tables: + db.query(""" + CREATE TABLE note_tags ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + note_id INTEGER, + tag TEXT, + FOREIGN KEY(note_id) REFERENCES notes(id) + ) + """) + @app.on_event("startup") async def init_search_index(): + await ensure_tables_exist() db = dataset.connect(DB_URL) # Create the FTS5 virtual table over notes.title and notes.body db.query(""" @@ -243,12 +276,6 @@ async def _upsert_note(note_id: Optional[int], payload: Dict[str, Any]): db.query("DELETE FROM notes_fts WHERE note_id = :nid", nid=note_id) # (Re‑)insert into FTS table - db.query( - "INSERT INTO notes_fts (title, body, note_id) VALUES (:title, :body, :nid)", - title=title, - body=body, - nid=note_id, - ) for t in tags: t = t.strip() if not t: