AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(notebooks): collapse duplicate inbox notebooks from 0046
tianma-if086d85d9d793
diff --git a/apps/api/src/notebook-service.test.mjs b/apps/api/src/notebook-service.test.mjsindex 5dc1316e..25eb1cd6 100644--- a/apps/api/src/notebook-service.test.mjs+++ b/apps/api/src/notebook-service.test.mjs@@ -89,6 +89,137 @@ describe("notebook identity", () => {}});+ test("migration 0053 collapses a second inbox created beside a historical slug inbox", () => {+ const sqlite = new Database(":memory:");+ try {+ sqlite.exec("PRAGMA foreign_keys = ON");+ for (const file of globSync("migrations/*.sql").sort().filter((file) => file.split("/").at(-1) < "0046")) {+ sqlite.exec(readFileSync(file, "utf8"));+ }+ sqlite.exec(`+ INSERT INTO notebooks (id, workspace_id, parent_id, name, slug, icon, color, sort_order)+ VALUES (+ 'nb_7f802977e3cc4e8eb30ef3665955ed39',+ 'ws_default',+ NULL,+ '等待分类',+ 'inbox',+ 'notebook',+ '#0f766e',+ 10+ );+ UPDATE memos SET notebook_id = 'nb_7f802977e3cc4e8eb30ef3665955ed39' WHERE notebook_id = 'nb_inbox';+ DELETE FROM notebooks WHERE id = 'nb_inbox';+ INSERT INTO memos (id, workspace_id, notebook_id, title, excerpt)+ VALUES (+ 'memo_historical_inbox',+ 'ws_default',+ 'nb_7f802977e3cc4e8eb30ef3665955ed39',+ 'Kept',+ 'kept'+ );+ `);+ sqlite.exec(readFileSync("migrations/0046_restore_workspace_inbox_identity.sql", "utf8"));+ expect(+ sqlite.query(`+ SELECT id, is_deleted FROM notebooks+ WHERE workspace_id = 'ws_default'+ AND (slug = 'inbox' OR id = 'nb_inbox' OR id = workspace_id || '_inbox')+ ORDER BY id+ `).all(),+ ).toEqual([+ { id: "nb_7f802977e3cc4e8eb30ef3665955ed39", is_deleted: 0 },+ { id: "ws_default_inbox", is_deleted: 0 },+ ]);++ sqlite.exec(readFileSync("migrations/0053_collapse_duplicate_inbox_notebooks.sql", "utf8"));+ sqlite.exec(readFileSync("migrations/0053_collapse_duplicate_inbox_notebooks.sql", "utf8"));++ expect(+ sqlite.query(`+ SELECT id, is_deleted FROM notebooks+ WHERE workspace_id = 'ws_default'+ AND (slug = 'inbox' OR id = 'nb_inbox' OR id = workspace_id || '_inbox')+ ORDER BY is_deleted, id+ `).all(),+ ).toEqual([+ { id: "nb_7f802977e3cc4e8eb30ef3665955ed39", is_deleted: 0 },+ { id: "ws_default_inbox", is_deleted: 1 },+ ]);+ expect(+ sqlite.query(`+ SELECT notebook_id, COUNT(*) AS count+ FROM memos+ WHERE workspace_id = 'ws_default' AND is_deleted = 0+ GROUP BY notebook_id+ `).all(),+ ).toEqual([+ { notebook_id: "nb_7f802977e3cc4e8eb30ef3665955ed39", count: 2 },+ ]);+ expect(sqlite.query("SELECT COUNT(*) AS count FROM notebooks WHERE is_deleted = 0 AND slug = 'inbox'").get()).toEqual({+ count: 1,+ });+ expect(+ sqlite.query(`+ SELECT name FROM sqlite_master+ WHERE type = 'index' AND name = 'idx_notebooks_one_active_inbox'+ `).get(),+ ).toEqual({ name: "idx_notebooks_one_active_inbox" });+ expect(() => sqlite.exec(`+ INSERT INTO notebooks (id, workspace_id, name, slug)+ VALUES ('nb_second_inbox', 'ws_default', '等待分类', 'inbox')+ `)).toThrow(/unique/i);+ expect(sqlite.query("PRAGMA foreign_key_check").all()).toEqual([]);+ } finally {+ sqlite.close();+ }+ });++ test("migration 0053 moves notes off the empty duplicate and keeps child notebooks", () => {+ const sqlite = new Database(":memory:");+ try {+ sqlite.exec("PRAGMA foreign_keys = ON");+ for (const file of globSync("migrations/*.sql").sort().filter((file) => file.split("/").at(-1) < "0053")) {+ sqlite.exec(readFileSync(file, "utf8"));+ }+ sqlite.exec(`+ INSERT INTO notebooks (id, workspace_id, parent_id, name, slug, icon, color, sort_order)+ VALUES+ ('ws_default_inbox', 'ws_default', NULL, '等待分类', 'inbox', 'notebook', '#0f766e', 10),+ ('nb_under_duplicate', 'ws_default', 'ws_default_inbox', 'Nested', 'nested', 'notebook', '#0f766e', 20);+ INSERT INTO memos (id, workspace_id, notebook_id, title, excerpt)+ VALUES+ ('memo_on_original', 'ws_default', 'nb_inbox', 'Keep me', 'keep'),+ ('memo_on_duplicate', 'ws_default', 'ws_default_inbox', 'Move me', 'move');+ `);+ sqlite.exec(readFileSync("migrations/0053_collapse_duplicate_inbox_notebooks.sql", "utf8"));++ expect(+ sqlite.query(`+ SELECT id FROM notebooks+ WHERE workspace_id = 'ws_default' AND is_deleted = 0+ AND (slug = 'inbox' OR id = 'nb_inbox' OR id = workspace_id || '_inbox')+ `).all(),+ ).toEqual([{ id: "nb_inbox" }]);+ expect(+ sqlite.query("SELECT notebook_id FROM memos WHERE id IN ('memo_on_duplicate', 'memo_on_original') ORDER BY id").all(),+ ).toEqual([+ { notebook_id: "nb_inbox" },+ { notebook_id: "nb_inbox" },+ ]);+ expect(sqlite.query("SELECT parent_id, is_deleted FROM notebooks WHERE id = 'nb_under_duplicate'").get()).toEqual({+ parent_id: "nb_inbox",+ is_deleted: 0,+ });+ expect(sqlite.query("SELECT is_deleted FROM notebooks WHERE id = 'ws_default_inbox'").get()).toEqual({+ is_deleted: 1,+ });+ expect(sqlite.query("PRAGMA foreign_key_check").all()).toEqual([]);+ } finally {+ sqlite.close();+ }+ });+test("rejects deleting a workspace inbox after it was renamed", async () => {const fixture = createFixture();try {diff --git a/migrations/0053_collapse_duplicate_inbox_notebooks.sql b/migrations/0053_collapse_duplicate_inbox_notebooks.sqlnew file mode 100644index 00000000..7ec520dc--- /dev/null+++ b/migrations/0053_collapse_duplicate_inbox_notebooks.sql@@ -0,0 +1,114 @@+PRAGMA foreign_keys = ON;++-- 0046 restored a workspace-scoped inbox id, but only looked for `nb_inbox`+-- or `{workspace}_inbox`. Workspaces whose inbox already existed under a+-- different id (slug `inbox`) got a second empty "等待分类". Keep one active+-- inbox per workspace: prefer the notebook that already has notes.+CREATE TABLE _inbox_dedup_canonical (+ workspace_id TEXT PRIMARY KEY,+ id TEXT NOT NULL+);++INSERT INTO _inbox_dedup_canonical (workspace_id, id)+SELECT n.workspace_id, n.id+FROM notebooks n+WHERE n.is_deleted = 0+ AND (n.slug = 'inbox' OR n.id = 'nb_inbox' OR n.id = n.workspace_id || '_inbox')+ AND n.id = (+ SELECT keep.id+ FROM notebooks keep+ WHERE keep.workspace_id = n.workspace_id+ AND keep.is_deleted = 0+ AND (keep.slug = 'inbox' OR keep.id = 'nb_inbox' OR keep.id = keep.workspace_id || '_inbox')+ ORDER BY+ (SELECT COUNT(*) FROM memos m WHERE m.notebook_id = keep.id AND m.is_deleted = 0) DESC,+ CASE+ WHEN keep.id = keep.workspace_id || '_inbox' THEN 0+ WHEN keep.id = 'nb_inbox' THEN 1+ ELSE 2+ END,+ keep.created_at ASC,+ keep.id ASC+ LIMIT 1+ );++UPDATE notebooks+SET+ parent_id = (+ SELECT canonical.id+ FROM _inbox_dedup_canonical canonical+ WHERE canonical.workspace_id = notebooks.workspace_id+ ),+ updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')+WHERE parent_id IN (+ SELECT extra.id+ FROM notebooks extra+ JOIN _inbox_dedup_canonical canonical+ ON canonical.workspace_id = extra.workspace_id+ WHERE extra.is_deleted = 0+ AND (extra.slug = 'inbox' OR extra.id = 'nb_inbox' OR extra.id = extra.workspace_id || '_inbox')+ AND extra.id <> canonical.id+);++UPDATE memos+SET notebook_id = (+ SELECT canonical.id+ FROM _inbox_dedup_canonical canonical+ WHERE canonical.workspace_id = memos.workspace_id+)+WHERE notebook_id IN (+ SELECT extra.id+ FROM notebooks extra+ JOIN _inbox_dedup_canonical canonical+ ON canonical.workspace_id = extra.workspace_id+ WHERE extra.is_deleted = 0+ AND (extra.slug = 'inbox' OR extra.id = 'nb_inbox' OR extra.id = extra.workspace_id || '_inbox')+ AND extra.id <> canonical.id+);++UPDATE companion_memories+SET scope_notebook_id = (+ SELECT canonical.id+ FROM _inbox_dedup_canonical canonical+ WHERE canonical.workspace_id = companion_memories.workspace_id+)+WHERE scope_notebook_id IN (+ SELECT extra.id+ FROM notebooks extra+ JOIN _inbox_dedup_canonical canonical+ ON canonical.workspace_id = extra.workspace_id+ WHERE extra.is_deleted = 0+ AND (extra.slug = 'inbox' OR extra.id = 'nb_inbox' OR extra.id = extra.workspace_id || '_inbox')+ AND extra.id <> canonical.id+);++UPDATE companion_actions+SET result_notebook_id = (+ SELECT canonical.id+ FROM _inbox_dedup_canonical canonical+ WHERE canonical.workspace_id = companion_actions.workspace_id+)+WHERE result_notebook_id IN (+ SELECT extra.id+ FROM notebooks extra+ JOIN _inbox_dedup_canonical canonical+ ON canonical.workspace_id = extra.workspace_id+ WHERE extra.is_deleted = 0+ AND (extra.slug = 'inbox' OR extra.id = 'nb_inbox' OR extra.id = extra.workspace_id || '_inbox')+ AND extra.id <> canonical.id+);++UPDATE notebooks+SET+ is_deleted = 1,+ deleted_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),+ updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')+WHERE is_deleted = 0+ AND (slug = 'inbox' OR id = 'nb_inbox' OR id = workspace_id || '_inbox')+ AND id NOT IN (SELECT id FROM _inbox_dedup_canonical);++DROP TABLE _inbox_dedup_canonical;++CREATE UNIQUE INDEX IF NOT EXISTS idx_notebooks_one_active_inbox+ ON notebooks(workspace_id)+ WHERE is_deleted = 0 AND slug = 'inbox';