MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / initialize

Method initialize

src/db/index.ts:103–160  ·  view source on GitHub ↗

* Initialize a new database at the given path

(dbPath: string)

Source from the content-addressed store, hash-verified

101 * Initialize a new database at the given path
102 */
103 static initialize(dbPath: string): DatabaseConnection {
104 // Ensure parent directory exists
105 const dir = path.dirname(dbPath);
106 if (!fs.existsSync(dir)) {
107 fs.mkdirSync(dir, { recursive: true });
108 }
109
110 // Create and configure database
111 const { db, backend } = createDatabase(dbPath);
112
113 configureConnection(db);
114
115 // Run schema initialization, splitting FTS5 from the rest so
116 // codegraph still works when Node.js was built without FTS5 (#1532).
117 const schemaPath = path.join(__dirname, 'schema.sql');
118 const schema = fs.readFileSync(schemaPath, 'utf-8');
119
120 const FTS5_MARKER = '-- Full-text search index on node names, docstrings, and signatures';
121 const ftsIdx = schema.indexOf(FTS5_MARKER);
122 let fts5Available = true;
123
124 if (ftsIdx >= 0) {
125 const preFts = schema.slice(0, ftsIdx);
126 // FTS ends after the update trigger; required tables and indexes follow
127 // it in schema.sql and must still be created when FTS5 is unavailable.
128 const ftsSection = schema.slice(ftsIdx).match(
129 /^[\s\S]*?CREATE TRIGGER IF NOT EXISTS nodes_au\b[\s\S]*?END;/
130 )?.[0];
131 if (!ftsSection) throw new Error('schema.sql: FTS5 update trigger not found');
132 // Execute everything before FTS5 first
133 db.exec(preFts);
134 // Try FTS5; if it fails, skip it and continue with LIKE-only search
135 try {
136 db.exec(ftsSection);
137 } catch (err: any) {
138 fts5Available = false;
139 const msg = err?.message ?? String(err);
140 console.warn(
141 `[codegraph] FTS5 not available in this Node.js build (${msg}). ` +
142 `Search will fall back to LIKE + fuzzy matching. ` +
143 `For full-text search, use a Node.js build with FTS5 enabled.`
144 );
145 }
146 db.exec(schema.slice(ftsIdx + ftsSection.length));
147 } else {
148 db.exec(schema);
149 }
150
151 // Record current schema version so migrations aren't re-applied on open
152 const currentVersion = getCurrentVersion(db);
153 if (currentVersion < CURRENT_SCHEMA_VERSION) {
154 db.prepare(
155 'INSERT OR IGNORE INTO schema_versions (version, applied_at, description) VALUES (?, ?, ?)'
156 ).run(CURRENT_SCHEMA_VERSION, Date.now(), 'Initial schema includes all migrations');
157 }
158
159 return new DatabaseConnection(db, dbPath, backend, fts5Available);
160 }

Callers 15

initMethod · 0.45
initSyncMethod · 0.45
recreateMethod · 0.45
indexAllMethod · 0.45
reinitializeResolverMethod · 0.45
security.test.tsFile · 0.45
db-perf.test.tsFile · 0.45
initializeFunction · 0.45
wal-heal.test.tsFile · 0.45

Calls 8

createDatabaseFunction · 0.90
getCurrentVersionFunction · 0.90
configureConnectionFunction · 0.85
warnMethod · 0.80
execMethod · 0.65
runMethod · 0.65
prepareMethod · 0.65
joinMethod · 0.45

Tested by 2

initializeFunction · 0.36
openDbFunction · 0.36