* Open a session DB in read-write mode and auto-run pending migrations. * Falls back to `open(id, { readonly: false })` if migration is unnecessary.
(sessionId: string)
| 125 | * Falls back to `open(id, { readonly: false })` if migration is unnecessary. |
| 126 | */ |
| 127 | openWritable(sessionId: string): DatabaseAdapter | null { |
| 128 | this.assertCompatible() |
| 129 | |
| 130 | const existing = this.cache.get(sessionId) |
| 131 | if (existing && !existing.readonly) return existing |
| 132 | |
| 133 | if (existing) { |
| 134 | existing.close() |
| 135 | this.cache.delete(sessionId) |
| 136 | } |
| 137 | |
| 138 | const dbPath = this.getDbPath(sessionId) |
| 139 | if (!fs.existsSync(dbPath)) return null |
| 140 | |
| 141 | const adapter = openBetterSqliteDatabase(dbPath, { |
| 142 | readonly: false, |
| 143 | nativeBinding: this.nativeBinding, |
| 144 | }) |
| 145 | |
| 146 | if (coreNeedsMigration(adapter, CURRENT_SCHEMA_VERSION)) { |
| 147 | const migrations = getChatDbMigrations(this.migrationDeps) |
| 148 | this.runMigrations(adapter, migrations) |
| 149 | this.assertCompatible() |
| 150 | } else { |
| 151 | this.raiseCompatibilityGateIfNeeded(getSchemaVersion(adapter)) |
| 152 | } |
| 153 | |
| 154 | this.cache.set(sessionId, adapter) |
| 155 | return adapter |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * 关闭指定会话的数据库连接 |
nothing calls this directly
no test coverage detected