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: migrate db in a streaming way
Abdullah Attaac9dca809839
diff --git a/packages/core/src/api/migrations.js b/packages/core/src/api/migrations.jsindex 7abc3276d..f8e274894 100644--- a/packages/core/src/api/migrations.js+++ b/packages/core/src/api/migrations.js@@ -66,11 +66,7 @@ class Migrations {dbCollection: this._db.colors},{- index: () => this._db.trash.raw,- dbCollection: this._db.trash- },- {- index: () => this._db.content.all(),+ iterate: true,dbCollection: this._db.content},{@@ -91,11 +87,11 @@ class Migrations {dbCollection: this._db.relations},{- index: () => this._db.noteHistory.sessionContent.all(),+ iterate: true,dbCollection: this._db.noteHistory},{- index: () => this._db.noteHistory.sessionContent.all(),+ iterate: true,dbCollection: this._db.noteHistory.sessionContent},{diff --git a/packages/core/src/database/migrator.js b/packages/core/src/database/migrator.jsindex 2f04ad3dc..d514d0c1f 100644--- a/packages/core/src/database/migrator.js+++ b/packages/core/src/database/migrator.js@@ -23,7 +23,11 @@ import { migrateCollection, migrateItem } from "../migrations";class Migrator {async migrate(db, collections, get, version, restore = false) {for (let collection of collections) {- if (!collection.index || !collection.dbCollection) continue;+ if (+ (!collection.iterate && !collection.index) ||+ !collection.dbCollection+ )+ continue;if (collection.dbCollection.collectionName)sendMigrationProgressEvent(@@ -35,61 +39,79 @@ class Migrator {await migrateCollection(collection.dbCollection, version);- const index = (await collection.index()) || [];- const toAdd = [];- for (var i = 0; i < index.length; ++i) {- let id = index[i];- let item = get(id, collection.dbCollection.collectionName);- if (!item) {- continue;- }-- // check if item is permanently deleted or just a soft delete- if (item.deleted && !item.type) {- await collection.dbCollection?._collection?.addItem(item);- continue;- }-- const itemId = item.id;- const migrated = await migrateItem(- item,+ if (collection.index) {+ await this.migrateItems(+ db,+ collection,+ collection.index(),+ get,version,- item.type || collection.type || collection.dbCollection.type,- db+ restore);+ } else if (collection.iterate) {+ for await (const index of collection.dbCollection._collection.iterate(+ 100+ )) {+ await this.migrateItems(db, collection, index, get, version, restore);+ }+ }+ }+ return true;+ }- if (migrated || restore) {- if (collection.type === "settings") {- await collection.dbCollection.merge(item);- } else if (item.type === "note") {- toAdd.push(await db.notes.merge(null, item));- } else if (collection.dbCollection._collection) {- toAdd.push(item);- } else {- throw new Error(- `No idea how to handle this kind of item: ${item.type}.`- );- }+ async migrateItems(db, collection, index, get, version, restore) {+ const toAdd = [];+ for (var i = 0; i < index.length; ++i) {+ let id = index[i];+ let item = get(id, collection.dbCollection.collectionName);+ if (!item) {+ continue;+ }- // if id changed after migration, we need to delete the old one.- if (item.id !== itemId) {- await collection.dbCollection._collection.deleteItem(itemId);- }- }+ // check if item is permanently deleted or just a soft delete+ if (item.deleted && !item.type) {+ await collection.dbCollection?._collection?.addItem(item);+ continue;}- if (toAdd.length > 0) {- await collection.dbCollection._collection.setItems(toAdd);- if (collection.dbCollection.collectionName)- sendMigrationProgressEvent(- db.eventManager,- collection.dbCollection.collectionName,- toAdd.length,- toAdd.length+ const itemId = item.id;+ const migrated = await migrateItem(+ item,+ version,+ item.type || collection.type || collection.dbCollection.type,+ db+ );++ if (migrated || restore) {+ if (collection.type === "settings") {+ await collection.dbCollection.merge(item);+ } else if (item.type === "note") {+ toAdd.push(await db.notes.merge(null, item));+ } else if (collection.dbCollection._collection) {+ toAdd.push(item);+ } else {+ throw new Error(+ `No idea how to handle this kind of item: ${item.type}.`);+ }++ // if id changed after migration, we need to delete the old one.+ if (item.id !== itemId) {+ await collection.dbCollection._collection.deleteItem(itemId);+ }}}- return true;++ if (toAdd.length > 0) {+ await collection.dbCollection._collection.setItems(toAdd);+ if (collection.dbCollection.collectionName)+ sendMigrationProgressEvent(+ db.eventManager,+ collection.dbCollection.collectionName,+ toAdd.length,+ toAdd.length+ );+ }}}export default Migrator;