Ordinary database schema upgrades previously ran VACUUM on free pages and showed misleading history-cleanup progress. Separate structure upgrades from required legacy conversions, retaining reclamation intent only for retries within the same startup. Preserve released data without new schema versions or persistent migration markers; Agent and normal writes remain unchanged. Add real SQLite and Worker regressions, bilingual operation-specific startup copy, and migration documentation. Synchronize native Electron drag input with actual resize state without weakening layout assertions. Validation: 47 focused migration tests passed; typecheck, lint and diff checks passed. Final full suite: 5137 passed, 74 skipped, 2 unrelated Electron failures. Both failing Electron tests passed an isolated rerun; full CI remains unverified. Release note: 修复普通数据库结构升级再次整理历史和回收空间的问题;仅在需要转换旧数据时执行相关处理,启动页准确显示当前升级操作。
122 lines
4.3 KiB
TypeScript
122 lines
4.3 KiB
TypeScript
import { app, ipcMain, type BrowserWindow } from 'electron'
|
|
import { stat } from 'node:fs/promises'
|
|
import { dirname, join } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { Worker } from 'node:worker_threads'
|
|
import {
|
|
assistantStorageActionSchema,
|
|
assistantStorageProgressSchema,
|
|
type AssistantStorageProgress
|
|
} from '../shared/assistant-storage-contracts'
|
|
import { ipcChannels } from '../shared/ipc-channels'
|
|
import { assertTrustedSender } from './trusted-ipc-sender'
|
|
import { getPendingAssistantStorageUpgrade, type AssistantStorageUpgrade } from './assistant/assistant-storage-upgrade'
|
|
import { loadMainWindow } from './window'
|
|
|
|
export async function prepareAssistantStorage(
|
|
window: BrowserWindow,
|
|
databasePath: string,
|
|
signal: AbortSignal
|
|
): Promise<void> {
|
|
const file = await stat(databasePath).catch((error: NodeJS.ErrnoException) => {
|
|
if (error.code === 'ENOENT') return undefined
|
|
throw error
|
|
})
|
|
if (!file) return
|
|
const upgrade = getPendingAssistantStorageUpgrade(databasePath)
|
|
if (!upgrade) return
|
|
let progress: AssistantStorageProgress = {
|
|
stage: 'upgrading', processed: 0, total: 0, bytesBefore: file.size
|
|
}
|
|
let shown = false
|
|
let retry: (() => void) | undefined
|
|
ipcMain.handle(ipcChannels.storageUpgradeProgress, (event) => {
|
|
assertTrustedSender(event, window)
|
|
return progress
|
|
})
|
|
ipcMain.handle(ipcChannels.storageUpgradeAction, (event, input: unknown) => {
|
|
assertTrustedSender(event, window)
|
|
const action = assistantStorageActionSchema.parse(input)
|
|
if (action === 'quit') app.quit()
|
|
else if (progress.stage === 'failed') retry?.()
|
|
})
|
|
const publish = (next: AssistantStorageProgress): void => {
|
|
progress = assistantStorageProgressSchema.parse(next)
|
|
if (window.isDestroyed()) return
|
|
if (!shown) {
|
|
shown = true
|
|
loadMainWindow(window, true)
|
|
}
|
|
window.setProgressBar(next.stage === 'failed' ? -1 :
|
|
next.total > 0 && next.stage === 'converting'
|
|
? next.processed / next.total : 2)
|
|
}
|
|
try {
|
|
while (!signal.aborted) {
|
|
try {
|
|
await runStorageWorker(databasePath, upgrade, signal, publish)
|
|
return
|
|
} catch (error) {
|
|
if (signal.aborted) return
|
|
publish({
|
|
...progress, stage: 'failed',
|
|
error: error instanceof Error ? error.message : '本地数据升级失败'
|
|
})
|
|
await new Promise<void>((resolve) => {
|
|
const finish = (): void => {
|
|
signal.removeEventListener('abort', finish)
|
|
retry = undefined
|
|
resolve()
|
|
}
|
|
retry = finish
|
|
signal.addEventListener('abort', finish, { once: true })
|
|
})
|
|
}
|
|
}
|
|
} finally {
|
|
ipcMain.removeHandler(ipcChannels.storageUpgradeProgress)
|
|
ipcMain.removeHandler(ipcChannels.storageUpgradeAction)
|
|
if (!window.isDestroyed()) window.setProgressBar(-1)
|
|
}
|
|
}
|
|
|
|
function runStorageWorker(
|
|
databasePath: string,
|
|
upgrade: AssistantStorageUpgrade,
|
|
signal: AbortSignal,
|
|
publish: (progress: AssistantStorageProgress) => void
|
|
): Promise<void> {
|
|
const cancellation = new SharedArrayBuffer(4)
|
|
const cancel = (): void => {
|
|
Atomics.store(new Int32Array(cancellation), 0, 1)
|
|
}
|
|
if (signal.aborted) return Promise.resolve()
|
|
signal.addEventListener('abort', cancel, { once: true })
|
|
return new Promise<void>((resolve, reject) => {
|
|
const worker = new Worker(join(
|
|
dirname(fileURLToPath(import.meta.url)), 'assistant-storage-worker.js'
|
|
), { workerData: { databasePath, cancellation, upgrade } })
|
|
let failure: Error | undefined
|
|
let done = false
|
|
worker.on('message', (message: {
|
|
progress?: AssistantStorageProgress
|
|
error?: string
|
|
cancelled?: boolean
|
|
done?: boolean
|
|
}) => {
|
|
if (message.progress) publish(message.progress)
|
|
if (message.error && !message.cancelled) failure = new Error(message.error)
|
|
if (message.done || message.cancelled) done = true
|
|
})
|
|
worker.once('error', () => {
|
|
failure = new Error('无法启动本地数据升级,请重启 GoodBuddy 后重试。')
|
|
})
|
|
worker.once('exit', (code) => {
|
|
signal.removeEventListener('abort', cancel)
|
|
if (failure) reject(failure)
|
|
else if (done && code === 0) resolve()
|
|
else reject(new Error('本地数据升级意外结束,请重试。'))
|
|
})
|
|
})
|
|
}
|