Skip to content

部署流程:冒烟测试兼容 Bot Fight Mode + 带表前缀时自动执行迁移 - #120

Merged
todosdev[bot] merged 3 commits into
mainfrom
tds/conv-01a149eb-30e6-75f5-81c6-8914981443e6
Oct 17, 2026
Merged

todosdev[bot] merged 3 commits into
mainfrom
tds/conv-01a149eb-30e6-75f5-81c6-8914981443e6

Conversation

@todosdev

@todosdev todosdev Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • The post-deploy smoke test no longer fails when Cloudflare Bot Fight Mode challenges the GitHub Actions runner. A new check through the Cloudflare API confirms the uploaded Worker version is serving 100% of production traffic; the public hostname's bot protection can't block it.
  • When TABLE_PREFIX is set, the deploy now applies prefixed migrations automatically before deploying the code. The migrations come from the same source as POST /setup, and are recorded in _<prefix>_schema_migrations in the same format as the setup route. Before, the deploy skipped migrations, so schema changes had to be applied by hand in the D1 console.
  • The deploy now refuses to touch any table that doesn't carry this app's prefix. The production D1 database is shared with ZETA, eNotify, TP and an unprefixed users table.

Changes

  • Worker (refactor only, no behaviour change)
    • apps/worker/src/lib/migrations.ts: getMigrations(prefix), tbl(), migrationsTable() and createMigrationsTableSql(), moved out of routes/setup.ts unchanged.
    • routes/setup.ts now imports them. The module has no imports and no TS-only syntax, so Node can load it directly.
  • Migration scripts (.github/scripts/)
    • d1-migrations.mjs imports the migrations module and prints the SQL for a prefix. It only allows CREATE TABLE/INDEX IF NOT EXISTS, ALTER TABLE … ADD and INSERT OR IGNORE, and every statement must target a table with the app's prefix. Anything else aborts the deploy.
    • d1-prefixed-migrations.sh prints the migration history and the planned migrations, then applies only the pending ones.
      • Each migration and its history row go in a single request.
      • Remote runs first require every live Worker version to have the same TABLE_PREFIX as the GitHub variable.
      • It refuses to run when SL_ tables exist but have no schema_v1 record.
    • d1-prepare-migrations.sh outputs mode=prefixed when TABLE_PREFIX is set (previously skip). The unprefixed wrangler path is unchanged. It also accepts D1_LOCATION=local for testing.
    • worker-table-prefix.sh reads the TABLE_PREFIX plain-text binding from a Worker version. It fails if the binding is a secret, because the value can't be read back to compare.
  • Deployment checks
    • verify-deployment.sh reads the version_id that wrangler deploy writes to WRANGLER_OUTPUT_FILE_PATH. It then checks, with retries, that wrangler deployments status --json shows that version at 100%, and that its TABLE_PREFIX matches.
    • smoke-test.sh makes one curl request per attempt so status, headers and body stay together.
      • A 403 with cf-mitigated: challenge produces a ::warning::.
      • Any other non-2xx response, or a JSON body that fails the check, fails the step.
  • Workflows
    • deploy.yml:
      • adds an "Apply prefixed D1 migrations" step;
      • Deploy now runs from apps/worker with WRANGLER_OUTPUT_FILE_PATH set;
      • adds a "Verify deployment" step;
      • CHECK_DB (the /api/config check) now also runs in prefixed mode;
      • the rollback hint also fires when verification fails.
    • ci.yml runs shellcheck on the deploy scripts and the deploy-script tests. They need no Cloudflare credentials.
  • Docs: Section 6 of README.md and README.zh-CN.md now describes:
    • automatic migrations before deploy in both modes;
    • that TABLE_PREFIX must be a plain-text Worker variable matching the GitHub production variable;
    • the required token scopes;
    • /setup as optional;
    • how a Bot Fight Mode challenge is handled.

Testing

  • node --test .github/scripts/test-d1-migrations.mjs .github/scripts/test-deploy-checks.mjs: all 17 tests pass locally. The D1 tests use a throwaway local D1 database.
    • Unprefixed: the wrangler path applies and re-runs cleanly, and a database bootstrapped through the setup route is baselined.
    • Prefixed, fully applied: prints an empty plan, and a bash trace shows only SELECT queries ran.
    • Prefixed, schema_v2 pending: applies only v2, existing rows get token_version = 0, and a second run is a no-op.
    • Prefixed, fresh install: creates only SL_* tables.
    • Existing SL_ tables with no or empty history: the script refuses to run.
    • Shared-database fixtures: the unprefixed users table and ZETA_* tables (schema, indexes and data) are unchanged in every scenario.
    • Indexes: every index the migrations declare exists on its SL_* table. A separate test reproduces the known index-name collision and shows this assertion catches it.
    • Smoke test (against a local HTTP server): 200 passes. A 403 with cf-mitigated: challenge warns and still probes every path. A plain 403, a 500 (even with the challenge header), a 302, and a 200 with a bad config body all fail.
    • Verify deployment (with a stub wrangler): passes when the new version serves 100%. Fails when the old version is still active, when the new version serves only part of the traffic, or when the prefix doesn't match.
  • actionlint (includes shellcheck), shellcheck .github/scripts/*.sh, pnpm check, pnpm typecheck --force and pnpm build --force all pass.
  • Not run against production; no Cloudflare credentials were available. Given production's _SL_schema_migrations, the first prefixed run should print Planned migrations: none and write nothing. Nobody has checked the table's contents yet. If schema_v1 is missing, the script refuses to run rather than guessing.
  • Index note (not fixed here; no schema change in this PR): index names are not prefixed. Per the production index list the user queried, all 12 of shortlink's explicit indexes currently sit on SL_* tables, so nothing is affected today. The risk is a second shortlink install, or another app using the same index name, in the same database. Fixing it would mean a new migration that adds prefixed index names, which the user can decide on.
  • Before deploying: the Worker's TABLE_PREFIX must be a plain-text variable, not a secret, and match the GitHub production value SL. CLOUDFLARE_API_TOKEN needs D1 edit plus Worker edit/version-read scope.

Todos added 3 commits October 17, 2026 21:12
- Add isolated local D1 tests for migration plans and shared database safety.
- Test challenge handling and active-version checks without production access.

Checkpoint: 30_Qk0mFgDFOpIHz4McVL
- Restore English text in README.md section 6 (Chinese stays in README.zh-CN.md).
- Use non-colliding index names in the shared-DB test fixture and assert every
  migration index exists on the prefixed table; document the index-name collision.
- Run shellcheck and the deploy-script tests in the CI workflow.

Checkpoint: 7jPqyGVbPXMuvvXcSuaR7
@todosdev
todosdev Bot merged commit faf072b into main Oct 17, 2026
0 of 2 checks passed
@mapxn
mapxn deleted the tds/conv-01a149eb-30e6-75f5-81c6-8914981443e6 branch October 18, 2026 08:56
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.

0 participants