Skip to content

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

core: make item migration fall through all db versions

Abdullah Attac09715d05395
diff --git a/packages/core/api/sync/merger.js b/packages/core/api/sync/merger.js
index ae5a350f0..ee912f65a 100644
--- a/packages/core/api/sync/merger.js
+++ b/packages/core/api/sync/merger.js
@@ -17,10 +17,9 @@ 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 { migrations } from "../../migrations";
+import { migrateItem } from "../../migrations";
import SparkMD5 from "spark-md5";
import setManipulator from "../../utils/set";
-import { CURRENT_DATABASE_VERSION } from "../../common";
import { logger } from "../../logger";
class Merger {
@@ -136,19 +135,7 @@ class Merger {
deserialized.type = type;
}
- if (!migrations[version]) {
- throw new Error(
- version > CURRENT_DATABASE_VERSION
- ? `Cannot migrate item to v${version}. Please update your client to the latest version.`
- : `Could not migrate item to v${version}. Please report this bug to the developers.`
- );
- }
-
- const migrate = migrations[version][type];
- if (migrate) {
- return await migrate(deserialized, this._db);
- }
- return deserialized;
+ return migrateItem(deserialized, version, type, this._db);
}
async _deserialize(item, migrate = true) {
diff --git a/packages/core/database/migrator.js b/packages/core/database/migrator.js
index ebdcf401c..f55157cae 100644
--- a/packages/core/database/migrator.js
+++ b/packages/core/database/migrator.js
@@ -17,7 +17,7 @@ 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 { migrations } from "../migrations";
+import { migrateItem } from "../migrations";
class Migrator {
async migrate(db, collections, get, version) {
@@ -40,8 +40,12 @@ class Migrator {
item.type = "tiptap";
}
- const migrate = migrations[version][item.type || collection.type];
- if (migrate) item = await migrate(item, db);
+ item = await migrateItem(
+ item,
+ version,
+ item.type || collection.type,
+ db
+ );
if (collection.dbCollection.merge) {
await collection.dbCollection.merge(item);
diff --git a/packages/core/migrations.js b/packages/core/migrations.js
index 58957614d..4c5e8b9c2 100644
--- a/packages/core/migrations.js
+++ b/packages/core/migrations.js
@@ -19,88 +19,108 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { parseHTML } from "./utils/html-parser";
import { decodeHTML5 } from "entities";
-
-export const migrations = {
- 5.0: {},
- 5.1: {},
- 5.2: {
- note: replaceDateEditedWithDateModified(),
- notebook: (item) => {
- item = replaceDateEditedWithDateModified()(item);
- return migrations["5.6"].notebook(item);
- },
- tag: replaceDateEditedWithDateModified(true),
- attachment: replaceDateEditedWithDateModified(true),
- trash: replaceDateEditedWithDateModified(),
- tiny: (item) => {
- item = replaceDateEditedWithDateModified()(item);
-
- if (!item.data || item.data.iv) return migrations["5.3"].tiny(item);
-
- item.data = removeToxClassFromChecklist(wrapTablesWithDiv(item.data));
- return migrations["5.3"].tiny(item);
- },
- settings: (item) => {
- item = replaceDateEditedWithDateModified(true)(item);
- return migrations["5.6"].settings(item);
+import { CURRENT_DATABASE_VERSION } from "./common";
+
+const migrations = [
+ { version: 5.0, types: {} },
+ { version: 5.1, types: {} },
+ {
+ version: 5.2,
+ types: {
+ note: replaceDateEditedWithDateModified(false),
+ notebook: replaceDateEditedWithDateModified(false),
+ tag: replaceDateEditedWithDateModified(true),
+ attachment: replaceDateEditedWithDateModified(true),
+ trash: replaceDateEditedWithDateModified(),
+ tiny: (item) => {
+ item = replaceDateEditedWithDateModified(false)(item);
+
+ if (!item.data || item.data.iv) return item;
+
+ item.data = removeToxClassFromChecklist(wrapTablesWithDiv(item.data));
+ return item;
+ },
+ settings: replaceDateEditedWithDateModified(true)
}
},
- 5.3: {
- tiny: (item) => {
- if (!item.data || item.data.iv) return migrations["5.4"].tiny(item);
- item.data = decodeWrappedTableHtml(item.data);
- return migrations["5.4"].tiny(item);
+ {
+ version: 5.3,
+ types: {
+ tiny: (item) => {
+ if (!item.data || item.data.iv) return item;
+ item.data = decodeWrappedTableHtml(item.data);
+ return item;
+ }
}
},
- 5.4: {
- tiny: (item) => {
- if (!item.data || item.data.iv) return item;
- item.type = "tiptap";
- item.data = tinyToTiptap(item.data);
- return item;
+ {
+ version: 5.4,
+ types: {
+ tiny: (item) => {
+ if (!item.data || item.data.iv) return item;
+ item.type = "tiptap";
+ item.data = tinyToTiptap(item.data);
+ return item;
+ }
}
},
- 5.5: {},
- 5.6: {
- notebook: (item) => {
- if (!item.topics) return item;
-
- item.topics = item.topics.map((topic) => {
- delete topic.notes;
- return topic;
- });
- return item;
- },
- settings: async (item, db) => {
- if (!item.pins) return item;
-
- for (const pin of item.pins) {
- if (!pin.data) continue;
-
- await db.shortcuts.add({
- item: {
- type: pin.type,
- id: pin.data.id,
- notebookId: pin.data.notebookId
- }
+ {
+ version: 5.5,
+ types: {}
+ },
+ {
+ version: 5.6,
+ types: {
+ notebook: (item) => {
+ if (!item.topics) return item;
+
+ item.topics = item.topics.map((topic) => {
+ delete topic.notes;
+ return topic;
});
+ return item;
+ },
+ settings: async (item, db) => {
+ if (!item.pins) return item;
+
+ for (const pin of item.pins) {
+ if (!pin.data) continue;
+ await db.shortcuts.add({
+ item: {
+ type: pin.type,
+ id: pin.data.id,
+ notebookId: pin.data.notebookId
+ }
+ });
+ }
+ delete item.pins;
+ return item;
}
- delete item.pins;
- return item;
}
},
- 5.7: {
- note: false,
- shortcut: false,
- notebook: false,
- tag: false,
- attachment: false,
- trash: false,
- tiny: false,
- tiptap: false,
- settings: false
+ { version: 5.7, types: {} }
+];
+
+export async function migrateItem(item, version, type, database) {
+ let migrationStartIndex = migrations.findIndex((m) => m.version === version);
+ if (migrationStartIndex <= -1) {
+ throw new Error(
+ version > CURRENT_DATABASE_VERSION
+ ? `Please update the app to the latest version.`
+ : `You seem to be on a very outdated version. Please update the app to the latest version.`
+ );
}
-};
+
+ for (; migrationStartIndex < migrations.length; ++migrationStartIndex) {
+ const migration = migrations[migrationStartIndex];
+ if (migration.version === CURRENT_DATABASE_VERSION) break;
+
+ const itemMigrator = migration.types[type];
+ if (!itemMigrator) continue;
+ item = await itemMigrator(item, database);
+ }
+ return item;
+}
function replaceDateEditedWithDateModified(removeDateEditedProperty = false) {
return function (item) {