Update FTS.

This commit is contained in:
retoor 2025-06-24 16:58:50 +02:00
parent b40060f770
commit 4f988959ce

39
main.py
View File

@ -77,10 +77,43 @@ app.mount("/static", StaticFiles(directory=UPLOAD_DIR), name="static")
if pathlib.Path(FRONTEND_DIR).exists(): if pathlib.Path(FRONTEND_DIR).exists():
app.mount("/assets", StaticFiles(directory=FRONTEND_DIR), name="assets") 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") @app.on_event("startup")
async def init_search_index(): async def init_search_index():
await ensure_tables_exist()
db = dataset.connect(DB_URL) db = dataset.connect(DB_URL)
# Create the FTS5 virtual table over notes.title and notes.body # Create the FTS5 virtual table over notes.title and notes.body
db.query(""" 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) db.query("DELETE FROM notes_fts WHERE note_id = :nid", nid=note_id)
# (Re)insert into FTS table # (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: for t in tags:
t = t.strip() t = t.strip()
if not t: if not t: