[P1/优化] SettingsModal inheritDatabase 复选框未校验源数据库完整性 #51

Closed
opened 2026-07-21 21:56:16 +08:00 by thzxx · 1 comment
Owner

问题类型

优化 / 高 / 前端组件

文件位置

src/components/settings/SettingsModal.tsx

问题描述

SettingsModal 中 inheritDatabase 复选框允许用户继承原工作空间数据库:

  • 用户勾选后直接执行继承
  • 未校验源数据库完整性
  • 若源数据库损坏(如未正常关闭导致 WAL 损坏),继承后新工作空间数据库也损坏
  • 用户难以察觉,后续使用中数据丢失

影响

  • 损坏数据库被继承
  • 数据完整性问题

建议修复

  1. 校验源数据库:继承前执行 PRAGMA integrity_check
async function validateDatabase(dbPath: string): Promise<boolean> {
  const tempDb = new Database(dbPath, { readonly: true });
  const result = tempDb.prepare('PRAGMA integrity_check').get() as { integrity_check: string };
  tempDb.close();
  return result.integrity_check === 'ok';
}

if (inheritDatabase) {
  const isValid = await validateDatabase(sourceDbPath);
  if (!isValid) {
    showError('源数据库损坏,无法继承');
    return;
  }
  // 执行继承
}
  1. 校验 schema 版本:确保源数据库 schema 版本与当前应用兼容
  2. 备份提示:继承前提示用户备份
  3. 回滚机制:继承失败时回滚到新工作空间初始状态
## 问题类型 优化 / 高 / 前端组件 ## 文件位置 `src/components/settings/SettingsModal.tsx` ## 问题描述 SettingsModal 中 inheritDatabase 复选框允许用户继承原工作空间数据库: - 用户勾选后直接执行继承 - 未校验源数据库完整性 - 若源数据库损坏(如未正常关闭导致 WAL 损坏),继承后新工作空间数据库也损坏 - 用户难以察觉,后续使用中数据丢失 ## 影响 - 损坏数据库被继承 - 数据完整性问题 ## 建议修复 1. **校验源数据库**:继承前执行 `PRAGMA integrity_check` ```ts async function validateDatabase(dbPath: string): Promise<boolean> { const tempDb = new Database(dbPath, { readonly: true }); const result = tempDb.prepare('PRAGMA integrity_check').get() as { integrity_check: string }; tempDb.close(); return result.integrity_check === 'ok'; } if (inheritDatabase) { const isValid = await validateDatabase(sourceDbPath); if (!isValid) { showError('源数据库损坏,无法继承'); return; } // 执行继承 } ``` 2. **校验 schema 版本**:确保源数据库 schema 版本与当前应用兼容 3. **备份提示**:继承前提示用户备份 4. **回滚机制**:继承失败时回滚到新工作空间初始状态
thzxx added the ??????? labels 2026-07-21 21:56:16 +08:00
Author
Owner

修复说明

文件: electron/ipc/handlers.ts + electron/preload.ts + src/types/global.d.ts + src/components/settings/SettingsModal.tsx

修复: 主进程新增 workspace:checkDatabaseIntegrity handler(用 better-sqlite3 只读打开源库执行 PRAGMA integrity_check);preload 暴露 workspace.checkDatabaseIntegritySettingsModal.handleApplyinheritDatabase 勾选时先校验源数据库,损坏或校验异常则 toast 报错并中止切换。

验证: tsc --noEmit 类型检查通过。

## 修复说明 **文件**: `electron/ipc/handlers.ts` + `electron/preload.ts` + `src/types/global.d.ts` + `src/components/settings/SettingsModal.tsx` **修复**: 主进程新增 `workspace:checkDatabaseIntegrity` handler(用 better-sqlite3 只读打开源库执行 `PRAGMA integrity_check`);preload 暴露 `workspace.checkDatabaseIntegrity`;`SettingsModal.handleApply` 在 `inheritDatabase` 勾选时先校验源数据库,损坏或校验异常则 toast 报错并中止切换。 **验证**: `tsc --noEmit` 类型检查通过。
thzxx closed this issue 2026-07-22 09:43:35 +08:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: MetonaTeam/metona-ai-desktop#51