* What `codegraph.db` holds, asked of SQLite itself through a read-only * connection: the codegraph schema, a database without it, not a database at * all (SQLITE_NOTADB), or `unknown` — locked, busy, a WAL db in a directory we * cannot create `-shm` in (read-only checkout, mount, another user's
(dbPath: string)
| 217 | * second connection opened and closed here leaves the first one's locks alone. |
| 218 | */ |
| 219 | function probeSchema(dbPath: string): 'schema' | 'no-schema' | 'not-sqlite' | 'unknown' { |
| 220 | let db: any = null; |
| 221 | try { |
| 222 | // A runtime without node:sqlite can't inspect the file: `unknown`, so the |
| 223 | // command goes on to open it and reports why that is impossible. |
| 224 | const { DatabaseSync } = loadNodeSqlite(); |
| 225 | db = new DatabaseSync(dbPath, { readOnly: true }); |
| 226 | const row = db.prepare("SELECT 1 AS ok FROM sqlite_master WHERE type = 'table' AND name = 'nodes'").get(); |
| 227 | return row !== undefined ? 'schema' : 'no-schema'; |
| 228 | } catch (error) { |
| 229 | return (error as { errcode?: number })?.errcode === SQLITE_NOTADB ? 'not-sqlite' : 'unknown'; |
| 230 | } finally { |
| 231 | // Never hold the handle: Windows file locking would block the owner. |
| 232 | try { db?.close(); } catch { /* already closed */ } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Find the nearest parent directory containing .codegraph/ |
no test coverage detected