From 14d20a8481ce46fedb36f698fcfbfde891b14d7f Mon Sep 17 00:00:00 2001 From: mapxn <16299874+mapxn@users.noreply.github.com> Date: Tue, 27 Oct 2026 11:40:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(ci):=20Worker=20=E4=B8=8D=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E6=97=B6=E6=8C=89=E8=B4=A6=E6=9C=AC=E6=94=BE=E8=A1=8C=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E5=AE=88=E5=8D=AB=EF=BC=8C=E5=B9=B6=E8=A1=A5=E5=9B=9E?= =?UTF-8?q?=E5=BD=92=E7=94=A8=E4=BE=8B=20(ONE-512)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit deployments status 失败时不再一律拒绝:__schema_migrations 存在即 视为本应用归属证明(Worker 被删、表与账本仍在的情形),打 warning 后继续幂等 迁移;带前缀表但无账本仍维持拒绝;deployments status 成功时的现役版本前缀比对 逻辑未改动。 test-d1-migrations.mjs 新增 D1_LOCATION=remote 场景,用既有 pnpm 替身做法 mock wrangler 控制面调用,覆盖「无表无历史放行 / 有账本放行 / 有表无账本拒绝」与 成功分支的前缀比对不回归。 --- .github/scripts/d1-prefixed-migrations.sh | 10 +- .github/scripts/test-d1-migrations.mjs | 163 +++++++++++++++++++++- 2 files changed, 170 insertions(+), 3 deletions(-) diff --git a/.github/scripts/d1-prefixed-migrations.sh b/.github/scripts/d1-prefixed-migrations.sh index ad0d3e6..23aab2d 100644 --- a/.github/scripts/d1-prefixed-migrations.sh +++ b/.github/scripts/d1-prefixed-migrations.sh @@ -13,6 +13,11 @@ # mistyped GitHub variable can't migrate another app's tables; # - prefixed tables without a migration history are never touched. # +# When the deployed Worker can't be read (it may not exist at all, e.g. after it +# was deleted), `__schema_migrations` itself proves that the prefixed +# tables belong to this app, so the run continues; prefix tables without that +# ledger keep failing. +# # D1_LOCATION=local targets the local D1 database instead (for testing this # script); the deployed-Worker check is skipped then. set -euo pipefail @@ -91,7 +96,10 @@ if [[ "$location" == remote ]]; then fi done echo "Deployed Worker uses TABLE_PREFIX=$prefix (version(s): $(tr '\n' ' ' <<<"$versions"))." - elif [[ "$has_history" == false && -z "$own_tables" ]]; then + elif [[ "$has_history" == true ]]; then + cat "$err" >&2 + echo "::warning::Could not read the deployed Worker (it may not exist yet, e.g. after it was deleted). $history exists, so the ${prefix}_ tables belong to this app; continuing with the idempotent migration run." + elif [[ -z "$own_tables" ]]; then cat "$err" >&2 echo "::warning::Could not read the deployed Worker (first deploy?). The database has no ${prefix}_ tables yet, so creating them is safe." else diff --git a/.github/scripts/test-d1-migrations.mjs b/.github/scripts/test-d1-migrations.mjs index 4304f11..2e6753e 100644 --- a/.github/scripts/test-d1-migrations.mjs +++ b/.github/scripts/test-d1-migrations.mjs @@ -3,6 +3,7 @@ import assert from 'node:assert/strict'; import { execFileSync } from 'node:child_process'; import { + mkdirSync, mkdtempSync, readFileSync, rmSync, @@ -72,8 +73,57 @@ migrations_dir = "migrations" sql, ]), ).map((r) => r.results); - const script = (name, prefix) => - run('bash', [join(scripts, name)], { TABLE_PREFIX: prefix }); + const script = (name, prefix, extraEnv = {}) => + run('bash', [join(scripts, name)], { + TABLE_PREFIX: prefix, + ...extraEnv, + }); + // 控制面替身(沿用 test-deploy-checks.mjs 的 pnpm 替身做法):只接管 wrangler 的 + // `deployments status` / `versions view`,其余调用原样交给真实 pnpm,并把 + // `--remote` 改写为 `--local`,让 D1 查询依然落到本 fixture 的本地库。 + // 用于跑 D1_LOCATION=remote 分支(Worker 存在与否的分情形判定)。 + const mockControlPlane = ({ status, prefix = 'SL' }) => { + const bin = join(cwd, 'mock-bin'); + mkdirSync(bin, { recursive: true }); + const realPnpm = execFileSync('bash', ['-c', 'command -v pnpm'], { + encoding: 'utf8', + }).trim(); + const statusCall = + status === 'missing' + ? ` printf '%s\\n' '{"error":{"code":10007,"text":"workers.api.error.script_not_found"}}' >&2 + exit 1` + : ` printf '%s\\n' '${JSON.stringify({ + versions: [{ version_id: 'live-version', percentage: 100 }], + })}' + exit 0`; + const bindings = + prefix === undefined + ? [] + : [{ name: 'TABLE_PREFIX', type: 'plain_text', text: prefix }]; + writeFileSync( + join(bin, 'pnpm'), + `#!/usr/bin/env bash +if [[ "$*" == 'exec wrangler deployments status --json' ]]; then +${statusCall} +fi +if [[ "$1 $2 $3" == 'exec wrangler versions' ]]; then + printf '%s\\n' '${JSON.stringify({ + id: 'live-version', + resources: { bindings }, + })}' + exit 0 +fi +args=() +for arg in "$@"; do + [[ "$arg" == --remote ]] && arg=--local + args+=("$arg") +done +exec '${realPnpm}' "\${args[@]}" +`, + { mode: 0o755 }, + ); + return { PATH: `${bin}:${process.env.PATH}` }; + }; const setup = (prefix, names) => { query( `${createMigrationsTableSql(prefix)};${getMigrations(prefix) @@ -138,6 +188,7 @@ migrations_dir = "migrations" run, query, script, + mockControlPlane, setup, shared, wranglerHistory, @@ -381,3 +432,111 @@ test('回归:d1_migrations 含本应用迁移文件名 + 非空前缀 → 仍 ); assert.deepEqual(f.foreignSnapshot(), before); }); + +// ─── 改动 B:Worker 不存在(deployments status 失败)分情形放行 ─────────────── + +// 用 pnpm 替身让 `wrangler deployments status` 报 10007(Worker 不存在), +// D1 查询仍打到本地库,于是走真实的 remote 分支判定。 +for (const scenario of [ + { + name: '无表无历史 + 自定义前缀 → 放行建表(既有行为)', + prepare: () => {}, + ok: true, + }, + { + name: '带前缀表 + 有账本历史 → 放行(改动 B 新行为)', + prepare: (f) => f.setup('SL', ['schema_v1']), + ok: true, + }, + { + name: '带前缀表 + 无账本历史 → 仍拒绝(守卫底线)', + prepare: (f) => f.query('CREATE TABLE SL_users (id TEXT)'), + ok: false, + }, +]) { + test(`Worker 不存在(deployments status 失败):${scenario.name}`, (t) => { + const f = fixture(t); + const before = f.shared(); + scenario.prepare(f); + const run = () => + f.script('d1-prefixed-migrations.sh', 'SL', { + D1_LOCATION: 'remote', + ...f.mockControlPlane({ status: 'missing' }), + }); + + if (!scenario.ok) { + assert.throws(run, (err) => + /refusing to migrate existing SL_ tables/.test(err.stdout ?? ''), + ); + // 拒绝时库里不得留下任何本应用的痕迹。 + assert.deepEqual( + f.query( + `SELECT name FROM sqlite_master WHERE type = 'table' + AND (name GLOB 'SL_*' OR name GLOB '_SL_*') AND name != 'SL_users'`, + )[0], + [], + ); + assert.deepEqual(f.foreignSnapshot(), before); + return; + } + + const output = run(); + assert.match(output, /::warning::Could not read the deployed Worker/); + assert.doesNotMatch(output, /::error::/); + assert.deepEqual( + f.query('SELECT name FROM _SL_schema_migrations ORDER BY name')[0], + [{ name: 'schema_v1' }, { name: 'schema_v2' }], + ); + f.assertOwnIndexes('SL'); + assert.deepEqual(f.foreignSnapshot(), before); + // 幂等续跑:第二次只剩空计划。 + assert.match( + f.script('d1-prefixed-migrations.sh', 'SL', { + D1_LOCATION: 'remote', + ...f.mockControlPlane({ status: 'missing' }), + }), + /Planned migrations: none/, + ); + }); +} + +// 回归:deployments status 成功时的现役版本前缀比对逻辑不变。 +for (const scenario of [ + { name: '现役前缀与 GitHub 变量一致 → 放行', live: 'SL', ok: true }, + { name: '现役前缀与 GitHub 变量不一致 → 拒绝', live: 'ZETA', ok: false }, +]) { + test(`Worker 存在(deployments status 成功):${scenario.name}`, (t) => { + const f = fixture(t); + const before = f.shared(); + const run = () => + f.script('d1-prefixed-migrations.sh', 'SL', { + D1_LOCATION: 'remote', + ...f.mockControlPlane({ status: 'live', prefix: scenario.live }), + }); + + if (!scenario.ok) { + assert.throws(run, (err) => + new RegExp( + `Deployed Worker version live-version uses TABLE_PREFIX='${scenario.live}'`, + ).test(err.stdout ?? ''), + ); + assert.deepEqual( + f.query( + `SELECT name FROM sqlite_master WHERE type = 'table' + AND (name GLOB 'SL_*' OR name GLOB '_SL_*')`, + )[0], + [], + ); + assert.deepEqual(f.foreignSnapshot(), before); + return; + } + + assert.match(run(), /Deployed Worker uses TABLE_PREFIX=SL/); + assert.deepEqual( + f.query('SELECT name FROM _SL_schema_migrations ORDER BY name')[0], + [{ name: 'schema_v1' }, { name: 'schema_v2' }], + ); + f.assertOwnIndexes('SL'); + assert.deepEqual(f.foreignSnapshot(), before); + }); +}