Skip to content

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

修复示例笔记保存失败

tianma-if25ed024c0cfe
diff --git a/migrations/0014_repair_seed_memo_content_hash.sql b/migrations/0014_repair_seed_memo_content_hash.sql
new file mode 100644
index 00000000..ee13c852
--- /dev/null
+++ b/migrations/0014_repair_seed_memo_content_hash.sql
@@ -0,0 +1,4 @@
+UPDATE memo_contents
+SET content_hash = '4edcf7092f86c561210d4eb788e96f28ec5c045f93952c82f5f57e1902f548bd'
+WHERE memo_id = 'memo_welcome'
+ AND content_hash = 'seed';
diff --git a/tests/seed-memo-migration.test.ts b/tests/seed-memo-migration.test.ts
new file mode 100644
index 00000000..9c0ff5c3
--- /dev/null
+++ b/tests/seed-memo-migration.test.ts
@@ -0,0 +1,58 @@
+import { createHash } from "node:crypto";
+import { globSync, readFileSync } from "node:fs";
+import { Database } from "bun:sqlite";
+import { describe, expect, test } from "bun:test";
+import { MemoUpdateSchema } from "../packages/shared/src/schemas";
+
+describe("seed memo migrations", () => {
+ test("repairs the legacy placeholder when an existing database upgrades", () => {
+ const db = new Database(":memory:");
+ const migrations = globSync("migrations/*.sql").sort();
+
+ for (const migration of migrations.filter((path) => !path.endsWith("0014_repair_seed_memo_content_hash.sql"))) {
+ db.exec(readFileSync(migration, "utf8"));
+ }
+
+ expect(
+ db.query(`SELECT content_hash FROM memo_contents WHERE memo_id = 'memo_welcome'`).get(),
+ ).toEqual({ content_hash: "seed" });
+
+ db.exec(readFileSync("migrations/0014_repair_seed_memo_content_hash.sql", "utf8"));
+
+ expect(
+ db.query(`SELECT length(content_hash) AS length FROM memo_contents WHERE memo_id = 'memo_welcome'`).get(),
+ ).toEqual({ length: 64 });
+ });
+
+ test("backfills a valid content hash for the bundled welcome memo", () => {
+ const db = new Database(":memory:");
+
+ for (const migration of globSync("migrations/*.sql").sort()) {
+ db.exec(readFileSync(migration, "utf8"));
+ }
+
+ const memo = db
+ .query(
+ `SELECT content_json, content_markdown, content_hash, revision
+ FROM memo_contents
+ WHERE memo_id = 'memo_welcome'`,
+ )
+ .get() as {
+ content_json: string;
+ content_markdown: string;
+ content_hash: string;
+ revision: number;
+ };
+ const expectedHash = createHash("sha256")
+ .update(memo.content_markdown + memo.content_json)
+ .digest("hex");
+
+ expect(memo.content_hash).toBe(expectedHash);
+ expect(
+ MemoUpdateSchema.safeParse({
+ expectedRevision: memo.revision,
+ expectedContentHash: memo.content_hash,
+ }).success,
+ ).toBe(true);
+ });
+});