2026-07-12 00:28:17 +08:00
|
|
|
// node:sqlite lifecycle and migrations for the Core package.
|
2026-05-31 03:29:41 +08:00
|
|
|
import { createRequire } from 'node:module';
|
2026-07-12 00:37:32 +08:00
|
|
|
import { CLAUDE_DIR, CODEX_DIR, TEXT_LIMIT, trunc, truncJson, extractText, extractContentType, extractMessageIsMeta, filePath, isDir, readLines } from './parsing.ts';
|
2026-07-10 18:10:45 +08:00
|
|
|
import { configureConnection } from './tx.ts';
|
2026-05-31 03:29:41 +08:00
|
|
|
const require = createRequire(import.meta.url);
|
|
|
|
|
const fs = require('node:fs');
|
|
|
|
|
const path = require('node:path');
|
|
|
|
|
const os = require('node:os');
|
|
|
|
|
const { DatabaseSync } = require('node:sqlite');
|
|
|
|
|
|
2026-07-12 00:37:32 +08:00
|
|
|
type SqliteDb = any;
|
|
|
|
|
|
2026-06-17 23:40:36 +08:00
|
|
|
const OBELISK_DIR = path.join(os.homedir(), '.obelisk');
|
|
|
|
|
const LEGACY_DB_PATH = path.join(CLAUDE_DIR, 'obelisk.sqlite');
|
|
|
|
|
const DB_PATH = path.join(OBELISK_DIR, 'obelisk.sqlite');
|
2026-06-13 03:42:01 +08:00
|
|
|
const SCHEMA = fs.readFileSync(new URL('./schema.sql', import.meta.url), 'utf8');
|
2026-05-31 03:29:41 +08:00
|
|
|
|
2026-06-17 23:40:36 +08:00
|
|
|
function migrateLegacyDbIfNeeded() {
|
|
|
|
|
if (fs.existsSync(DB_PATH)) return;
|
|
|
|
|
if (!fs.existsSync(LEGACY_DB_PATH)) return;
|
|
|
|
|
fs.mkdirSync(path.dirname(DB_PATH), { recursive: true });
|
|
|
|
|
fs.copyFileSync(LEGACY_DB_PATH, DB_PATH);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-31 03:29:41 +08:00
|
|
|
function openDb() {
|
2026-06-17 23:40:36 +08:00
|
|
|
migrateLegacyDbIfNeeded();
|
|
|
|
|
fs.mkdirSync(path.dirname(DB_PATH), { recursive: true });
|
2026-05-31 03:29:41 +08:00
|
|
|
const db = new DatabaseSync(DB_PATH);
|
2026-07-10 18:10:45 +08:00
|
|
|
configureConnection(db, { busyTimeoutMs: 250 });
|
2026-06-17 23:40:36 +08:00
|
|
|
migrateExistingColumns(db);
|
2026-05-31 03:29:41 +08:00
|
|
|
db.exec(SCHEMA);
|
2026-06-12 22:20:29 +08:00
|
|
|
migrateDb(db);
|
2026-05-31 03:29:41 +08:00
|
|
|
return db;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-10 18:10:45 +08:00
|
|
|
// Queries and daemon-arbitration checks must never migrate/configure the index.
|
|
|
|
|
// The caller is responsible for ensuring the database exists first.
|
|
|
|
|
function openReadDb() {
|
|
|
|
|
const db = new DatabaseSync(DB_PATH, { readOnly: true });
|
|
|
|
|
db.exec('PRAGMA busy_timeout=250');
|
|
|
|
|
return db;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 00:37:32 +08:00
|
|
|
function openWriterLeaseDb(lockPath: string): SqliteDb {
|
2026-07-10 18:10:45 +08:00
|
|
|
return new DatabaseSync(lockPath);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 00:37:32 +08:00
|
|
|
function ensureColumn(db: SqliteDb, table: string, column: string, definition: string): void {
|
|
|
|
|
const columns = db.prepare(`PRAGMA table_info(${table})`).all().map((c: { name: string }) => c.name);
|
2026-06-12 22:20:29 +08:00
|
|
|
if (!columns.includes(column)) db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${definition}`);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 00:37:32 +08:00
|
|
|
function tableExists(db: SqliteDb, table: string): boolean {
|
2026-06-17 23:40:36 +08:00
|
|
|
return Boolean(db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name=?").get(table));
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 00:37:32 +08:00
|
|
|
function migrateExistingColumns(db: SqliteDb): void {
|
2026-06-17 23:40:36 +08:00
|
|
|
if (tableExists(db, 'sessions')) ensureColumn(db, 'sessions', 'source', "TEXT DEFAULT 'claude'");
|
|
|
|
|
if (tableExists(db, 'messages')) {
|
|
|
|
|
ensureColumn(db, 'messages', 'content_type', 'TEXT');
|
|
|
|
|
ensureColumn(db, 'messages', 'is_meta', 'INTEGER DEFAULT 0');
|
|
|
|
|
ensureColumn(db, 'messages', 'source', "TEXT DEFAULT 'claude'");
|
|
|
|
|
}
|
|
|
|
|
if (tableExists(db, 'memories')) {
|
|
|
|
|
ensureColumn(db, 'memories', 'anchors', 'TEXT');
|
|
|
|
|
ensureColumn(db, 'memories', 'deleted_at', 'TEXT');
|
|
|
|
|
ensureColumn(db, 'memories', 'deleted_reason', 'TEXT');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 00:37:32 +08:00
|
|
|
function migrateDb(db: SqliteDb): void {
|
2026-06-17 23:40:36 +08:00
|
|
|
migrateExistingColumns(db);
|
2026-06-12 22:20:29 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-12 00:37:32 +08:00
|
|
|
function rebuildMemoryFts(db: SqliteDb): void {
|
2026-06-12 22:20:29 +08:00
|
|
|
db.exec("INSERT INTO memories_fts(memories_fts) VALUES('rebuild')");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-31 03:29:41 +08:00
|
|
|
|
2026-07-10 18:10:45 +08:00
|
|
|
export { CLAUDE_DIR, CODEX_DIR, OBELISK_DIR, DB_PATH, TEXT_LIMIT, openDb, openReadDb, openWriterLeaseDb, rebuildMemoryFts, trunc, truncJson, extractText, extractContentType, extractMessageIsMeta, filePath, isDir, readLines, fs, path, os };
|