-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_fts5.py
More file actions
47 lines (37 loc) · 1.33 KB
/
migrate_fts5.py
File metadata and controls
47 lines (37 loc) · 1.33 KB
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
import sqlite3
DB_PATH = "journal.db"
con = sqlite3.connect(DB_PATH)
cur = con.cursor()
cur.execute("PRAGMA journal_mode=WAL;")
# Drop and recreate only if you're okay resetting the FTS index (not the base table).
# cur.execute("DROP TABLE IF EXISTS entries_fts")
# External-content FTS5 with prefix search (2–3 chars)
cur.execute("""
CREATE VIRTUAL TABLE IF NOT EXISTS entries_fts
USING fts5(
content,
content='entries',
content_rowid='id',
tokenize='porter unicode61',
prefix='2 3'
);
""")
# Triggers: keep FTS in sync with entries
cur.executescript("""
CREATE TRIGGER IF NOT EXISTS entries_ai AFTER INSERT ON entries BEGIN
INSERT INTO entries_fts(rowid, content) VALUES (new.id, new.content);
END;
CREATE TRIGGER IF NOT EXISTS entries_ad AFTER DELETE ON entries BEGIN
INSERT INTO entries_fts(entries_fts, rowid, content) VALUES('delete', old.id, NULL);
END;
CREATE TRIGGER IF NOT EXISTS entries_au AFTER UPDATE ON entries BEGIN
INSERT INTO entries_fts(entries_fts, rowid, content) VALUES('delete', old.id, NULL);
INSERT INTO entries_fts(rowid, content) VALUES (new.id, new.content);
END;
""")
# Canonical backfill for external-content tables
# (Rebuilds the entire index from the 'entries' table)
cur.execute("INSERT INTO entries_fts(entries_fts) VALUES('rebuild');")
con.commit()
con.close()
print("FTS5 migration complete.")