A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
core: fix migration for db version > 6
Abdullah Atta3795c69a7b0a
diff --git a/packages/core/src/api/migrations.ts b/packages/core/src/api/migrations.tsindex 76fed1843..779d8235b 100644--- a/packages/core/src/api/migrations.ts+++ b/packages/core/src/api/migrations.ts@@ -73,6 +73,10 @@ const collections: MigratableCollections = [{name: "notes",table: "notes"+ },+ {+ name: "vaults",+ table: "vaults"}];diff --git a/packages/core/src/api/sync/index.ts b/packages/core/src/api/sync/index.tsindex 956638a64..cf523a4e3 100644--- a/packages/core/src/api/sync/index.ts+++ b/packages/core/src/api/sync/index.ts@@ -261,7 +261,7 @@ class Sync {);}}- await this.connection?.send("PushCompleted");+ if (done > 0) await this.connection?.send("PushCompleted");return true;}@@ -299,6 +299,7 @@ class Sync {* @private*/async onPushCompleted() {+ console.log("PUSH COMPLETED!");this.db.eventManager.publish(EVENTS.databaseSyncRequested, true, false);}diff --git a/packages/core/src/collections/notebooks.ts b/packages/core/src/collections/notebooks.tsindex e538532a8..4ca7df77e 100644--- a/packages/core/src/collections/notebooks.ts+++ b/packages/core/src/collections/notebooks.ts@@ -52,7 +52,6 @@ export class Notebooks implements ICollection {"Please use db.notebooks.merge to merge remote notebooks");- //TODO reliably and efficiently check for duplicates.const id = notebookArg.id || getId();const oldNotebook = await this.notebook(id);diff --git a/packages/core/src/database/indexer.ts b/packages/core/src/database/indexer.tsindex 8d8813ad5..b2a5486bc 100644--- a/packages/core/src/database/indexer.ts+++ b/packages/core/src/database/indexer.ts@@ -52,8 +52,11 @@ export default class Indexer<T> {}async clear() {+ await this.storage().removeMulti(+ this._indices.map((key) => this.makeId(key))+ );this._indices = [];- await this.storage().clear();+ await this.storage().write(this.type, this._indices);}async read(diff --git a/packages/core/src/database/migrator.ts b/packages/core/src/database/migrator.tsindex b55d7cf42..9a5cb1947 100644--- a/packages/core/src/database/migrator.ts+++ b/packages/core/src/database/migrator.ts@@ -56,37 +56,45 @@ class Migrator {collections: MigratableCollections,version: number) {- const vaultKey = await db.storage().read<Cipher<"base64">>("vaultKey");- if (vaultKey)- await migrateVaultKey(db, vaultKey, version, CURRENT_DATABASE_VERSION);- await migrateKV(db, version, CURRENT_DATABASE_VERSION);+ if (version <= 5.9) {+ const vaultKey = await db.storage().read<Cipher<"base64">>("vaultKey");+ if (vaultKey)+ await migrateVaultKey(db, vaultKey, version, CURRENT_DATABASE_VERSION);+ await migrateKV(db, version, CURRENT_DATABASE_VERSION);+ }for (const collection of collections) {sendMigrationProgressEvent(db.eventManager, collection.name, 0, 0);- const indexedCollection = new IndexedCollection(- db.storage,- collection.name- );const table = new SQLCollection(db.sql,db.transaction,collection.table,db.eventManager);-- await migrateCollection(indexedCollection, version);-- await indexedCollection.init();- await table.init();- for await (const entries of indexedCollection.iterate(100)) {- await this.migrateItems(- db,- table,- collection.name,- entries.map((i) => i[1]),- version+ if (version <= 5.9) {+ const indexedCollection = new IndexedCollection(+ db.storage,+ collection.name);++ await migrateCollection(indexedCollection, version);++ await indexedCollection.init();+ await table.init();+ for await (const entries of indexedCollection.iterate(100)) {+ await this.migrateToSQLite(+ db,+ table,+ collection.name,+ entries.map((i) => i[1]),+ version+ );+ }+ await indexedCollection.clear();+ } else {+ await table.init();+ await this.migrateItems(db, table, collection.name, version);}}@@ -94,7 +102,7 @@ class Migrator {return true;}- async migrateItems(+ private async migrateToSQLite(db: Database,table: SQLCollection<keyof DatabaseSchema>,type: keyof Collections,@@ -133,8 +141,7 @@ class Migrator {if (migrated === true) {if (!isDeleted(item) && item.type === "settings") {// we are removing the old settings.- if (process.env.NODE_ENV === "test")- await db.storage().remove("settings");+ await db.storage().remove("settings");} else toAdd.push(item);// if id changed after migration, we need to delete the old one.@@ -154,5 +161,64 @@ class Migrator {);}}++ private async migrateItems(+ db: Database,+ table: SQLCollection<keyof DatabaseSchema>,+ type: keyof Collections,+ version: number+ ) {+ let progress = 0;+ let toAdd = [];+ let toDelete = [];+ for await (const item of table.stream(100)) {+ if (toAdd.length >= 500) {+ await table.put(toAdd as any);+ await table.delete(toDelete);+ progress += toAdd.length;+ sendMigrationProgressEvent(db.eventManager, type, progress, progress);+ toAdd = [];+ toDelete = [];+ }++ const itemId = item.id;+ let migrated = await migrateItem(+ item as any,+ version,+ CURRENT_DATABASE_VERSION,+ isDeleted(item) ? "never" : item.type || "never",+ db,+ "local"+ );++ // trash item is also a notebook or a note so we have to migrate it separately.+ if (isTrashItem(item)) {+ migrated = await migrateItem(+ item as any,+ version,+ CURRENT_DATABASE_VERSION,+ item.itemType,+ db,+ "local"+ );+ }++ if (!migrated) continue;++ toAdd.push(item);++ // if id changed after migration, we need to delete the old one.+ if (item.id !== itemId) {+ toDelete.push(itemId);+ }+ }++ await table.put(toAdd as any);+ await table.delete(toDelete);+ progress += toAdd.length;+ sendMigrationProgressEvent(db.eventManager, type, progress, progress);+ toAdd = [];+ toDelete = [];+ }}export default Migrator;diff --git a/packages/core/src/database/sql-collection.ts b/packages/core/src/database/sql-collection.tsindex 39de68a0a..207a8778d 100644--- a/packages/core/src/database/sql-collection.ts+++ b/packages/core/src/database/sql-collection.ts@@ -111,6 +111,7 @@ export class SQLCollection<}async delete(ids: string[]) {+ if (ids.length <= 0) return;await this.db().deleteFrom<keyof DatabaseSchema>(this.type).where("id", "in", ids)@@ -152,6 +153,7 @@ export class SQLCollection<}async put(items: (SQLiteItem<T> | undefined)[]) {+ if (items.length <= 0) return;const entries = items.reduce((array, item) => {if (!item) return array;if (!item.remote) {diff --git a/packages/core/src/migrations.ts b/packages/core/src/migrations.tsindex b29628a9f..cc49b331d 100644--- a/packages/core/src/migrations.ts+++ b/packages/core/src/migrations.ts@@ -62,8 +62,17 @@ type Migration = {migrationType: MigrationType,itemType: MigrationItemType) => "skip" | boolean | Promise<boolean | "skip"> | void;+ /**+ * @deprecated+ */collection?: (collection: IndexedCollection) => Promise<void> | void;+ /**+ * @deprecated+ */vaultKey?: (db: Database, key: Cipher<"base64">) => Promise<void> | void;+ /**+ * @deprecated+ */kv?: (db: Database) => Promise<void> | void;};@@ -420,17 +429,14 @@ const migrations: Migration[] = [},async vaultKey(db, key) {await db.vaults.add({ title: "Default", key });- // TODO: remove this- if (process.env.NODE_ENV === "test")- await db.storage().remove("vaultKey");+ await db.storage().remove("vaultKey");},async kv(db) {for (const key of KEYS) {const value = await db.storage().read(key);if (value === undefined || value === null) continue;await db.kv().write(key, value as any);- // TODO: remove this- if (process.env.NODE_ENV === "test") await db.storage().remove(key);+ await db.storage().remove(key);}}},@@ -516,6 +522,9 @@ export async function migrateItem<TItemType extends MigrationItemType>(return count > 0;}+/**+ * @deprecated+ */export async function migrateCollection(collection: IndexedCollection,version: number@@ -538,6 +547,9 @@ export async function migrateCollection(}}+/**+ * @deprecated+ */export async function migrateVaultKey(db: Database,vaultKey: Cipher<"base64">,@@ -562,6 +574,9 @@ export async function migrateVaultKey(}}+/**+ * @deprecated+ */export async function migrateKV(db: Database,version: number,