From 7d8564dc346bee3ec211c97e56537ee94b87ea27 Mon Sep 17 00:00:00 2001 From: Dash Date: Sun, 16 Aug 2026 23:01:12 +1200 Subject: [PATCH] =?UTF-8?q?fix(desktop):=20dev=20writer=20=E4=B8=8D?= =?UTF-8?q?=E5=BE=97=E6=8A=8A=E6=AD=A3=E5=BC=8F=20profile=20=E5=8D=87?= =?UTF-8?q?=E5=88=B0=E5=AE=89=E8=A3=85=E7=89=88=E6=B2=A1=E6=9C=89=E7=9A=84?= =?UTF-8?q?=20schema?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 已合入 main、但安装包还没带上的 migration 同样会打挂正式版。 unpackaged writer 在正式目录发现 pending 时 fail closed,改走 isolated。 Signed-off-by: Dash --- .../officialProfileMigrationPolicy.test.ts | 70 +++++++++++++++++++ apps/desktop/src/main/index.ts | 7 ++ apps/desktop/src/main/localDb/migrate.ts | 15 ++++ .../localDb/officialProfileMigrationPolicy.ts | 23 ++++++ docs/dev-rules/database-and-migrations.md | 4 ++ docs/dev-rules/desktop-development.md | 2 + 6 files changed, 121 insertions(+) create mode 100644 apps/desktop/src/main/__tests__/officialProfileMigrationPolicy.test.ts create mode 100644 apps/desktop/src/main/localDb/officialProfileMigrationPolicy.ts diff --git a/apps/desktop/src/main/__tests__/officialProfileMigrationPolicy.test.ts b/apps/desktop/src/main/__tests__/officialProfileMigrationPolicy.test.ts new file mode 100644 index 0000000000..a2106955d7 --- /dev/null +++ b/apps/desktop/src/main/__tests__/officialProfileMigrationPolicy.test.ts @@ -0,0 +1,70 @@ +import { readFileSync } from 'node:fs'; +import { resolve } from 'node:path'; +import { describe, expect, it } from 'vitest'; + +import { + officialProfileWriterMigrationMessage, + shouldRefuseOfficialProfileWriterMigration, +} from '../localDb/officialProfileMigrationPolicy'; + +describe('shouldRefuseOfficialProfileWriterMigration', () => { + it('unpackaged writer + 正式 profile + pending 必须拒绝', () => { + expect( + shouldRefuseOfficialProfileWriterMigration({ + isPackaged: false, + officialSharedProfile: true, + pendingCount: 1, + }), + ).toBe(true); + }); + + it('schema 已对齐时允许打开正式 profile', () => { + expect( + shouldRefuseOfficialProfileWriterMigration({ + isPackaged: false, + officialSharedProfile: true, + pendingCount: 0, + }), + ).toBe(false); + }); + + it('packaged / 沙箱 / custom 仍可迁', () => { + expect( + shouldRefuseOfficialProfileWriterMigration({ + isPackaged: true, + officialSharedProfile: true, + pendingCount: 3, + }), + ).toBe(false); + expect( + shouldRefuseOfficialProfileWriterMigration({ + isPackaged: false, + officialSharedProfile: false, + pendingCount: 3, + }), + ).toBe(false); + }); +}); + +describe('officialProfileWriterMigrationMessage', () => { + it('点名待执行 migration 并指向 isolated', () => { + const message = officialProfileWriterMigrationMessage(['0091_amazing_blur.sql']); + expect(message).toContain('0091_amazing_blur.sql'); + expect(message).toContain('--isolated='); + }); +}); + +describe('official profile writer wiring', () => { + it('index 对正式 profile 落地 XDT_OFFICIAL_SHARED_PROFILE,migrate 在备份前拒绝', () => { + const indexSource = readFileSync(resolve(process.cwd(), 'src/main/index.ts'), 'utf8'); + const migrateSource = readFileSync( + resolve(process.cwd(), 'src/main/localDb/migrate.ts'), + 'utf8', + ); + expect(indexSource).toContain("process.env.XDT_OFFICIAL_SHARED_PROFILE = '1'"); + const refuseIdx = migrateSource.indexOf('shouldRefuseOfficialProfileWriterMigration'); + const backupIdx = migrateSource.indexOf('await backupDb('); + expect(refuseIdx).toBeGreaterThan(-1); + expect(backupIdx).toBeGreaterThan(refuseIdx); + }); +}); diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts index 06e0fc1f3a..2c4ef8fad5 100644 --- a/apps/desktop/src/main/index.ts +++ b/apps/desktop/src/main/index.ts @@ -112,6 +112,13 @@ if (devFlags.isolatedOnProductionProfile) { ); exit(1); } +if (!app.isPackaged && devFlags.profileKind === 'production-shared') { + // 正式 profile 上的 unpackaged writer 不得应用 pending migration。 + // 已合入 main、但安装包还没带上的序号会把安装版打挂(2026-08-16 schema 91)。 + process.env.XDT_OFFICIAL_SHARED_PROFILE = '1'; +} else { + delete process.env.XDT_OFFICIAL_SHARED_PROFILE; +} if (devFlags.schedulerPassive) { // 统一收敛到 env:scheduler-host 只认 XDT_SCHEDULER_PASSIVE,不重复解析 argv。 process.env.XDT_SCHEDULER_PASSIVE = '1'; diff --git a/apps/desktop/src/main/localDb/migrate.ts b/apps/desktop/src/main/localDb/migrate.ts index 90070b4d5d..3019074602 100644 --- a/apps/desktop/src/main/localDb/migrate.ts +++ b/apps/desktop/src/main/localDb/migrate.ts @@ -32,6 +32,10 @@ import { readSchemaVersion, runMigrationReplay, } from './migrationRunner'; +import { + officialProfileWriterMigrationMessage, + shouldRefuseOfficialProfileWriterMigration, +} from './officialProfileMigrationPolicy'; const log = createLogger('migrate'); @@ -92,6 +96,17 @@ export async function runMigrations( ); return; } + if ( + shouldRefuseOfficialProfileWriterMigration({ + isPackaged: app.isPackaged, + officialSharedProfile: process.env.XDT_OFFICIAL_SHARED_PROFILE === '1', + pendingCount: pending.length, + }) + ) { + throw new Error( + officialProfileWriterMigrationMessage(pending.map((migration) => migration.fileName)), + ); + } // 备份前先做磁盘侧准备:腾旧备份预算 + 剩余空间预检。 // 背景(2026-07 用户现场):旧流程只在迁移成功后清理旧备份——一旦备份把磁盘 diff --git a/apps/desktop/src/main/localDb/officialProfileMigrationPolicy.ts b/apps/desktop/src/main/localDb/officialProfileMigrationPolicy.ts new file mode 100644 index 0000000000..d51a98fc27 --- /dev/null +++ b/apps/desktop/src/main/localDb/officialProfileMigrationPolicy.ts @@ -0,0 +1,23 @@ +/** + * 正式 profile 上的 dev writer 不得应用 pending migration。 + * + * 2026-08-16:含 0091 的 checkout 以 writer 打开正式 Cindy 目录,把安装版 + * 0.1.50(只到 0090)打挂。`assertSharedDevMigrationPolicy` 只拦「未合入 + * origin/main」的产物;已合入主干、但安装包还没带上的 migration 照样会写进 + * 共享库。这一层看真实 profile + pending,不看 isolated 旗标。 + */ +export function shouldRefuseOfficialProfileWriterMigration(input: { + isPackaged: boolean; + officialSharedProfile: boolean; + pendingCount: number; +}): boolean { + return !input.isPackaged && input.officialSharedProfile && input.pendingCount > 0; +} + +export function officialProfileWriterMigrationMessage(pendingNames: readonly string[]): string { + const pending = pendingNames.length > 0 ? pendingNames.join(', ') : '(unknown)'; + return ( + `开发版不能把正式 Cindy 目录的数据库升到当前 checkout 的 schema(待执行 ${pending})。` + + '请改用 --isolated=<名字>,或等包含这些 migration 的正式版发布后再用共享目录。' + ); +} diff --git a/docs/dev-rules/database-and-migrations.md b/docs/dev-rules/database-and-migrations.md index 5d5565ddcf..a43a44c89e 100644 --- a/docs/dev-rules/database-and-migrations.md +++ b/docs/dev-rules/database-and-migrations.md @@ -90,6 +90,10 @@ companion CommonJS 格式和历史 runtime identity 冻结;不能用单独 typ - 未进入 `main` 的 migration 禁止连接共享 Cindy userData 运行;否则分支换号或回退后会让 本地 `schema_version` 与真实结构永久分叉。 +- **已合入 `main`、但安装包还没带上的 migration 同样不得写进正式 profile。** unpackaged + writer 在 Cindy / CindyGlobal / CindyDev 上发现 pending 就 fail closed(2026-08-16: + checkout 带着 0091 把 0.1.50 的共享库升到 91,安装版打不开)。要验证新 schema 必须 + `--isolated[=<名字>]`;等正式版发布后再用共享目录。 - 需要启动验证时,按照 `desktop-development.md` 的参数说明使用显式 `--isolated[=<名字>]` 沙箱。migration replay 自身使用临时数据库,不污染用户数据。 - 不得为了测试 migration 临时改写、降级或删除用户数据库;需要历史状态时新增最小 fixture。 diff --git a/docs/dev-rules/desktop-development.md b/docs/dev-rules/desktop-development.md index 5dc332d9e0..3a6136d0b9 100644 --- a/docs/dev-rules/desktop-development.md +++ b/docs/dev-rules/desktop-development.md @@ -76,6 +76,8 @@ Cindy 内部而拒绝重启,或因目标 userData 被其他 checkout 占用而 正式版目录保持历史兼容:CN → `Cindy`,Global → `CindyGlobal`,不在启动时改名或搬迁用户数据。 非隔离 dev 也使用当前区域对应的正式 profile;`--isolated` 沙箱再按相同区域映射派生目录。 +**dev writer 不得把正式 profile 升到当前 checkout 比安装版更新的 schema**:有 pending +migration 就拒绝启动,改用 `--isolated=<名字>`。`--preserve-running` / 共库 passive 仍只读。 跨区域共享、登录态迁移或旧版本回滚应使用显式隔离目录,避免不同构建误用同一 profile。 ### 并行多开 dev