(runtime?: RuntimeIdentity)
| 310 | error?: string |
| 311 | } |
| 312 | export function migrateAllDatabases(runtime?: RuntimeIdentity): { |
| 313 | success: boolean |
| 314 | migratedCount: number |
| 315 | failures: MigrationFailure[] |
| 316 | error?: string |
| 317 | } { |
| 318 | const { sessionIds, forceRepairIds } = checkMigrationNeeded() |
| 319 | const forceRepairSet = new Set(forceRepairIds) |
| 320 | |
| 321 | if (sessionIds.length === 0) { |
| 322 | return { success: true, migratedCount: 0, failures: [] } |
| 323 | } |
| 324 | |
| 325 | let migratedCount = 0 |
| 326 | const failures: MigrationFailure[] = [] |
| 327 | |
| 328 | for (const sessionId of sessionIds) { |
| 329 | try { |
| 330 | const needsForceRepair = forceRepairSet.has(sessionId) |
| 331 | const db = openDatabaseWithMigration(sessionId, needsForceRepair, runtime) |
| 332 | if (db) { |
| 333 | db.close() |
| 334 | migratedCount++ |
| 335 | } |
| 336 | } catch (error) { |
| 337 | const errorMessage = error instanceof Error ? error.message : String(error) |
| 338 | console.error(`[Database] Failed to migrate ${sessionId}:`, errorMessage) |
| 339 | failures.push({ sessionId, error: errorMessage }) |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // 如果有失败的数据库,返回部分成功状态 |
| 344 | if (failures.length > 0) { |
| 345 | const failedIds = failures.map((f) => f.sessionId.split('_').slice(-1)[0]).join(', ') |
| 346 | return { |
| 347 | success: false, |
| 348 | migratedCount, |
| 349 | failures, |
| 350 | error: `${failures.length} 个数据库迁移失败(ID: ${failedIds})。建议在侧边栏中删除这些损坏的会话。`, |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return { success: true, migratedCount, failures: [] } |
| 355 | } |
no test coverage detected