The rest of initialize: configure the connection and create the schema.
(db: SqliteDatabase, backend: SqliteBackend, dbPath: string)
| 135 | |
| 136 | /** The rest of {@link initialize}: configure the connection and create the schema. */ |
| 137 | private static initializeOn(db: SqliteDatabase, backend: SqliteBackend, dbPath: string): DatabaseConnection { |
| 138 | configureConnection(db); |
| 139 | |
| 140 | // Run schema initialization, splitting FTS5 from the rest so |
| 141 | // codegraph still works when Node.js was built without FTS5 (#1532). |
| 142 | const schemaPath = path.join(__dirname, 'schema.sql'); |
| 143 | const schema = fs.readFileSync(schemaPath, 'utf-8'); |
| 144 | |
| 145 | const FTS5_MARKER = '-- Full-text search index on node names, docstrings, and signatures'; |
| 146 | const ftsIdx = schema.indexOf(FTS5_MARKER); |
| 147 | let fts5Available = true; |
| 148 | |
| 149 | if (ftsIdx >= 0) { |
| 150 | const preFts = schema.slice(0, ftsIdx); |
| 151 | // FTS ends after the update trigger; required tables and indexes follow |
| 152 | // it in schema.sql and must still be created when FTS5 is unavailable. |
| 153 | const ftsSection = schema.slice(ftsIdx).match( |
| 154 | /^[\s\S]*?CREATE TRIGGER IF NOT EXISTS nodes_au\b[\s\S]*?END;/ |
| 155 | )?.[0]; |
| 156 | if (!ftsSection) throw new Error('schema.sql: FTS5 update trigger not found'); |
| 157 | // Execute everything before FTS5 first |
| 158 | db.exec(preFts); |
| 159 | // Try FTS5; if it fails, skip it and continue with LIKE-only search |
| 160 | try { |
| 161 | db.exec(ftsSection); |
| 162 | } catch (err: any) { |
| 163 | fts5Available = false; |
| 164 | const msg = err?.message ?? String(err); |
| 165 | console.warn( |
| 166 | `[codegraph] FTS5 not available in this Node.js build (${msg}). ` + |
| 167 | `Search will fall back to LIKE + fuzzy matching. ` + |
| 168 | `For full-text search, use a Node.js build with FTS5 enabled.` |
| 169 | ); |
| 170 | } |
| 171 | db.exec(schema.slice(ftsIdx + ftsSection.length)); |
| 172 | } else { |
| 173 | db.exec(schema); |
| 174 | } |
| 175 | |
| 176 | // Record current schema version so migrations aren't re-applied on open |
| 177 | const currentVersion = getCurrentVersion(db); |
| 178 | if (currentVersion < CURRENT_SCHEMA_VERSION) { |
| 179 | db.prepare( |
| 180 | 'INSERT OR IGNORE INTO schema_versions (version, applied_at, description) VALUES (?, ?, ?)' |
| 181 | ).run(CURRENT_SCHEMA_VERSION, Date.now(), 'Initial schema includes all migrations'); |
| 182 | } |
| 183 | |
| 184 | return new DatabaseConnection(db, dbPath, backend, fts5Available); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Open an existing database. |
no test coverage detected