Skip to content

Commit 2968598

Browse files
ashu17706claude
andcommitted
fix: initialize QMD store tables (content, documents, vectors) on database creation
When smriti opens a fresh database for the first time, it now properly initializes QMD's store tables (content, documents, content_vectors) and loads the sqlite-vec extension, in addition to memory tables. This fixes the 'no such table: content_vectors' error that occurred when running smriti status in fresh CI environments. - Added initializeQmdStore() to setup all required QMD tables - Content-addressable storage (content table) - Document indexing (documents table) - Vector embeddings support (content_vectors, vectors_vec tables) - Proper sqlite-vec extension loading Fixes Install Test failure on all platforms. Co-Authored-By: Claude Haiku 4.5 <[email protected]>
1 parent 192234f commit 2968598

1 file changed

Lines changed: 57 additions & 5 deletions

File tree

src/db.ts

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,58 @@ import { initializeMemoryTables } from "./qmd";
1818

1919
let _db: Database | null = null;
2020

21+
/** Initialize QMD store tables (content, documents, vectors, etc) */
22+
function initializeQmdStore(db: Database): void {
23+
// Load sqlite-vec extension
24+
sqliteVec.load(db);
25+
db.exec("PRAGMA journal_mode = WAL");
26+
db.exec("PRAGMA foreign_keys = ON");
27+
28+
// Create content-addressable storage
29+
db.exec(`
30+
CREATE TABLE IF NOT EXISTS content (
31+
hash TEXT PRIMARY KEY,
32+
doc TEXT NOT NULL,
33+
created_at TEXT NOT NULL
34+
)
35+
`);
36+
37+
// Documents table
38+
db.exec(`
39+
CREATE TABLE IF NOT EXISTS documents (
40+
id INTEGER PRIMARY KEY AUTOINCREMENT,
41+
collection TEXT NOT NULL,
42+
path TEXT NOT NULL,
43+
title TEXT NOT NULL,
44+
hash TEXT NOT NULL,
45+
created_at TEXT NOT NULL,
46+
modified_at TEXT NOT NULL,
47+
active INTEGER NOT NULL DEFAULT 1,
48+
FOREIGN KEY (hash) REFERENCES content(hash) ON DELETE CASCADE,
49+
UNIQUE(collection, path)
50+
)
51+
`);
52+
53+
// Content vectors - required for vector search
54+
db.exec(`
55+
CREATE TABLE IF NOT EXISTS content_vectors (
56+
hash TEXT NOT NULL,
57+
seq INTEGER NOT NULL DEFAULT 0,
58+
pos INTEGER NOT NULL DEFAULT 0,
59+
model TEXT NOT NULL,
60+
embedded_at TEXT NOT NULL,
61+
PRIMARY KEY (hash, seq)
62+
)
63+
`);
64+
65+
// Create virtual vec table for sqlite-vec
66+
try {
67+
db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS vectors_vec USING vec0(embedding float[1536])`);
68+
} catch {
69+
// May fail if model doesn't support this dimension, that's OK
70+
}
71+
}
72+
2173
/** Get or create the shared database connection */
2274
export function getDb(path?: string): Database {
2375
if (_db) return _db;
@@ -32,10 +84,9 @@ export function getDb(path?: string): Database {
3284
}
3385
}
3486
_db = new Database(dbPath);
35-
_db.exec("PRAGMA journal_mode = WAL");
36-
_db.exec("PRAGMA foreign_keys = ON");
37-
// Load sqlite-vec extension for vector search support
38-
sqliteVec.load(_db);
87+
initializeQmdStore(_db);
88+
// Also initialize QMD memory tables (sessions, messages)
89+
initializeMemoryTables(_db);
3990
return _db;
4091
}
4192

@@ -438,7 +489,8 @@ export function seedDefaults(db: Database): void {
438489
/** Initialize DB, create tables, seed defaults. Returns the DB instance. */
439490
export function initSmriti(dbPath?: string): Database {
440491
const db = getDb(dbPath);
441-
initializeMemoryTables(db);
492+
// getDb() now calls createStore() which initializes QMD tables,
493+
// so we just need to initialize Smriti tables
442494
initializeSmritiTables(db);
443495
seedDefaults(db);
444496
return db;

0 commit comments

Comments
 (0)