Skip to content

fix: d1_migrations 守卫改内容判定(三处同源)+ 补回归用例 (ONE-476) - #151

Merged
mapxn merged 1 commit into
mainfrom
agent/daniel/a1402e8f789b
Oct 26, 2026
Merged

mapxn merged 1 commit into
mainfrom
agent/daniel/a1402e8f789b

Conversation

@mapxn

@mapxn mapxn commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

一句话

d1_migrations 守卫从「只看表存不存在」改为「看表里有没有本应用自己的迁移文件名」,三处同源(prepare 脚本 / prefixed 脚本新装分支 / 运行时 detectTablePrefixConflict),并补 3 组共 5 条回归用例。子单 ONE-478,父单 ONE-476。

改动

  • .github/scripts/d1-migration-files.mjs(新增):从 apps/worker/migrations/*.sql 现场枚举本应用 Wrangler 迁移文件名(一行一个),供两个部署脚本共用,脚本里没有任何硬编码清单。
  • .github/scripts/d1-prepare-migrations.sh:unprefixed 判定中 d1_migrations 一支改为 has_app_migration_history()——查 SELECT name FROM d1_migrations 与本应用文件名集合求交,有交集才置 unprefixed=true。_schema_migrations 一支、其余分支、错误文案未动。
  • .github/scripts/d1-prefixed-migrations.sh:新装分支守卫换成同一个 has_app_migration_history(),与 prepare 同口径。
  • apps/worker/src/lib/migrations.ts:新增 WRANGLER_MIGRATION_FILES,值为 getMigrations('').map(m => m.fileName)(复用既有 fileName 单源,不另起清单;新增 Wrangler 迁移只需在 getMigrations() 里加一条)。
  • apps/worker/src/lib/migrations-runner.ts:detectTablePrefixConflict() 改为查 d1_migrations 的 name 与 WRANGLER_MIGRATION_FILES 求交。

回归用例(规格 §3 的 1–6)

  • .github/scripts/test-d1-migrations.mjs:①外来 d1_migrations(仅 0001_other_app.sql/0002_other_app.sql)+ TABLE_PREFIX=SL + 无任何 SL 表 → prepare 输出 mode=prefixed,prefixed 脚本计划并执行 schema_v1 schema_v2、_SL_schema_migrations 两行齐全、assertOwnIndexes('SL') 通过、他表快照与 d1_migrations 内容前后不变;②d1_migrations 含 0001_init.sql/0002_token_version.sql → prepare 仍以 /unprefixed schema/ 中止,prefixed 脚本同样拒绝,且未建出任何 SL 表。
  • apps/worker/test/migrations-runner.test.ts:④外来 d1_migrations + prefix=SL + 无 SL 表 → 不抛 TablePrefixConflict,正常建出 _SL_schema_migrations 两行;⑤d1_migrations 含 0001_init.sql → 仍抛 TablePrefixConflict(/unprefixed schema/);⑥WRANGLER_MIGRATION_FILES 与 apps/worker/migrations/ 目录实际文件集合相等(漂移即红)。

自测证据(红 → 绿)

红:把三处守卫临时改回「只看存在」后跑新用例 ——

  • 回归:仅外来 d1_migrations(别家文件名)+ prefix=SL → FAIL(Command failed: d1-prepare-migrations.sh,即线上误杀)
  • 回归:仅外来 d1_migrations(别家文件名)+ SL 表全删 → FAIL(同上)
  • 两条真守卫用例保持 PASS(说明用例锁的是「别家账本误判」,不是把守卫拆掉)

绿(本 commit,本地跑):

  • node --test .github/scripts/test-d1-migrations.mjs → 13/13 pass
  • pnpm test(worker vitest 等价,node:test)→ 110/110 pass
  • pnpm check(biome)→ clean;shellcheck .github/scripts/*.sh → clean
  • pnpm typecheck → 2/2 successful;pnpm build → 2/2 successful

未做:未接触任何线上 D1 / KV / Secrets,未改 TABLE_PREFIX=SL 双边配置,未改迁移内容与账本结构。

Closes ONE-478(父单 ONE-476)

@mapxn
mapxn merged commit 1520407 into main Oct 26, 2026
2 checks passed
@mapxn
mapxn deleted the agent/daniel/a1402e8f789b branch November 8, 2026 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant