Repository navigation
ONE-346: nav 迁移账本化 — 迁移列表 + 账本 runner + 收敛 schema 定义 - #6
Merged
Merged
Conversation
Replace nav's bare "CREATE TABLE IF NOT EXISTS" schema replay with the shortlink-style ledger: a versioned migration list plus a `_<prefix>_schema_migrations` ledger table recording what has run. - lib/migrations.ts: single source of truth for the schema. migrationsTable() is the only place the ledger name is derived (`_` + prefix core without its trailing underscore + `_schema_migrations`), and getMigrations() collapses nav v1's schema into one baseline `schema_v1`. - lib/migrations-runner.ts: runMigrations() resolves the three-state prefix, guards against a silent prefix switch (nav business-table family + ledger table scan), creates the ledger, skips recorded migrations, records each success and stops at the first failure without recording it. - setup (POST /api/auth/setup) and the read-path guard ensureSchema() share the runner; setup now also reports tablePrefix + per-migration applied/skipped and maps a prefix conflict to 409. - Removed the second schema definition: db/schema.sql, scripts/gen-schema.mjs, the generated module, the `codegen` steps and the codegen-only SQL helpers. Refs ONE-346
Review of PR #6 (ONE-363) found the conflict probe comparing a table-derived prefix core ("nav") with the raw configured prefix ("nav_"), so an existing nav_* database without the ledger was rejected as a prefix conflict on the first run (500 on read paths, 409 on setup). - lib/migrations.ts: export prefixCore() — the ledger name and the conflict probe now share one definition of "prefix without its trailing underscore". - lib/migrations-runner.ts: compare detected cores against prefixCore(prefix), so a pre-ledger nav_* schema is adopted and recorded as schema_v1 instead of being refused. - lib/migrations-runner.ts: the no-prefix/ledger-present hint now points at nav's own prefix (TABLE_PREFIX=nav_) instead of suggesting whichever ledger happened to be scanned first (could be another project's SL_/ZETA_ in the shared D1). - test: regression case — drop the ledger from an initialised nav_* database and re-run; schema_v1 is applied and recorded, no TablePrefixConflictError. Refs ONE-346
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.
Closes ONE-346(子单 ONE-357)
把 nav 的裸建表式 schema 初始化改成 shortlink 的账本式:版本化迁移列表 + 账本表
_nav_schema_migrations。改了什么
worker/src/lib/migrations.ts(新增,schema 唯一可信源)migrationsTable(prefix):账本表名唯一生成处,公式_+ 前缀核 +_schema_migrations,前缀核 = 生效前缀去掉尾部下划线、大小写原值保留(nav_→_nav_schema_migrations、app_→_app_schema_migrations、abc123→_abc123_schema_migrations、""→_schema_migrations)。createMigrationsTableSql(prefix):name TEXT PRIMARY KEY, applied_at INTEGER NOT NULL DEFAULT (unixepoch())。getMigrations(prefix):迁移列表,schema_v1= 原schema.sql的全部表/索引 DDL +setup_completed='0'种子(种子留在迁移内,不另设后置步骤)。worker/src/lib/migrations-runner.ts(新增)runMigrations(env):getTablePrefix三态解析 → 防静默切换冲突探测 → 建账本表 → 读已应用集合 → 按序执行未记录项、成功后立即记账 → 首错即停、失败项不落账、后续不执行;返回{ prefix, migrations: [{ name, status, error? }] };前缀冲突抛TablePrefixConflictError。*_passkey_credentials+*_search_engines+*_login_attempts三者同前缀才判定,避免共享 D1 他项目表误判)+ 账本表_<前缀>_schema_migrations形态扫描。非空前缀下只用业务表族判定(账本名无法归属项目,共享库里_sl_schema_migrations之类不该拦 nav 首装);空前缀下账本扫描参与判定(更保守)。exec()执行,注释剥离 + 单行化沿用 shortlink 做法(exec()只处理多行串首行)。applyMigrationList(db, prefix, migrations)独立可测(失败注入用)。worker/src/routes/auth.ts:POST /api/auth/setup改走applyMigrations(),响应在{ success, user, tablePrefix }形状上追加migrations: [{name,status}](applied/skipped);TablePrefixConflictError→ 409{ error: "table prefix conflict", detail }。worker/src/lib/migrate.ts:ensureSchema()保留 per-isolateschemaReady快路径,内部改走 runner;读路径首次建表行为不变。worker/src/db/schema.sql、worker/scripts/gen-schema.mjs、生成的worker/src/db/schema.generated.ts、worker/package.json的codegen步骤(dev/build/typecheck/test/deploy 均去掉 codegen),以及只服务建表的splitStatements/runScript;同步.gitignore、biome.json、.github/workflows/deploy.yml注释。README.md新增「数据库迁移账本」小节(表名公式、schema_v1塌缩说明、runner 语义、今后著述规则);docs/api-contract.md补 setup 响应的migrations/409 说明。不新增依赖、不新增 PRAGMA 探测式升级代码、不动 TABLE_PREFIX 三态语义、不动对外 API 契约与鉴权逻辑。
测试
worker/test/migrations-ledger.test.ts:migrationsTable()三态 + 大小写原值保留;空库首跑applied且账本恰含schema_v1;第二跑全skipped且sqlite_master(表 + 索引)与账本行完全不变;前缀冲突(nav_→无前缀、nav_→app_、无前缀→nav_)均抛错且不建第二套;共享库他项目表/账本(eNotify_*、SL_*、ZETA_*、_sl_schema_migrations)不误判;失败注入时失败项不落账、后续不执行、重跑重试该项。auth.test.ts增 setup 响应账本字段与 409 冲突用例。本地结果:
合并关口(用户执行,团队不碰线上 D1)
重初始化时只删本项目表(生效前缀
nav_):nav_users、nav_passkey_credentials、nav_sessions、nav_login_attempts、nav_challenges、nav_categories、nav_links、nav_search_engines、nav_settings、_nav_schema_migrations顺序:删表 → 调
POST /api/auth/setup→ 核对_nav_schema_migrations恰含schema_v1→ 重建唯一所有者。不整库删除、不动eNotify_*/SL_*/ZETA_*/post_*。评审返工(ONE-363 打回,提交
4709dc2)detectTablePrefixConflict用前缀核比对(prefixCore(prefix).toLowerCase()),migrations.ts导出prefixCore与账本表名公式共用同一定义。存量nav_*库(有业务表、无账本)首跑不再被误判为冲突:正常识别、schema_v1记账为applied。e.g. TABLE_PREFIX=nav_),并注明扫到的账本可能属于共享 D1 的其他项目,不再建议sl_之类的他项目前缀。detectLedgerPrefixes不捕获空前缀账本,按评审结论保持现状(业务表族兜底闭环)。nav_*后删掉_nav_schema_migrations再跑,验证schema_v1正常 applied 并记账、不抛TablePrefixConflictError(未修复时该用例失败,已本地验证)。本地:
pnpm --filter worker test5 files / 39 tests passed;pnpm typecheck、pnpm lint(exit 0)、pnpm format:checkclean。