MCPcopy Create free account
hub / github.com/ChatLab/ChatLab / openWritable

Method openWritable

packages/node-runtime/src/database-manager.ts:127–156  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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 * 关闭指定会话的数据库连接

Callers

nothing calls this directly

Calls 11

assertCompatibleMethod · 0.95
getDbPathMethod · 0.95
runMigrationsMethod · 0.95
openBetterSqliteDatabaseFunction · 0.90
getChatDbMigrationsFunction · 0.90
getSchemaVersionFunction · 0.90
setMethod · 0.80
getMethod · 0.65
closeMethod · 0.65
deleteMethod · 0.65

Tested by

no test coverage detected