core: fix database migration

This commit is contained in:
Abdullah Atta
2024-02-07 13:37:19 +05:00
parent dd7765df97
commit 446005b8e2
4 changed files with 16 additions and 11 deletions

View File

@@ -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() {

View File

@@ -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);

View File

@@ -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);
}
}
},

View File

@@ -473,8 +473,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 {