File size: 3,733 Bytes
4f50791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
from __future__ import annotations

import json
import sqlite3
import sys
from pathlib import Path


ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))

from outbush_ai.content import KNOWLEDGE_ITEMS, Source

DB_PATH = ROOT / "data" / "outbush_knowledge.sqlite"


def source_key(source: Source) -> str:
    return source.url


def main() -> int:
    DB_PATH.parent.mkdir(parents=True, exist_ok=True)
    tmp_path = DB_PATH.with_suffix(".sqlite.tmp")
    if tmp_path.exists():
        tmp_path.unlink()
    conn = sqlite3.connect(tmp_path)
    try:
        conn.executescript(
            """
            PRAGMA journal_mode = DELETE;
            PRAGMA foreign_keys = ON;

            CREATE TABLE meta (
                key TEXT PRIMARY KEY,
                value TEXT NOT NULL
            );

            CREATE TABLE sources (
                id INTEGER PRIMARY KEY,
                title TEXT NOT NULL,
                url TEXT NOT NULL UNIQUE,
                retrieved TEXT NOT NULL,
                jurisdiction TEXT NOT NULL,
                category TEXT NOT NULL
            );

            CREATE TABLE knowledge_items (
                key TEXT PRIMARY KEY,
                title TEXT NOT NULL,
                text TEXT NOT NULL,
                source_id INTEGER NOT NULL REFERENCES sources(id),
                tags_json TEXT NOT NULL,
                risk TEXT NOT NULL,
                sort_order INTEGER NOT NULL
            );

            CREATE VIRTUAL TABLE knowledge_fts USING fts5(
                key UNINDEXED,
                title,
                text,
                tags
            );
            """
        )
        conn.executemany(
            "INSERT INTO meta(key, value) VALUES (?, ?)",
            [
                ("format", "outbush-knowledge-v1"),
                ("item_count", str(len(KNOWLEDGE_ITEMS))),
                ("generated_from", "outbush_ai.content.KNOWLEDGE_ITEMS"),
            ],
        )
        source_ids: dict[str, int] = {}
        for item in KNOWLEDGE_ITEMS:
            key = source_key(item.source)
            if key in source_ids:
                continue
            cursor = conn.execute(
                """
                INSERT INTO sources(title, url, retrieved, jurisdiction, category)
                VALUES (?, ?, ?, ?, ?)
                """,
                (
                    item.source.title,
                    item.source.url,
                    item.source.retrieved,
                    item.source.jurisdiction,
                    item.source.category,
                ),
            )
            source_ids[key] = int(cursor.lastrowid)
        for order, item in enumerate(KNOWLEDGE_ITEMS):
            conn.execute(
                """
                INSERT INTO knowledge_items(key, title, text, source_id, tags_json, risk, sort_order)
                VALUES (?, ?, ?, ?, ?, ?, ?)
                """,
                (
                    item.key,
                    item.title,
                    item.text,
                    source_ids[source_key(item.source)],
                    json.dumps(item.tags),
                    item.risk,
                    order,
                ),
            )
            conn.execute(
                """
                INSERT INTO knowledge_fts(key, title, text, tags)
                VALUES (?, ?, ?, ?)
                """,
                (item.key, item.title, item.text, " ".join(item.tags)),
            )
        conn.commit()
    finally:
        conn.close()
    tmp_path.replace(DB_PATH)
    print(f"Wrote {DB_PATH} with {len(KNOWLEDGE_ITEMS)} items")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())