Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: fix `database disk image is malformed` error on updating deleted content

Abdullah Atta5dbabc2706c0
diff --git a/packages/core/__tests__/fts-triggers.test.ts b/packages/core/__tests__/fts-triggers.test.ts
new file mode 100644
index 000000000..b0d87d841
--- /dev/null
+++ b/packages/core/__tests__/fts-triggers.test.ts
@@ -0,0 +1,89 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { expect, test } from "vitest";
+import { databaseTest } from "./utils";
+
+test("updating deleted content should not throw", () =>
+ databaseTest().then(async (db) => {
+ const id = await db.notes.add({
+ title: "New note"
+ });
+
+ const contentId = await db.content.add({
+ data: "helloworld",
+ noteId: id
+ });
+
+ await db.content.remove(contentId!);
+
+ await expect(
+ db.content.collection.update(
+ [contentId!],
+ { synced: true },
+ { sendEvent: false }
+ )
+ ).resolves.toBeFalsy();
+ }));
+
+test("updating content should not break full text search", () =>
+ databaseTest().then(async (db) => {
+ const id = await db.notes.add({
+ title: "New note"
+ });
+
+ const contentId = await db.content.add({
+ data: "hello world",
+ noteId: id
+ });
+
+ await db.content.add({
+ id: contentId,
+ data: "i am amazing",
+ noteId: id
+ });
+
+ expect(await db.lookup.notes("amazing").ids()).toContain(id);
+ expect(await db.lookup.notes("hello world").ids()).not.toContain(id);
+ }));
+
+test("updating note title should not break full text search", () =>
+ databaseTest().then(async (db) => {
+ const id = await db.notes.add({
+ title: "New note"
+ });
+ await db.notes.add({
+ id,
+ title: "What an amazing note!"
+ });
+ expect(await db.lookup.notes("amazing").ids()).toContain(id);
+ expect(await db.lookup.notes("new note").ids()).not.toContain(id);
+ }));
+
+test("updating deleted note should not throw", () =>
+ databaseTest().then(async (db) => {
+ const id = await db.notes.add({
+ title: "New note"
+ });
+ await db.notes.remove(id);
+
+ await expect(
+ db.notes.collection.update([id], { synced: true }, { sendEvent: false })
+ ).resolves.toBeFalsy();
+ }));
diff --git a/packages/core/src/database/triggers.ts b/packages/core/src/database/triggers.ts
index 7d76aff58..295da9734 100644
--- a/packages/core/src/database/triggers.ts
+++ b/packages/core/src/database/triggers.ts
@@ -31,8 +31,8 @@ export async function createTriggers(db: Kysely<RawDatabaseSchema>) {
.addEvent("insert")
.when((eb) =>
eb.and([
- eb.or([eb("new.deleted", "is", null), eb("new.deleted", "==", false)]),
- eb.or([eb("new.locked", "is", null), eb("new.locked", "==", false)]),
+ eb("new.deleted", "is not", true),
+ eb("new.locked", "is not", true),
eb("new.data", "is not", null)
])
)
@@ -71,6 +71,13 @@ export async function createTriggers(db: Kysely<RawDatabaseSchema>) {
.onTable("content", "main")
.after()
.addEvent("update")
+ .when((eb) =>
+ eb.and([
+ eb("old.deleted", "is not", true),
+ eb("old.noteId", "is not", null),
+ eb("old.data", "is not", null)
+ ])
+ )
.addQuery((c) =>
c.insertInto("content_fts").values({
content_fts: sql.lit("delete"),
@@ -135,7 +142,13 @@ export async function createTriggers(db: Kysely<RawDatabaseSchema>) {
.ifNotExists()
.onTable("notes", "main")
.after()
- .addEvent("update", ["title"])
+ .addEvent("update")
+ .when((eb) =>
+ eb.and([
+ eb("old.deleted", "is not", true),
+ eb("old.title", "is not", null)
+ ])
+ )
.addQuery((c) =>
c.insertInto("notes_fts").values({
notes_fts: sql.lit("delete"),