* Open an existing database
(dbPath: string)
| 125 | * Open an existing database |
| 126 | */ |
| 127 | static open(dbPath: string): DatabaseConnection { |
| 128 | if (!fs.existsSync(dbPath)) { |
| 129 | throw new Error(`Database not found: ${dbPath}`); |
| 130 | } |
| 131 | |
| 132 | const { db, backend } = createDatabase(dbPath); |
| 133 | |
| 134 | configureConnection(db); |
| 135 | |
| 136 | // Check and run migrations if needed |
| 137 | const conn = new DatabaseConnection(db, dbPath, backend); |
| 138 | const currentVersion = getCurrentVersion(db); |
| 139 | |
| 140 | if (currentVersion < CURRENT_SCHEMA_VERSION) { |
| 141 | runMigrations(db, currentVersion); |
| 142 | } |
| 143 | |
| 144 | // Self-heal a bulk-load window that never closed (crash between |
| 145 | // beginBulkNodeLoad and endBulkNodeLoad): the FTS triggers are missing and |
| 146 | // nodes_fts is stale. Rebuild + recreate so search stays in sync. |
| 147 | conn.healBulkNodeLoad(); |
| 148 | |
| 149 | // Self-heal a killed session's leftover oversized WAL (#1431) — one |
| 150 | // statSync when healthy, off-thread checkpoint+truncate when not. |
| 151 | void conn.healOversizedWal(); |
| 152 | |
| 153 | return conn; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * FTS maintenance triggers dropped/recreated around a bulk load. |
no test coverage detected