Skip to content

ONE-203: three-state TABLE_PREFIX + silent-switch guard + CI none convention - #141

Merged
mapxn merged 2 commits into
mainfrom
agent/daniel/9566c723bd1d
Oct 25, 2026
Merged

mapxn merged 2 commits into
mainfrom
agent/daniel/9566c723bd1d

Conversation

@mapxn

@mapxn mapxn commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Implements the frozen three-state TABLE_PREFIX semantics for shortlink (父单 ONE-203 / 实现子单 ONE-205).

  • Single source of truth apps/worker/src/lib/table-prefix.ts: DEFAULT_TABLE_PREFIX = 'sl', resolveTablePrefix(), tbl().
  • Three-state resolution: undefined → sl; explicit '' → no prefix; custom value → as-is (case preserved, [^a-zA-Z0-9_] stripped).
  • Silent-switch guard (highest priority): migrations-runner.ts aborts with TablePrefixConflictError (surfaced as HTTP 409 by setup/admin routes) when the DB already holds an initialized schema under a different prefix. Deploy scripts (d1-prepare-migrations.sh / d1-prefixed-migrations.sh) mirror the guard.
  • CI none convention: new resolve-table-prefix.mjs resolves vars.TABLE_PREFIX — unset/empty → sl, none → '', else as-is — and deploy.yml passes the resolved value to every step that uses it.
  • Script adaptation: worker-table-prefix.sh now prints sl for an unset Worker binding; verify-deployment.sh compares literally (case-sensitive, no folding) so production SL is not confused with sl.
  • Tests + docs: three-state unit tests, guard tests (runtime + deploy), none-convention tests; README (en/zh) and CLAUDE.md updated with the three-state semantics + default sl + none convention + upgrade warning.

Test evidence (local)

  • pnpm check — OK (108 files, no issues)
  • pnpm typecheck — 2 packages successful
  • pnpm test — web 54 pass / worker 83 pass, 0 fail
  • shellcheck .github/scripts/*.sh — clean
  • node --test .github/scripts/test-d1-migrations.mjs .github/scripts/test-deploy-checks.mjs — 24 pass, 0 fail
  • pnpm build — 2 packages successful

Notes

  • No migrations/D1 writes performed; this change does not rename tables or alter data. Rollback = revert this PR (merge to main auto-deploys) or wrangler rollback the Worker version; tables stay SL_*.
  • Production baseline (TABLE_PREFIX=SL, _SL_schema_migrations with schema_v1/v2) is the "explicitly configured" branch — effective prefix unchanged.

mapxn added 2 commits October 25, 2026 08:45
…vention

Implement the frozen three-state prefix semantics with a single source of
truth (apps/worker/src/lib/table-prefix.ts):

- undefined → default 'sl'; '' → no prefix; custom value → as-is (case
  preserved, [^a-zA-Z0-9_] stripped)
- db.ts tbl() and migrations.ts tbl() both resolve through the shared helper
- migrations-runner resolves the effective prefix and aborts (409 via setup /
  admin routes) when the database already holds a schema under a different
  prefix — never silently switches
- deploy workflow resolves vars.TABLE_PREFIX three-state (unset/empty → sl,
  none → '', else as-is) and passes the resolved value to all scripts
- d1-prepare/d1-prefixed/worker-table-prefix/verify-deployment scripts adapted;
  an unset Worker binding now reads as the default 'sl'
- tests for the three-state resolution, the silent-switch guard, the none
  convention and the deploy scripts; docs updated (README en/zh, CLAUDE.md)
…d 2)

Address ONE-206 review blocking items — the guard treated "any foreign table"
as a shortlink schema switch, and "already-initialised under the current
prefix" as a conflict:

- The shared D1 database carries other apps' tables (bare users, ZETA_*,
  eNotify_*) and wrangler's d1_migrations. A bare `users` table alone is no
  longer a signal; unprefixed shortlink is recognised by `_schema_migrations`
  (containing schema_v1) or `d1_migrations`.
- When the current prefix is already initialised (`_<prefix>_schema_migrations`
  / `<prefix>_users`), migration is incremental and allowed — never aborted.
- Prefixed shortlink detection for the explicit-empty case now uses
  `^_(.+)_schema_migrations$` and, in the runtime, the intersection of
  shortlink-unique tables (passkeys + links + totp_used) — no single-table
  matching.
- Restore the shared-DB test fixture's bare `users` and add regression tests
  for both boundaries.
- Frontend: map the setup/admin 409 to a localized message that passes through
  the backend's actionable TABLE_PREFIX guidance (was falling into generic).
@mapxn
mapxn merged commit 2f0cd68 into main Oct 25, 2026
2 checks passed
@mapxn
mapxn deleted the agent/daniel/9566c723bd1d 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