@saluzi/codegraph 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/codegraph.d.ts +25 -0
- package/dist/bin/node-version-check.d.ts +37 -0
- package/dist/bin/uninstall.d.ts +14 -0
- package/dist/context/formatter.d.ts +33 -0
- package/dist/context/formatter.js +244 -0
- package/dist/context/index.d.ts +117 -0
- package/dist/context/index.js +1050 -0
- package/dist/db/index.d.ts +101 -0
- package/dist/db/index.js +250 -0
- package/dist/db/migrations.d.ts +47 -0
- package/dist/db/migrations.js +131 -0
- package/dist/db/queries.d.ts +291 -0
- package/dist/db/queries.js +1349 -0
- package/dist/db/schema.sql +151 -0
- package/dist/db/sqlite-adapter.d.ts +49 -0
- package/dist/db/sqlite-adapter.js +141 -0
- package/dist/directory.d.ts +62 -0
- package/dist/directory.js +264 -0
- package/dist/errors.d.ts +149 -0
- package/dist/errors.js +219 -0
- package/dist/extraction/dfm-extractor.d.ts +31 -0
- package/dist/extraction/dfm-extractor.js +151 -0
- package/dist/extraction/grammars.d.ts +94 -0
- package/dist/extraction/grammars.js +357 -0
- package/dist/extraction/index.d.ts +148 -0
- package/dist/extraction/index.js +1286 -0
- package/dist/extraction/languages/c-cpp.d.ts +4 -0
- package/dist/extraction/languages/c-cpp.js +126 -0
- package/dist/extraction/languages/csharp.d.ts +3 -0
- package/dist/extraction/languages/csharp.js +72 -0
- package/dist/extraction/languages/dart.d.ts +3 -0
- package/dist/extraction/languages/dart.js +192 -0
- package/dist/extraction/languages/go.d.ts +3 -0
- package/dist/extraction/languages/go.js +58 -0
- package/dist/extraction/languages/index.d.ts +10 -0
- package/dist/extraction/languages/index.js +49 -0
- package/dist/extraction/languages/java.d.ts +3 -0
- package/dist/extraction/languages/java.js +64 -0
- package/dist/extraction/languages/javascript.d.ts +3 -0
- package/dist/extraction/languages/javascript.js +90 -0
- package/dist/extraction/languages/kotlin.d.ts +3 -0
- package/dist/extraction/languages/kotlin.js +253 -0
- package/dist/extraction/languages/lua.d.ts +3 -0
- package/dist/extraction/languages/lua.js +150 -0
- package/dist/extraction/languages/luau.d.ts +3 -0
- package/dist/extraction/languages/luau.js +37 -0
- package/dist/extraction/languages/pascal.d.ts +3 -0
- package/dist/extraction/languages/pascal.js +66 -0
- package/dist/extraction/languages/php.d.ts +3 -0
- package/dist/extraction/languages/php.js +107 -0
- package/dist/extraction/languages/python.d.ts +3 -0
- package/dist/extraction/languages/python.js +56 -0
- package/dist/extraction/languages/ruby.d.ts +3 -0
- package/dist/extraction/languages/ruby.js +114 -0
- package/dist/extraction/languages/rust.d.ts +3 -0
- package/dist/extraction/languages/rust.js +109 -0
- package/dist/extraction/languages/scala.d.ts +3 -0
- package/dist/extraction/languages/scala.js +139 -0
- package/dist/extraction/languages/swift.d.ts +3 -0
- package/dist/extraction/languages/swift.js +91 -0
- package/dist/extraction/languages/typescript.d.ts +3 -0
- package/dist/extraction/languages/typescript.js +129 -0
- package/dist/extraction/liquid-extractor.d.ts +52 -0
- package/dist/extraction/liquid-extractor.js +313 -0
- package/dist/extraction/parse-worker.d.ts +8 -0
- package/dist/extraction/parse-worker.js +94 -0
- package/dist/extraction/svelte-extractor.d.ts +56 -0
- package/dist/extraction/svelte-extractor.js +272 -0
- package/dist/extraction/tree-sitter-helpers.d.ts +39 -0
- package/dist/extraction/tree-sitter-helpers.js +103 -0
- package/dist/extraction/tree-sitter-types.d.ts +191 -0
- package/dist/extraction/tree-sitter-types.js +10 -0
- package/dist/extraction/tree-sitter.d.ts +238 -0
- package/dist/extraction/tree-sitter.js +2430 -0
- package/dist/extraction/vue-extractor.d.ts +36 -0
- package/dist/extraction/vue-extractor.js +163 -0
- package/dist/extraction/wasm/tree-sitter-c.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-c_sharp.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-cpp.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-dart.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-go.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-java.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-javascript.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-kotlin.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-lua.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-luau.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-pascal.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-php.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-python.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-ruby.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-rust.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-scala.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-swift.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-tsx.wasm +0 -0
- package/dist/extraction/wasm/tree-sitter-typescript.wasm +0 -0
- package/dist/extraction/wasm-runtime-flags.d.ts +46 -0
- package/dist/extraction/wasm-runtime-flags.js +105 -0
- package/dist/graph/index.d.ts +8 -0
- package/dist/graph/index.js +13 -0
- package/dist/graph/queries.d.ts +109 -0
- package/dist/graph/queries.js +366 -0
- package/dist/graph/traversal.d.ts +137 -0
- package/dist/graph/traversal.js +528 -0
- package/dist/index.d.ts +509 -0
- package/dist/index.js +800 -0
- package/dist/installer/claude-md-template.d.ts +19 -0
- package/dist/installer/config-writer.d.ts +29 -0
- package/dist/installer/index.d.ts +140 -0
- package/dist/installer/instructions-template.d.ts +30 -0
- package/dist/installer/targets/claude.d.ts +55 -0
- package/dist/installer/targets/codex.d.ts +18 -0
- package/dist/installer/targets/cursor.d.ts +35 -0
- package/dist/installer/targets/hermes.d.ts +18 -0
- package/dist/installer/targets/opencode.d.ts +30 -0
- package/dist/installer/targets/registry.d.ts +38 -0
- package/dist/installer/targets/shared.d.ts +92 -0
- package/dist/installer/targets/toml.d.ts +64 -0
- package/dist/installer/targets/types.d.ts +122 -0
- package/dist/mcp/index.d.ts +98 -0
- package/dist/mcp/server-instructions.d.ts +20 -0
- package/dist/mcp/tools.d.ts +269 -0
- package/dist/mcp/transport.d.ts +117 -0
- package/dist/resolution/frameworks/cargo-workspace.d.ts +20 -0
- package/dist/resolution/frameworks/cargo-workspace.js +225 -0
- package/dist/resolution/frameworks/csharp.d.ts +8 -0
- package/dist/resolution/frameworks/csharp.js +213 -0
- package/dist/resolution/frameworks/drupal.d.ts +51 -0
- package/dist/resolution/frameworks/drupal.js +335 -0
- package/dist/resolution/frameworks/express.d.ts +8 -0
- package/dist/resolution/frameworks/express.js +225 -0
- package/dist/resolution/frameworks/go.d.ts +8 -0
- package/dist/resolution/frameworks/go.js +158 -0
- package/dist/resolution/frameworks/index.d.ts +52 -0
- package/dist/resolution/frameworks/index.js +137 -0
- package/dist/resolution/frameworks/java.d.ts +8 -0
- package/dist/resolution/frameworks/java.js +177 -0
- package/dist/resolution/frameworks/laravel.d.ts +13 -0
- package/dist/resolution/frameworks/laravel.js +248 -0
- package/dist/resolution/frameworks/nestjs.d.ts +26 -0
- package/dist/resolution/frameworks/nestjs.js +374 -0
- package/dist/resolution/frameworks/python.d.ts +10 -0
- package/dist/resolution/frameworks/python.js +278 -0
- package/dist/resolution/frameworks/react.d.ts +8 -0
- package/dist/resolution/frameworks/react.js +272 -0
- package/dist/resolution/frameworks/ruby.d.ts +8 -0
- package/dist/resolution/frameworks/ruby.js +198 -0
- package/dist/resolution/frameworks/rust.d.ts +8 -0
- package/dist/resolution/frameworks/rust.js +207 -0
- package/dist/resolution/frameworks/svelte.d.ts +9 -0
- package/dist/resolution/frameworks/svelte.js +249 -0
- package/dist/resolution/frameworks/swift.d.ts +10 -0
- package/dist/resolution/frameworks/swift.js +376 -0
- package/dist/resolution/frameworks/vue.d.ts +9 -0
- package/dist/resolution/frameworks/vue.js +306 -0
- package/dist/resolution/import-resolver.d.ts +61 -0
- package/dist/resolution/import-resolver.js +663 -0
- package/dist/resolution/index.d.ts +118 -0
- package/dist/resolution/index.js +744 -0
- package/dist/resolution/lru-cache.d.ts +24 -0
- package/dist/resolution/lru-cache.js +62 -0
- package/dist/resolution/name-matcher.d.ts +50 -0
- package/dist/resolution/name-matcher.js +384 -0
- package/dist/resolution/path-aliases.d.ts +72 -0
- package/dist/resolution/path-aliases.js +238 -0
- package/dist/resolution/strip-comments.d.ts +40 -0
- package/dist/resolution/strip-comments.js +441 -0
- package/dist/resolution/types.d.ts +181 -0
- package/dist/resolution/types.js +8 -0
- package/dist/search/query-parser.d.ts +61 -0
- package/dist/search/query-parser.js +177 -0
- package/dist/search/query-utils.d.ts +59 -0
- package/dist/search/query-utils.js +383 -0
- package/dist/sync/git-hooks.d.ts +54 -0
- package/dist/sync/git-hooks.js +223 -0
- package/dist/sync/index.d.ts +25 -0
- package/dist/sync/index.js +28 -0
- package/dist/sync/watch-policy.d.ts +51 -0
- package/dist/sync/watch-policy.js +124 -0
- package/dist/sync/watcher.d.ts +83 -0
- package/dist/sync/watcher.js +192 -0
- package/dist/types.d.ts +433 -0
- package/dist/types.js +75 -0
- package/dist/ui/glyphs.d.ts +42 -0
- package/dist/ui/shimmer-progress.d.ts +11 -0
- package/dist/ui/shimmer-worker.d.ts +2 -0
- package/dist/ui/types.d.ts +20 -0
- package/dist/utils.d.ts +231 -0
- package/dist/utils.js +549 -0
- package/package.json +24 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database Layer
|
|
3
|
+
*
|
|
4
|
+
* Handles SQLite database initialization and connection management.
|
|
5
|
+
*/
|
|
6
|
+
import { SqliteDatabase, SqliteBackend } from './sqlite-adapter'
|
|
7
|
+
import { SchemaVersion } from '../types'
|
|
8
|
+
export { SqliteDatabase, SqliteBackend } from './sqlite-adapter'
|
|
9
|
+
/**
|
|
10
|
+
* Database connection wrapper with lifecycle management
|
|
11
|
+
*/
|
|
12
|
+
export declare class DatabaseConnection {
|
|
13
|
+
private db
|
|
14
|
+
private dbPath
|
|
15
|
+
private backend
|
|
16
|
+
private constructor()
|
|
17
|
+
/**
|
|
18
|
+
* Initialize a new database at the given path
|
|
19
|
+
*/
|
|
20
|
+
static initialize(dbPath: string): DatabaseConnection
|
|
21
|
+
/**
|
|
22
|
+
* Open an existing database
|
|
23
|
+
*/
|
|
24
|
+
static open(dbPath: string): DatabaseConnection
|
|
25
|
+
/**
|
|
26
|
+
* Get the underlying database instance
|
|
27
|
+
*/
|
|
28
|
+
getDb(): SqliteDatabase
|
|
29
|
+
/**
|
|
30
|
+
* Get the SQLite backend serving this connection. Per-instance so
|
|
31
|
+
* MCP cross-project queries report the right backend even when
|
|
32
|
+
* multiple project DBs are open in the same process.
|
|
33
|
+
*/
|
|
34
|
+
getBackend(): SqliteBackend
|
|
35
|
+
/**
|
|
36
|
+
* Get database file path
|
|
37
|
+
*/
|
|
38
|
+
getPath(): string
|
|
39
|
+
/**
|
|
40
|
+
* The journal mode actually in effect (e.g. 'wal', 'delete').
|
|
41
|
+
*
|
|
42
|
+
* SQLite silently keeps the prior mode if WAL can't be enabled — e.g. on
|
|
43
|
+
* filesystems without shared-memory support (some network/virtualized mounts,
|
|
44
|
+
* WSL2 /mnt), and always on the wasm backend. So the effective mode can differ
|
|
45
|
+
* from what `configureConnection` requested. Surfaced in `codegraph status` so
|
|
46
|
+
* a "database is locked" report is triageable: 'wal' ⇒ readers never block on a
|
|
47
|
+
* writer; anything else ⇒ they can. See issue #238.
|
|
48
|
+
*/
|
|
49
|
+
getJournalMode(): string
|
|
50
|
+
/**
|
|
51
|
+
* Get current schema version
|
|
52
|
+
*/
|
|
53
|
+
getSchemaVersion(): SchemaVersion | null
|
|
54
|
+
/**
|
|
55
|
+
* Execute a function within a transaction
|
|
56
|
+
*/
|
|
57
|
+
transaction<T>(fn: () => T): T
|
|
58
|
+
/**
|
|
59
|
+
* Get database file size in bytes
|
|
60
|
+
*/
|
|
61
|
+
getSize(): number
|
|
62
|
+
/**
|
|
63
|
+
* Optimize database (vacuum and analyze)
|
|
64
|
+
*/
|
|
65
|
+
optimize(): void
|
|
66
|
+
/**
|
|
67
|
+
* Lightweight, non-blocking maintenance to run after bulk writes
|
|
68
|
+
* (indexAll, sync). Two operations:
|
|
69
|
+
*
|
|
70
|
+
* - `PRAGMA optimize` — incremental ANALYZE; SQLite only re-analyzes
|
|
71
|
+
* tables whose row counts changed materially since the last
|
|
72
|
+
* ANALYZE. Without it, the query planner has no statistics on the
|
|
73
|
+
* freshly-bulk-loaded tables and can pick suboptimal indexes.
|
|
74
|
+
*
|
|
75
|
+
* - `PRAGMA wal_checkpoint(PASSIVE)` — fold pending WAL pages back
|
|
76
|
+
* into the main database file so the WAL file doesn't grow
|
|
77
|
+
* unboundedly between automatic checkpoints (auto-fires at 1000
|
|
78
|
+
* pages by default; large indexAll runs blow past that).
|
|
79
|
+
*
|
|
80
|
+
* Both operations are silently swallowed on failure — they're a
|
|
81
|
+
* best-effort optimization, never load-bearing for correctness.
|
|
82
|
+
*/
|
|
83
|
+
runMaintenance(): void
|
|
84
|
+
/**
|
|
85
|
+
* Close the database connection
|
|
86
|
+
*/
|
|
87
|
+
close(): void
|
|
88
|
+
/**
|
|
89
|
+
* Check if the database connection is open
|
|
90
|
+
*/
|
|
91
|
+
isOpen(): boolean
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Default database filename
|
|
95
|
+
*/
|
|
96
|
+
export declare const DATABASE_FILENAME = 'codegraph.db'
|
|
97
|
+
/**
|
|
98
|
+
* Get the default database path for a project
|
|
99
|
+
*/
|
|
100
|
+
export declare function getDatabasePath(projectRoot: string): string
|
|
101
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/db/index.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Database Layer
|
|
4
|
+
*
|
|
5
|
+
* Handles SQLite database initialization and connection management.
|
|
6
|
+
*/
|
|
7
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
+
if (k2 === undefined) k2 = k;
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
14
|
+
}) : (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
}));
|
|
18
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
+
}) : function(o, v) {
|
|
21
|
+
o["default"] = v;
|
|
22
|
+
});
|
|
23
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
+
var ownKeys = function(o) {
|
|
25
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
+
var ar = [];
|
|
27
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
+
return ar;
|
|
29
|
+
};
|
|
30
|
+
return ownKeys(o);
|
|
31
|
+
};
|
|
32
|
+
return function (mod) {
|
|
33
|
+
if (mod && mod.__esModule) return mod;
|
|
34
|
+
var result = {};
|
|
35
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
+
__setModuleDefault(result, mod);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
})();
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.DATABASE_FILENAME = exports.DatabaseConnection = void 0;
|
|
42
|
+
exports.getDatabasePath = getDatabasePath;
|
|
43
|
+
const sqlite_adapter_1 = require("./sqlite-adapter");
|
|
44
|
+
const fs = __importStar(require("fs"));
|
|
45
|
+
const path = __importStar(require("path"));
|
|
46
|
+
const migrations_1 = require("./migrations");
|
|
47
|
+
/**
|
|
48
|
+
* Apply connection-level PRAGMAs. Shared by `initialize` and `open` so the two
|
|
49
|
+
* paths can't drift.
|
|
50
|
+
*
|
|
51
|
+
* `busy_timeout` is set FIRST, before any pragma that might touch the database
|
|
52
|
+
* file (notably `journal_mode`). If another process holds a write lock at open
|
|
53
|
+
* time, the later pragmas — and the connection's first query — then wait out
|
|
54
|
+
* the lock instead of throwing "database is locked" immediately. See issue #238.
|
|
55
|
+
*
|
|
56
|
+
* The 5s window (was 120s) rides out a normal incremental sync; the old
|
|
57
|
+
* 2-minute wait presented as a frozen, hung agent. With WAL, reads never block
|
|
58
|
+
* on a writer, so this timeout only governs cross-process write contention
|
|
59
|
+
* (e.g. the git-hook `codegraph sync` running while the MCP server writes).
|
|
60
|
+
*/
|
|
61
|
+
function configureConnection(db) {
|
|
62
|
+
db.pragma('busy_timeout = 5000'); // MUST be first — see above
|
|
63
|
+
db.pragma('foreign_keys = ON');
|
|
64
|
+
db.pragma('journal_mode = WAL'); // node:sqlite supports WAL on every platform
|
|
65
|
+
db.pragma('synchronous = NORMAL'); // safe with WAL mode
|
|
66
|
+
db.pragma('cache_size = -64000'); // 64 MB page cache
|
|
67
|
+
db.pragma('temp_store = MEMORY'); // temp tables in memory
|
|
68
|
+
db.pragma('mmap_size = 268435456'); // 256 MB memory-mapped I/O
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Database connection wrapper with lifecycle management
|
|
72
|
+
*/
|
|
73
|
+
class DatabaseConnection {
|
|
74
|
+
db;
|
|
75
|
+
dbPath;
|
|
76
|
+
backend;
|
|
77
|
+
constructor(db, dbPath, backend) {
|
|
78
|
+
this.db = db;
|
|
79
|
+
this.dbPath = dbPath;
|
|
80
|
+
this.backend = backend;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Initialize a new database at the given path
|
|
84
|
+
*/
|
|
85
|
+
static initialize(dbPath) {
|
|
86
|
+
// Ensure parent directory exists
|
|
87
|
+
const dir = path.dirname(dbPath);
|
|
88
|
+
if (!fs.existsSync(dir)) {
|
|
89
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
90
|
+
}
|
|
91
|
+
// Create and configure database
|
|
92
|
+
const { db, backend } = (0, sqlite_adapter_1.createDatabase)(dbPath);
|
|
93
|
+
configureConnection(db);
|
|
94
|
+
// Run schema initialization
|
|
95
|
+
const schemaPath = path.join(__dirname, 'schema.sql');
|
|
96
|
+
const schema = fs.readFileSync(schemaPath, 'utf-8');
|
|
97
|
+
db.exec(schema);
|
|
98
|
+
// Record current schema version so migrations aren't re-applied on open
|
|
99
|
+
const currentVersion = (0, migrations_1.getCurrentVersion)(db);
|
|
100
|
+
if (currentVersion < migrations_1.CURRENT_SCHEMA_VERSION) {
|
|
101
|
+
db.prepare('INSERT OR IGNORE INTO schema_versions (version, applied_at, description) VALUES (?, ?, ?)').run(migrations_1.CURRENT_SCHEMA_VERSION, Date.now(), 'Initial schema includes all migrations');
|
|
102
|
+
}
|
|
103
|
+
return new DatabaseConnection(db, dbPath, backend);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Open an existing database
|
|
107
|
+
*/
|
|
108
|
+
static open(dbPath) {
|
|
109
|
+
if (!fs.existsSync(dbPath)) {
|
|
110
|
+
throw new Error(`Database not found: ${dbPath}`);
|
|
111
|
+
}
|
|
112
|
+
const { db, backend } = (0, sqlite_adapter_1.createDatabase)(dbPath);
|
|
113
|
+
configureConnection(db);
|
|
114
|
+
// Check and run migrations if needed
|
|
115
|
+
const conn = new DatabaseConnection(db, dbPath, backend);
|
|
116
|
+
const currentVersion = (0, migrations_1.getCurrentVersion)(db);
|
|
117
|
+
if (currentVersion < migrations_1.CURRENT_SCHEMA_VERSION) {
|
|
118
|
+
(0, migrations_1.runMigrations)(db, currentVersion);
|
|
119
|
+
}
|
|
120
|
+
return conn;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Get the underlying database instance
|
|
124
|
+
*/
|
|
125
|
+
getDb() {
|
|
126
|
+
return this.db;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get the SQLite backend serving this connection. Per-instance so
|
|
130
|
+
* MCP cross-project queries report the right backend even when
|
|
131
|
+
* multiple project DBs are open in the same process.
|
|
132
|
+
*/
|
|
133
|
+
getBackend() {
|
|
134
|
+
return this.backend;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Get database file path
|
|
138
|
+
*/
|
|
139
|
+
getPath() {
|
|
140
|
+
return this.dbPath;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The journal mode actually in effect (e.g. 'wal', 'delete').
|
|
144
|
+
*
|
|
145
|
+
* SQLite silently keeps the prior mode if WAL can't be enabled — e.g. on
|
|
146
|
+
* filesystems without shared-memory support (some network/virtualized mounts,
|
|
147
|
+
* WSL2 /mnt), and always on the wasm backend. So the effective mode can differ
|
|
148
|
+
* from what `configureConnection` requested. Surfaced in `codegraph status` so
|
|
149
|
+
* a "database is locked" report is triageable: 'wal' ⇒ readers never block on a
|
|
150
|
+
* writer; anything else ⇒ they can. See issue #238.
|
|
151
|
+
*/
|
|
152
|
+
getJournalMode() {
|
|
153
|
+
const raw = this.db.pragma('journal_mode');
|
|
154
|
+
const row = Array.isArray(raw) ? raw[0] : raw;
|
|
155
|
+
const mode = row && typeof row === 'object'
|
|
156
|
+
? row.journal_mode
|
|
157
|
+
: row;
|
|
158
|
+
return String(mode ?? '').toLowerCase();
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Get current schema version
|
|
162
|
+
*/
|
|
163
|
+
getSchemaVersion() {
|
|
164
|
+
const row = this.db
|
|
165
|
+
.prepare('SELECT version, applied_at, description FROM schema_versions ORDER BY version DESC LIMIT 1')
|
|
166
|
+
.get();
|
|
167
|
+
if (!row)
|
|
168
|
+
return null;
|
|
169
|
+
return {
|
|
170
|
+
version: row.version,
|
|
171
|
+
appliedAt: row.applied_at,
|
|
172
|
+
description: row.description ?? undefined,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Execute a function within a transaction
|
|
177
|
+
*/
|
|
178
|
+
transaction(fn) {
|
|
179
|
+
return this.db.transaction(fn)();
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Get database file size in bytes
|
|
183
|
+
*/
|
|
184
|
+
getSize() {
|
|
185
|
+
const stats = fs.statSync(this.dbPath);
|
|
186
|
+
return stats.size;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Optimize database (vacuum and analyze)
|
|
190
|
+
*/
|
|
191
|
+
optimize() {
|
|
192
|
+
this.db.exec('VACUUM');
|
|
193
|
+
this.db.exec('ANALYZE');
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Lightweight, non-blocking maintenance to run after bulk writes
|
|
197
|
+
* (indexAll, sync). Two operations:
|
|
198
|
+
*
|
|
199
|
+
* - `PRAGMA optimize` — incremental ANALYZE; SQLite only re-analyzes
|
|
200
|
+
* tables whose row counts changed materially since the last
|
|
201
|
+
* ANALYZE. Without it, the query planner has no statistics on the
|
|
202
|
+
* freshly-bulk-loaded tables and can pick suboptimal indexes.
|
|
203
|
+
*
|
|
204
|
+
* - `PRAGMA wal_checkpoint(PASSIVE)` — fold pending WAL pages back
|
|
205
|
+
* into the main database file so the WAL file doesn't grow
|
|
206
|
+
* unboundedly between automatic checkpoints (auto-fires at 1000
|
|
207
|
+
* pages by default; large indexAll runs blow past that).
|
|
208
|
+
*
|
|
209
|
+
* Both operations are silently swallowed on failure — they're a
|
|
210
|
+
* best-effort optimization, never load-bearing for correctness.
|
|
211
|
+
*/
|
|
212
|
+
runMaintenance() {
|
|
213
|
+
try {
|
|
214
|
+
this.db.exec('PRAGMA optimize');
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
// ignore
|
|
218
|
+
}
|
|
219
|
+
try {
|
|
220
|
+
this.db.exec('PRAGMA wal_checkpoint(PASSIVE)');
|
|
221
|
+
}
|
|
222
|
+
catch {
|
|
223
|
+
// ignore (e.g., not in WAL mode)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Close the database connection
|
|
228
|
+
*/
|
|
229
|
+
close() {
|
|
230
|
+
this.db.close();
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Check if the database connection is open
|
|
234
|
+
*/
|
|
235
|
+
isOpen() {
|
|
236
|
+
return this.db.open;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
exports.DatabaseConnection = DatabaseConnection;
|
|
240
|
+
/**
|
|
241
|
+
* Default database filename
|
|
242
|
+
*/
|
|
243
|
+
exports.DATABASE_FILENAME = 'codegraph.db';
|
|
244
|
+
/**
|
|
245
|
+
* Get the default database path for a project
|
|
246
|
+
*/
|
|
247
|
+
function getDatabasePath(projectRoot) {
|
|
248
|
+
return path.join(projectRoot, '.codegraph', exports.DATABASE_FILENAME);
|
|
249
|
+
}
|
|
250
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database Migrations
|
|
3
|
+
*
|
|
4
|
+
* Schema versioning and migration support.
|
|
5
|
+
*/
|
|
6
|
+
import { SqliteDatabase } from './sqlite-adapter'
|
|
7
|
+
/**
|
|
8
|
+
* Current schema version
|
|
9
|
+
*/
|
|
10
|
+
export declare const CURRENT_SCHEMA_VERSION = 4
|
|
11
|
+
/**
|
|
12
|
+
* Migration definition
|
|
13
|
+
*/
|
|
14
|
+
interface Migration {
|
|
15
|
+
version: number
|
|
16
|
+
description: string
|
|
17
|
+
up: (db: SqliteDatabase) => void
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Get the current schema version from the database
|
|
21
|
+
*/
|
|
22
|
+
export declare function getCurrentVersion(db: SqliteDatabase): number
|
|
23
|
+
/**
|
|
24
|
+
* Run all pending migrations
|
|
25
|
+
*/
|
|
26
|
+
export declare function runMigrations(
|
|
27
|
+
db: SqliteDatabase,
|
|
28
|
+
fromVersion: number,
|
|
29
|
+
): void
|
|
30
|
+
/**
|
|
31
|
+
* Check if the database needs migration
|
|
32
|
+
*/
|
|
33
|
+
export declare function needsMigration(db: SqliteDatabase): boolean
|
|
34
|
+
/**
|
|
35
|
+
* Get list of pending migrations
|
|
36
|
+
*/
|
|
37
|
+
export declare function getPendingMigrations(db: SqliteDatabase): Migration[]
|
|
38
|
+
/**
|
|
39
|
+
* Get migration history from database
|
|
40
|
+
*/
|
|
41
|
+
export declare function getMigrationHistory(db: SqliteDatabase): Array<{
|
|
42
|
+
version: number
|
|
43
|
+
appliedAt: number
|
|
44
|
+
description: string | null
|
|
45
|
+
}>
|
|
46
|
+
export {}
|
|
47
|
+
//# sourceMappingURL=migrations.d.ts.map
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Database Migrations
|
|
4
|
+
*
|
|
5
|
+
* Schema versioning and migration support.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.CURRENT_SCHEMA_VERSION = void 0;
|
|
9
|
+
exports.getCurrentVersion = getCurrentVersion;
|
|
10
|
+
exports.runMigrations = runMigrations;
|
|
11
|
+
exports.needsMigration = needsMigration;
|
|
12
|
+
exports.getPendingMigrations = getPendingMigrations;
|
|
13
|
+
exports.getMigrationHistory = getMigrationHistory;
|
|
14
|
+
/**
|
|
15
|
+
* Current schema version
|
|
16
|
+
*/
|
|
17
|
+
exports.CURRENT_SCHEMA_VERSION = 4;
|
|
18
|
+
/**
|
|
19
|
+
* All migrations in order
|
|
20
|
+
*
|
|
21
|
+
* Note: Version 1 is the initial schema, handled by schema.sql
|
|
22
|
+
* Future migrations go here.
|
|
23
|
+
*/
|
|
24
|
+
const migrations = [
|
|
25
|
+
{
|
|
26
|
+
version: 2,
|
|
27
|
+
description: 'Add project metadata, provenance tracking, and unresolved ref context',
|
|
28
|
+
up: (db) => {
|
|
29
|
+
db.exec(`
|
|
30
|
+
CREATE TABLE IF NOT EXISTS project_metadata (
|
|
31
|
+
key TEXT PRIMARY KEY,
|
|
32
|
+
value TEXT NOT NULL,
|
|
33
|
+
updated_at INTEGER NOT NULL
|
|
34
|
+
);
|
|
35
|
+
ALTER TABLE unresolved_refs ADD COLUMN file_path TEXT NOT NULL DEFAULT '';
|
|
36
|
+
ALTER TABLE unresolved_refs ADD COLUMN language TEXT NOT NULL DEFAULT 'unknown';
|
|
37
|
+
ALTER TABLE edges ADD COLUMN provenance TEXT DEFAULT NULL;
|
|
38
|
+
CREATE INDEX IF NOT EXISTS idx_unresolved_file_path ON unresolved_refs(file_path);
|
|
39
|
+
CREATE INDEX IF NOT EXISTS idx_edges_provenance ON edges(provenance);
|
|
40
|
+
`);
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
version: 3,
|
|
45
|
+
description: 'Add lower(name) expression index for memory-efficient case-insensitive lookups',
|
|
46
|
+
up: (db) => {
|
|
47
|
+
db.exec(`
|
|
48
|
+
CREATE INDEX IF NOT EXISTS idx_nodes_lower_name ON nodes(lower(name));
|
|
49
|
+
`);
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
version: 4,
|
|
54
|
+
description: 'Drop redundant idx_edges_source / idx_edges_target (covered by source_kind / target_kind composites)',
|
|
55
|
+
up: (db) => {
|
|
56
|
+
db.exec(`
|
|
57
|
+
DROP INDEX IF EXISTS idx_edges_source;
|
|
58
|
+
DROP INDEX IF EXISTS idx_edges_target;
|
|
59
|
+
`);
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
/**
|
|
64
|
+
* Get the current schema version from the database
|
|
65
|
+
*/
|
|
66
|
+
function getCurrentVersion(db) {
|
|
67
|
+
try {
|
|
68
|
+
const row = db
|
|
69
|
+
.prepare('SELECT MAX(version) as version FROM schema_versions')
|
|
70
|
+
.get();
|
|
71
|
+
return row?.version ?? 0;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
// Table doesn't exist yet
|
|
75
|
+
return 0;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Record a migration as applied
|
|
80
|
+
*/
|
|
81
|
+
function recordMigration(db, version, description) {
|
|
82
|
+
db.prepare('INSERT INTO schema_versions (version, applied_at, description) VALUES (?, ?, ?)').run(version, Date.now(), description);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Run all pending migrations
|
|
86
|
+
*/
|
|
87
|
+
function runMigrations(db, fromVersion) {
|
|
88
|
+
const pending = migrations.filter((m) => m.version > fromVersion);
|
|
89
|
+
if (pending.length === 0) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
// Sort by version
|
|
93
|
+
pending.sort((a, b) => a.version - b.version);
|
|
94
|
+
// Run each migration in a transaction
|
|
95
|
+
for (const migration of pending) {
|
|
96
|
+
db.transaction(() => {
|
|
97
|
+
migration.up(db);
|
|
98
|
+
recordMigration(db, migration.version, migration.description);
|
|
99
|
+
})();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Check if the database needs migration
|
|
104
|
+
*/
|
|
105
|
+
function needsMigration(db) {
|
|
106
|
+
const current = getCurrentVersion(db);
|
|
107
|
+
return current < exports.CURRENT_SCHEMA_VERSION;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Get list of pending migrations
|
|
111
|
+
*/
|
|
112
|
+
function getPendingMigrations(db) {
|
|
113
|
+
const current = getCurrentVersion(db);
|
|
114
|
+
return migrations
|
|
115
|
+
.filter((m) => m.version > current)
|
|
116
|
+
.sort((a, b) => a.version - b.version);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Get migration history from database
|
|
120
|
+
*/
|
|
121
|
+
function getMigrationHistory(db) {
|
|
122
|
+
const rows = db
|
|
123
|
+
.prepare('SELECT version, applied_at, description FROM schema_versions ORDER BY version')
|
|
124
|
+
.all();
|
|
125
|
+
return rows.map((row) => ({
|
|
126
|
+
version: row.version,
|
|
127
|
+
appliedAt: row.applied_at,
|
|
128
|
+
description: row.description,
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=migrations.js.map
|