Repository navigation
ONE-446: nav 修复——搜索引擎默认种子(幂等补种 5 引擎,仅空表) - #9
Merged
Merged
Conversation
新装搜索框无引擎可用:search_engines 表与 CRUD 都在,但迁移与 setup
全程没有种子 INSERT。
- worker/src/db/seeds.ts:冻结的 5 引擎常量(固定 slug id、{q} 占位、
sort_order 1–5、仅 Google is_default)+ seedSearchEngines(),先 COUNT
探空表,再单条 INSERT OR IGNORE,任意一行存在即整表不碰。
- worker/src/lib/migrate.ts:applyMigrations 增加 seedDefaults 选项,
在 runMigrations 之后用 runner 解析出的 prefix 补种;不带该选项的
read-path guard(ensureSchema)永不补种,避免页面请求把用户删空的表
重新填上。
- 初始化 / 升级两条路显式开启:POST /api/auth/setup、
runSetupMigrations(POST /api/setup 与 POST /api/admin/setup)。
不动 schema、不新增迁移版本、不动 TABLE_PREFIX 逻辑、不改 DTO。
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.
背景
新装必然空表:
search_engines表与 CRUD 都齐,但迁移与 setup 全程没有种子 INSERT,搜索框渲染为空。本 PR 在 setup / 升级路径上补一次幂等的空表补种。改了哪些文件
worker/src/db/seeds.ts(新增)SEED_SEARCH_ENGINES+seedSearchEngines(db, prefix)worker/src/lib/migrate.tsapplyMigrations(env, { seedDefaults }):runMigrations之后按 runner 解析出的 prefix 补种worker/src/routes/auth.tsPOST /api/auth/setup(初始化)传seedDefaults: trueworker/src/services/setup.tsrunSetupMigrations(POST /api/setup与POST /api/admin/setup共用)传seedDefaults: trueworker/test/search-engine-seed.test.ts(新增)不动前端一行代码。
种子实现(严格按冻结契约)
幂等的两层保障:
SELECT COUNT(*) AS count FROM {prefix}search_engines,count > 0直接返回{seeded:false, inserted:0},整表一行都不碰(不 UPDATE、不 DELETE、不重排)。INSERT OR IGNORE:id 是固定 slug(非随机 uuid),即便两条 setup 请求在「探空 → 插入」之间并发,主键也会吞掉重复行,不会出现第二批。时间戳用
nowIso()(与库内其它写入同格式),icon 为NULL(契约允许)。表名走${prefix}search_engines,prefix 取自runMigrations返回的实际生效值,不二次解析TABLE_PREFIX,避免补种落到另一个前缀的库。挂载点与账本纪律
契约允许「
runMigrations之后」或「runSetupMigrations同层」。我选的是前者 + 显式开关,理由:POST /api/auth/setup)与「检查并升级数据库」(POST /api/setup、POST /api/admin/setup)确实共用applyMigrations这一个漏斗,把补种放在这里就只有一处实现。applyMigrations同时被读路径 guardensureSchema(GET /api/public//api/private)调用。如果无条件补种,用户把 5 个引擎全删了,下一次冷启动的页面请求就会把引擎「复活」——这不在契约意图内。所以补种由seedDefaults显式开关控制,只有初始化和升级两条路开启,读路径永不补种(search-engine-seed.test.ts里有一条专门钉住这一点)。schema_v2、不改schema_v1、不动 ledger 表、不动TABLE_PREFIX三态逻辑。升级时它随升级按钮一起跑,天然可重复执行。回滚方式
无 schema 变更、无迁移版本、无数据迁移,回滚 =
git revert本 PR 那一个 commit 重新部署。已补种的 5 行是纯数据,如需清掉可在管理面搜索框里删(或用DELETE FROM {prefix}search_engines WHERE id LIKE 'seed-%',需用户自行决定,团队不碰线上 D1)。自测证据(本地)
worker/test/search-engine-seed.test.ts覆盖的两种必测场景 + 边界:nav_/""/app_三种前缀)seedSearchEngines第一次{seeded:true,inserted:5}、第二次{seeded:false,inserted:0};整行快照(含 created_at)字节级不变seed-*一行都不出现applyMigrations(env)(无seedDefaults)后仍为 0 行POST /api/auth/setup201 后 5 行且/api/public返回 5 个引擎、仅 GoogleisDefault=true;POST /api/setup两次后仍 5 行真实 D1 未跑(团队不碰线上 D1);测试用
test/helpers.ts的node:sqliteD1 替身,保留真实列约束与默认值。需要评审留意的两点
POST /api/auth/setup的既有顺序:该路由在 secret 校验之前就跑了applyMigrations(存量行为,schema 因此在未鉴权时也会被建出来)。补种搭在同一处,所以未鉴权的首次POST /api/auth/setup也会把 5 条默认引擎写进刚建好的空表。数据是契约里写死的默认值、只在空表发生、无外部输入,且不鉴权那次请求本来就已经在建表;如果评审认为需要收紧到「建号成功后再补种」,我可以把它下移到createOwner之后,代价是初始化路径与升级路径各调用一次(实现仍在同一函数)。GET /api/public带cache-control: public, max-age=60,补种完成后新装页面最多 60s 内可能仍显示旧(空)payload——这是既有缓存策略,不在本单范围内。关联 ONE-446。复杂档:实现 → Tony 评审 → Mike 审核 验证,全程 CI 全绿。