Skip to content

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

core: fix database migration

Abdullah Atta3e588e243c89
diff --git a/packages/core/src/api/migrations.ts b/packages/core/src/api/migrations.ts
index af772f4bd..76fed1843 100644
--- a/packages/core/src/api/migrations.ts
+++ b/packages/core/src/api/migrations.ts
@@ -64,7 +64,7 @@ const collections: MigratableCollections = [
},
{
name: "notehistory",
- table: "relations"
+ table: "notehistory"
},
{
name: "sessioncontent",
@@ -84,8 +84,11 @@ class Migrations {
async init() {
this.version =
- (await this.db.storage().read("v")) || CURRENT_DATABASE_VERSION;
- this.db.kv().write("v", this.version);
+ (await this.db.kv().read("v")) ||
+ (await this.db.storage().read("v")) ||
+ CURRENT_DATABASE_VERSION;
+
+ await this.db.kv().write("v", this.version);
}
required() {
diff --git a/packages/core/src/database/migrator.ts b/packages/core/src/database/migrator.ts
index 707ff64f5..63b8d3abb 100644
--- a/packages/core/src/database/migrator.ts
+++ b/packages/core/src/database/migrator.ts
@@ -104,10 +104,12 @@ class Migrator {
const toAdd = [];
for (let i = 0; i < items.length; ++i) {
const item = items[i];
+ // can be true due to corrupted data.
+ if (Array.isArray(item)) continue;
if (!item) continue;
// check if item is permanently deleted or just a soft delete
- if (isDeleted(item) && !isTrashItem(item)) {
+ if (isDeleted(item)) {
toAdd.push(item);
continue;
}
@@ -134,8 +136,8 @@ class Migrator {
);
}
- if (migrated) {
- if (item.type !== "settings") {
+ if (migrated === true) {
+ if (item.type === "settings") {
// we are removing the old settings.
await db.storage().remove("settings");
} else toAdd.push(item);
diff --git a/packages/core/src/migrations.ts b/packages/core/src/migrations.ts
index 407698948..53a5b65be 100644
--- a/packages/core/src/migrations.ts
+++ b/packages/core/src/migrations.ts
@@ -287,8 +287,8 @@ const migrations: Migration[] = [
attachment: async (item, db) => {
for (const noteId of item.noteIds || []) {
await db.relations.add(
- { type: "attachment", id: item.id },
- { type: "note", id: noteId }
+ { type: "note", id: noteId },
+ { type: "attachment", id: item.id }
);
}
@@ -399,7 +399,7 @@ const migrations: Migration[] = [
const value = await db.storage().read(key);
if (value === undefined || value === null) continue;
await db.kv().write(key, value as any);
- await db.storage().remove(key);
+ // await db.storage().remove(key);
}
}
},
diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts
index a667e22b1..4e75dc0eb 100644
--- a/packages/core/src/types.ts
+++ b/packages/core/src/types.ts
@@ -474,8 +474,8 @@ export type BaseTrashItem<TItem extends BaseItem<"note" | "notebook">> =
export type TrashItem = BaseTrashItem<Note> | BaseTrashItem<Notebook>;
-export function isDeleted(item: object): item is DeletedItem {
- return "deleted" in item && !!item.deleted;
+export function isDeleted(item: any): item is DeletedItem {
+ return !!item.deleted && !item.type;
}
export function isTrashItem(item: MaybeDeletedItem<Item>): item is TrashItem {