Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix: apply D1 migrations non-interactively

tianma-ifd0a9c918db85
diff --git a/docs/manual-deploy.md b/docs/manual-deploy.md
index ab3b4031..7e86c3ca 100644
--- a/docs/manual-deploy.md
+++ b/docs/manual-deploy.md
@@ -69,7 +69,7 @@ Before running `bun run deploy`, copy the D1 `database_id` and R2 bucket name in
`.env.local` is read only by the local EdgeEver scripts. It is not automatically uploaded to Cloudflare as a file, and it must never be uploaded manually or committed to Git. A build started directly from Cloudflare Dashboard does not automatically receive the D1, R2, or password settings in this file. Use `bun run deploy` for the first deployment, then use `bun run deploy:builds:setup` to copy the required settings securely into Workers Builds.
-The deployment command now runs `deploy:doctor` first and stops when the D1 ID, R2 bucket, or login password is missing. After deployment, `deploy:verify` checks the remote D1 schema and the Worker's authentication Secret; a missing requirement makes deployment exit as failed. The Worker also fails closed: when a production instance has neither a password Secret nor an enabled user, it returns `auth_not_configured` instead of opening an unauthenticated workspace. `/api/health` checks D1 and authentication initialization; the instance is ready only when it returns `200` with `"ok": true`.
+The deployment command now runs `deploy:doctor` first and stops when the D1 ID, R2 bucket, or login password is missing. Remote D1 migrations run automatically in non-interactive mode, so no confirmation is required and a cancelled Wrangler prompt cannot be mistaken for success. After deployment, `deploy:verify` checks the remote D1 schema and the Worker's authentication Secret; a missing requirement makes deployment exit as failed. The Worker also fails closed: when a production instance has neither a password Secret nor an enabled user, it returns `auth_not_configured` instead of opening an unauthenticated workspace. `/api/health` checks D1 and authentication initialization; the instance is ready only when it returns `200` with `"ok": true`.
Existing installations do not need to migrate. If you intentionally switch from the hash setting to `EDGE_EVER_AUTH_PASSWORD`, remove the old `EDGE_EVER_AUTH_PASSWORD_HASH` from `.env.local`, Workers Builds, and the Worker's runtime Secrets; otherwise the legacy hash remains authoritative.
diff --git a/docs/manual-deploy.zh-CN.md b/docs/manual-deploy.zh-CN.md
index 1a5b61e5..05d6bac4 100644
--- a/docs/manual-deploy.zh-CN.md
+++ b/docs/manual-deploy.zh-CN.md
@@ -69,7 +69,7 @@ bun run deploy
`.env.local` 只供本机 EdgeEver 脚本读取,不会作为文件自动上传到 Cloudflare,也不应手工上传或提交到 Git。若从 Cloudflare Dashboard 直接触发构建,该文件中的 D1、R2 和密码配置不会自动生效;请使用 `bun run deploy` 完成首次部署,再使用 `bun run deploy:builds:setup` 将必要配置安全地写入 Workers Builds。
-部署命令会先自动执行 `deploy:doctor`,缺少 D1 ID、R2 bucket 或登录密码时会直接停止。部署完成后还会执行 `deploy:verify`,检查远程 D1 的关键数据表和 Worker 鉴权 Secret;任一缺失都会让部署以失败退出。Worker 也采用安全关闭策略:生产实例既没有密码 Secret、又没有有效用户时,会返回 `auth_not_configured`,不会降级成免登录工作区。`/api/health` 会检查 D1 和鉴权初始化状态;只有返回 `200` 且包含 `"ok": true` 才表示实例真正可用。
+部署命令会先自动执行 `deploy:doctor`,缺少 D1 ID、R2 bucket 或登录密码时会直接停止。远程 D1 migration 会以非交互模式自动执行,不需要用户确认,避免取消操作被 Wrangler 误报为成功。部署完成后还会执行 `deploy:verify`,检查远程 D1 的关键数据表和 Worker 鉴权 Secret;任一缺失都会让部署以失败退出。Worker 也采用安全关闭策略:生产实例既没有密码 Secret、又没有有效用户时,会返回 `auth_not_configured`,不会降级成免登录工作区。`/api/health` 会检查 D1 和鉴权初始化状态;只有返回 `200` 且包含 `"ok": true` 才表示实例真正可用。
已有实例无需迁移。如果确实要从哈希配置切换为 `EDGE_EVER_AUTH_PASSWORD`,需要同时从 `.env.local`、Workers Builds 和 Worker 运行时 Secrets 中移除旧的 `EDGE_EVER_AUTH_PASSWORD_HASH`,否则旧哈希仍会优先生效。
diff --git a/scripts/verify-deployment.mjs b/scripts/verify-deployment.mjs
index 90f10dd3..e4cea1fa 100644
--- a/scripts/verify-deployment.mjs
+++ b/scripts/verify-deployment.mjs
@@ -55,7 +55,9 @@ const main = () => {
const tableNames = new Set(parseD1Rows(schemaOutput).map((row) => row.name));
const missingTables = REQUIRED_TABLES.filter((table) => !tableNames.has(table));
if (missingTables.length > 0) {
- throw new Error(`Remote D1 migrations are incomplete; missing tables: ${missingTables.join(", ")}.`);
+ throw new Error(
+ `Remote D1 migrations are incomplete; missing tables: ${missingTables.join(", ")}. Run bun run db:migrate:remote and retry deployment.`,
+ );
}
console.log(`[ok] remote D1 schema: ${REQUIRED_TABLES.length} required tables`);
diff --git a/scripts/wrangler-runner.mjs b/scripts/wrangler-runner.mjs
index 4331a9a9..13878888 100644
--- a/scripts/wrangler-runner.mjs
+++ b/scripts/wrangler-runner.mjs
@@ -10,6 +10,16 @@ export const buildWranglerInvocation = (args, options = {}) => ({
args: [resolveWranglerCliPath(options.cwd), ...args],
});
+export const isD1MigrationApplyCommand = (args) => {
+ const command = args.join(" ");
+ return /(?:^|\s)d1\s+migrations\s+apply(?:\s|$)/.test(command);
+};
+
+export const buildWranglerEnvironment = (args, env = process.env) => ({
+ ...env,
+ ...(isD1MigrationApplyCommand(args) ? { CI: "true" } : {}),
+});
+
export const runWranglerSync = (args, options = {}) => {
const cwd = options.cwd ?? resolve(".");
const cliPath = resolveWranglerCliPath(cwd);
@@ -28,5 +38,6 @@ export const runWranglerSync = (args, options = {}) => {
cwd,
shell: false,
...spawnOptions,
+ env: buildWranglerEnvironment(args, spawnOptions.env),
});
};
diff --git a/tests/wrangler-runner.test.ts b/tests/wrangler-runner.test.ts
index 6d3e92b7..9fd1e306 100644
--- a/tests/wrangler-runner.test.ts
+++ b/tests/wrangler-runner.test.ts
@@ -1,6 +1,8 @@
import { describe, expect, test } from "bun:test";
import {
buildWranglerInvocation,
+ buildWranglerEnvironment,
+ isD1MigrationApplyCommand,
resolveWranglerCliPath,
runWranglerSync,
} from "../scripts/wrangler-runner.mjs";
@@ -24,4 +26,15 @@ describe("cross-platform Wrangler runner", () => {
expect(result.status).toBe(0);
expect(result.stdout).toMatch(/^\d+\.\d+\.\d+/);
});
+
+ test("forces D1 migration apply into non-interactive CI mode", () => {
+ const args = ["--config", "generated.toml", "d1", "migrations", "apply", "DB", "--remote"];
+
+ expect(isD1MigrationApplyCommand(args)).toBe(true);
+ expect(buildWranglerEnvironment(args, { EXISTING: "value" })).toMatchObject({
+ EXISTING: "value",
+ CI: "true",
+ });
+ expect(buildWranglerEnvironment(["d1", "migrations", "list", "DB"], {})).not.toHaveProperty("CI");
+ });
});