Repository navigation
feat(worker): 迁移账本化(schema_migrations 账本 + 历史塌缩 schema_v1)(ONE-400) - #152
Merged
Merged
Conversation
- 新增 worker/src/lib/migrations.ts:迁移清单单一真源,历史塌缩为一条 schema_v1(原 getSchema() DDL 逐字迁入),文件头写明追加规矩。 - 新增 worker/src/lib/migrations-runner.ts:migrationsTable() 是账本表名唯一 构造点;runLedgerMigrations() 逐条应用未记录的迁移,全部语句成功后才记账, 任一失败立即抛错(含迁移名与原始错误),不再吞错。 - setup.ts:getSchema() 删除;runMigrations() 更名 runLegacyMigrations()(逻辑 未改,仅过渡分支调用);runSetup() 按账本三分支重组(账本存在→runner + 一致性 检查;无账本有业务表→旧补丁→验形→打戳;都没有→新装 runner + 默认设置播种), SetupResult 成功变体新增 migrations 字段。前缀冲突守卫与 detectTablePrefixes 未动。 - 测试:新增 ledger.test.ts(新装/结构对照快照/存量过渡/验形失败/前缀变体/守卫/ 账本一致性)、ledger-failure.test.ts(失败可见性与续跑);既有 setup 相关用例按 新状态机调整,无删用例。SqliteD1 提取到 test/support 复用。 - docs/migration-ledger-cutover-runbook.md:生产切换 runbook(§8 逐步清单 + 每步 核对与回滚 + 两条路径关系);CLAUDE.md 同步账本制。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
父单 ONE-400 规格 §3/§5/§7:把 eNotify 的探测式补丁迁移换成账本制,schema 一字不改,历史塌缩为一条
schema_v1。本 PR 是后端子单 ONE-401 的全部实现,无前端改动。改动
新增
worker/src/lib/migrations.ts— 迁移清单单一真源:Migration+getMigrations(prefix),当前仅schema_v1(原getSchema()DDL 逐字迁入,{prefix}占位替换方式不变)。文件头写明追加规矩(只在末尾追加schema_vN、已合并迁移永不改名/改内容/重排序、只做可加性变更、SQL 字面量不得含;)。worker/src/lib/migrations-runner.ts—migrationsTable(prefix)是账本表名的唯一构造点(_+ 前缀核心 +_schema_migrations;无前缀即_schema_migrations);runLedgerMigrations()建账本表 → 读已记录集合 → 按顺序逐条:已记录skipped,未记录按;切分逐句prepare().run(),全部语句成功后才记账applied,任一失败立即抛错(message 含迁移名与原始错误、附cause),不记账、不继续。worker/test/ledger.test.ts、worker/test/ledger-failure.test.ts、worker/test/support/sqlite-d1.ts、worker/test/fixtures/schema-before-ledger.json(改造前结构快照)。修改
worker/src/lib/setup.ts— 删getSchema();runMigrations()更名runLegacyMigrations()(6 块逻辑一字未改,仅过渡分支调用);runSetup()按三分支重组:账本存在 → 一致性检查(账本有记录但users不在 → 失败指引)+ runner;无账本有业务表 → 旧补丁 → 验形(7 表 +body/四渠道列/calendar_mode+lunar_expiry_date)→ 打戳;都没有 → runner +DEFAULT_SETTINGS播种。SetupResult成功变体加可选migrations字段(含stamped)。前缀冲突守卫与detectTablePrefixes()未动,且仍在任何账本动作之前。worker/test/{admin,migrations,prefix,setup}.test.ts、worker/test/helpers.ts— 按新状态机调整断言与 fixture(MockD1 增加账本行模型),未删任何用例。CLAUDE.md— setup/迁移段落同步账本制(清单位置、追加规矩、账本名公式)。文档
docs/migration-ledger-cutover-runbook.md— 生产切换 runbook(§8 逐步清单 + 每步核对/回滚 + 两条路径关系)。其中备份改名采用后缀形式enotify_<table>_bak_<YYYYMMDD>,原因写在 runbook 里:bak_users这类前缀形式会被detectTablePrefixes()认成另一套 eNotify 表族,第 3 步新装会被PREFIX_CONFLICT拦下;同时补了「先前点过检查并升级、账本已打戳」时先把账本表挪开的一步(否则第 3 步会按设计报LEDGER_INCONSISTENT)。自测
pnpm typecheck✅pnpm build✅pnpm lint/pnpm format:check✅(仅剩一条既有 warning:worker/src/routes/items.ts的未使用 import,非本单引入)pnpm test✅ 17 files / 213 tests 全过(改造前 15 files / 187 tests;新增 26 个用例,无删除)覆盖:新装(7 表 + 账本 + 默认设置 + 二次
skipped且结构与数据不变)、结构对照(用改造前getSchema()生成的结构快照 fixture 逐表比对PRAGMA table_info与索引清单)、存量过渡最旧形态(旧补丁补齐 + 验形打戳 + 各行数据逐行不变 + 退役渠道清理同旧行为)、现行形态(直接打戳、数据不变、再跑skipped)、验形失败两种构造(缺表 / 列名大小写导致旧补丁加不上)→ 失败且未建账本、修好后可续上、前缀变体(_enotify_schema_migrations/_schema_migrations/_X_schema_migrations+ 两套前缀共存互不干扰)、守卫回归(他前缀仍 409 且不建账本;别家_ZETA_schema_migrations不影响新装与过渡)、失败可见性(注入必失败迁移 → 抛错含迁移名、不记账、后续不执行、修好后从账本续跑)。关口
按纪律停在合并关口:不合并、不部署、不碰生产库。生产切换由用户按 runbook 执行。