aboutsummaryrefslogtreecommitdiff
path: root/init_db.py
blob: a401a915dc844be6c125f0bebe85c9505f7bf16b (plain)
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
#!/usr/bin/env python3
from __future__ import annotations

import sqlite3
from pathlib import Path

DB_PATH = Path("cheat.db")


def main() -> None:
    DB_PATH.parent.mkdir(parents=True, exist_ok=True)

    conn = sqlite3.connect(DB_PATH)
    try:
        conn.execute("PRAGMA journal_mode=WAL;")
        conn.execute("PRAGMA foreign_keys=ON;")

        conn.execute("""
        CREATE TABLE IF NOT EXISTS cards (
            id TEXT PRIMARY KEY,
            command TEXT NOT NULL,
            explanation TEXT NOT NULL,
            intent_json TEXT NOT NULL,
            alternatives_json TEXT NOT NULL,
            requires_json TEXT NOT NULL,
            packages_json TEXT NOT NULL,
            tags_json TEXT NOT NULL,
            platform_json TEXT NOT NULL,
            shell_json TEXT NOT NULL,
            safety TEXT NOT NULL,
            search_text TEXT NOT NULL,
            created_at TEXT DEFAULT CURRENT_TIMESTAMP,
            updated_at TEXT DEFAULT CURRENT_TIMESTAMP
        );
        """)

        conn.execute("""
        CREATE TABLE IF NOT EXISTS card_embeddings (
            card_id TEXT PRIMARY KEY,
            model_name TEXT NOT NULL,
            embedding_blob BLOB NOT NULL,
            embedding_dim INTEGER NOT NULL,
            created_at TEXT DEFAULT CURRENT_TIMESTAMP,
            FOREIGN KEY(card_id) REFERENCES cards(id) ON DELETE CASCADE
        );
        """)

        conn.execute("""
        CREATE INDEX IF NOT EXISTS idx_cards_command
        ON cards(command);
        """)

        conn.execute("""
        CREATE INDEX IF NOT EXISTS idx_cards_safety
        ON cards(safety);
        """)

        conn.commit()
        print(f"Initialized database at {DB_PATH}")
    finally:
        conn.close()


if __name__ == "__main__":
    main()