* Rebuild the project's database from scratch and return a fresh, empty * instance — the "same result as a fresh init" semantics that `codegraph * index` documents. * * Unlike `open()` followed by `clear()`, this DISCARDS the existing * `.codegraph/codegraph.db` (and its `-wal`/`-shm`
(projectRoot: string)
| 336 | * (and running migrations against) the poisoned database entirely. |
| 337 | */ |
| 338 | static async recreate(projectRoot: string): Promise<CodeGraph> { |
| 339 | await initGrammars(); |
| 340 | const resolvedRoot = path.resolve(projectRoot); |
| 341 | |
| 342 | // Check if initialized — recreate REBUILDS an existing project; it is not a |
| 343 | // first-time `init`. |
| 344 | if (!isInitialized(resolvedRoot)) { |
| 345 | throw new Error(`CodeGraph not initialized in ${resolvedRoot}. Run init() first.`); |
| 346 | } |
| 347 | |
| 348 | const dbPath = getDatabasePath(resolvedRoot); |
| 349 | try { |
| 350 | removeDatabaseFiles(dbPath); |
| 351 | } catch (err) { |
| 352 | // POSIX unlinks an open file fine; this fires mainly on Windows when a |
| 353 | // live daemon/MCP server still holds the database. Turn the raw EBUSY into |
| 354 | // an actionable instruction instead of a generic failure. |
| 355 | const reason = err instanceof Error ? err.message : String(err); |
| 356 | throw new Error( |
| 357 | `Could not rebuild the index — the database file is in use (${reason}). ` + |
| 358 | `Stop any running CodeGraph MCP server/daemon for this project and retry, ` + |
| 359 | `or remove the ${getCodeGraphDir(resolvedRoot)} directory and run "codegraph init".` |
| 360 | ); |
| 361 | } |
| 362 | |
| 363 | // Re-create an empty, freshly-schema'd database at the same path. |
| 364 | const db = DatabaseConnection.initialize(dbPath); |
| 365 | const queries = new QueryBuilder(db.getDb()); |
| 366 | |
| 367 | return new CodeGraph(db, queries, resolvedRoot); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Open synchronously (without sync) |
no test coverage detected