Skip to content

【规格】G1应用层唯一校验+G2外键统一开(承接#819,parity图内) #854

Closed

Description

@FeatherHunter

Part of #765

Problem Statement

站在私家大厨用户的角度:我按菜名加菜、改名、查看时,遇到重名菜谱,系统有时返回这一行、有时返回那一行,采购清单与历史统计也可能算到错的菜上;子数据(食材、步骤、历史)一旦指向不存在的菜谱,页面照样渲染,错得悄无声息。我要的是:重名写不进去(报错说清楚),按名只定位到唯一一行,脏关系的写入当场被拦住——且这一切在老库不动结构的前提下做到。

Solution

站在用户的角度:加菜或改名撞到已有的菜名时,命令直接失败并告诉我是哪个名字撞了,不写脏数据;用名字查看、做菜、记历史时,若库里已有重名(历史遗留),系统报错而不是随机挑一行;食材、步骤、历史等子记录指向不存在的菜谱时,写入同样失败。老库结构一行不改;真正的数据库级唯一保证由图外另票跟进,本规格只做应用层与连接层的正确性。

User Stories

  1. As a home cook adding a recipe, I want a duplicate dish name to be rejected with the offending name spelled out, so that I never create two dishes I cannot tell apart later.
  2. As a home cook renaming a recipe, I want renaming onto an existing name to be rejected, so that I do not silently merge two dishes.
  3. As a home cook with leading/trailing spaces in my input, I want the name check to ignore those spaces exactly the way the add path already trims, so that " 辣椒炒肉 " and "辣椒炒肉" count as the same name.
  4. As a home cook looking up a recipe by name, I want the lookup to resolve to exactly one recipe, so that the detail page I see is deterministic.
  5. As a home cook opening a legacy database that already contains duplicate names, I want the lookup to fail loudly instead of showing a random one of the duplicates, so that I know cleanup is needed.
  6. As a home cook viewing a recipe, I want ingredients and steps that belong to it and nothing else, so that the shopping list I generate is correct.
  7. As a home cook recording a cooking session, I want the history entry to attach to exactly the recipe I named, so that per-dish statistics stay truthful.
  8. As a home cook generating a shopping list for several dishes, I want each ingredient merged under the right dish names, so that quantities add up correctly.
  9. As a home cook adding an ingredient, I want an ingredient that points at a non-existent recipe to be rejected, so that orphan rows never accumulate.
  10. As a home cook adding a cooking step, I want a step that points at a non-existent recipe to be rejected for the same reason.
  11. As a home cook recording history, I want a history entry for a non-existent recipe to be rejected for the same reason.
  12. As a home cook using only the documented commands, I want all of the above to hold without ever touching the database file directly, so that the read commands I already use behave exactly as before.
  13. As a domain implementer for the add flow, I want one canonical name-equality rule to call, so that add-time checks and rename-time checks cannot drift apart.
  14. As a domain implementer for the update flow, I want the same rule at rename time, so that the two main write paths share one verdict.
  15. As a future schema owner, I want the application-level rule to match the future unique index semantics exactly, so that the later index lands with zero behavior change.

Implementation Decisions

  • 重名判定口径:写前去首尾空格后精确相等、大小写敏感,与未来数据库唯一约束的二进制语义一致;大小写、全半角等归一化不在本规格内,另立票。
  • 判定落点:录入域的新增路径与修改域的改名路径在写入前先查重,撞名即失败并报出撞名的名字;按名定位读到多行时报错,不再任取第一行;空名与缺名的现有报错行为不变。
  • 同一口径只定义一次,两处写路径引用它,不各写一份。
  • 外键开关落点:数据层开库入口统一显式开启外键 enforcement,一处声明;其余连接参数不搬,读路径的查询语句零改动。
  • 只增不删口径保持:应用层不提供物理删除,导入失败的回滚走事务一次提交或整批丢弃,不用按行删除语句。
  • 老库结构变更不在本规格内:真正的唯一索引由图外另票承接,建索引前先重数重名行;其余表的同类声明与现状差异要全表扫则另立票。

Testing Decisions

  • 好测试只看对外行为:命令的成功失败、报错里是否点名、库里行数的变化;不测内部函数形状与实现细节。
  • 测试缝只用一个,取现有最高的:命令唯一出口的真实进程调用(加菜、改名、查看、搜菜、记历史、采购合并全走真命令打副本库)。不新增任何测试缝。
  • 先例:命令级全票用例(加菜到查看闭环、步骤与采购合并、历史闭环、更新与全维度筛选)、取数层用例(空查询响亮失败、历史统计闭环、采购合并、体检排序)、决议校验脚本的正反例门(两节齐则过、缺一节则点名失败)。
  • 必测行为:重名新增被拦且点名;改名撞名被拦;遗留重名下按名定位报错;坏外键子行(食材、步骤、历史指向不存在的菜谱)写入失败;全量子表孤儿计数为零;读回归(列表、搜索、详情、体检的输出与既有地面真相一致)。
  • 全部测试打副本库,不碰真库;并发与编译走仓内既有排队通道。

Out of Scope

  • 老库结构变更(唯一索引的真实建成,图外另票);其余表的声明与现状差异全表审计。
  • 菜名归一化(大小写、全半角、异体字)。
  • 其余连接参数的搬运;读路径的新功能。
  • 插件侧、发版、其他五个技能;采购清单表、做菜进度表等缺表的补建。

Further Notes

  • 承接裁定 #819(决议与校验脚本已交付);外键显式开启的一行(openChefDb 内 PRAGMA foreign_keys=ON)已在提交树生效,本票核验 PRAGMA foreign_keys=1 通过,无需再改。
  • 背景诚实注:当前运行时的新连接默认已开启外键,属隐式依赖,显式声明是为可移植与防回退;老实现按名查到多行时取第一行,新行为改为报错,是正确性优先于逐字复刻旧缺陷。
  • 本规格挂 parity 图,实施票落在录入域与修改域;说明面同步另有专票,不在本规格内。

进度:100%

下一步:无(已终验关闭)。

实施记录

  • 提交:de28e6bf(packages/skill-chef/src/fetch/db.ts,29 增 4 删):canonicalRecipeName 单一口径(去首尾空格后精确相等、大小写敏感)+新增前查重+改名前查重并存去空格名+按名多行报错(id 优先命中,名命中 0 行报无此菜谱、多行报撞名点名行数);openChefDb 的 PRAGMA foreign_keys=ON 核验通过(本票零改动)。
  • 自查(副本库真命令):19/19 全绿——重名新增拦且点名、带空格重名拦、库内无脏行、改名撞名拦、坏外键食材/步骤/历史拦、PRAGMA foreign_keys=1、孤儿计数全 0、原生坏子行直插被拦、遗留重名下查看报错点名行数、读回归(列表/搜索/详情/体检)与详情形状一致。
  • 回归:fetch+cli+policy+render+skill 27/27 全绿;变异(删新增查重)17/19 变红,还原后 19/19 复绿,有鉴别力。

Activity

  1. added
    ready-for-agent
    on Sep 21, 2026
  2. FeatherHunter commented on Sep 21, 2026

    @FeatherHunter
    OwnerAuthor

    决议(#854 终验通过,关闭):

    G1 应用层唯一校验已实施:canonicalRecipeName(去首尾空格后精确相等、大小写敏感,与未来唯一索引二进制语义一致)在 packages/skill-chef/src/fetch/db.ts 定义一次,新增与改名两处引用;新增撞名与改名撞名均失败并点名(菜名已存在:“X”);按名定位 id 优先,名命中多行报错(菜名撞名(库内多行同名,须先清重):“X”共N行),不再任取第一行;空名与缺名行为不变。

    G2 外键统一开:核验 openChefDb 内 PRAGMA foreign_keys=ON 已在提交树生效,PRAGMA foreign_keys=1,其余连接参数未搬、读查询零改动;本票零改动。

    自查(副本库真命令 19/19+回归 27/27+变异有鉴别力),提交 de28e6bf。老库结构零改动;数据库级唯一索引由图外 #852 跟进。

  3. added 2 commits that reference this issue on Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ready-for-agent

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions