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
feat: use single version for db & backup migration
thecodrr0a192c243897
diff --git a/packages/core/api/migrations.js b/packages/core/api/migrations.jsindex 7a295a9f5..5d12dad57 100644--- a/packages/core/api/migrations.js+++ b/packages/core/api/migrations.js@@ -1,4 +1,5 @@-const CURRENT_VERSION = 3;+import { CURRENT_DATABASE_VERSION } from "../common";+class Migrations {/***@@ -6,7 +7,7 @@ class Migrations {*/constructor(db) {this._db = db;- this.dbVersion = CURRENT_VERSION;+ this.dbVersion = CURRENT_DATABASE_VERSION;}async init() {@@ -14,13 +15,13 @@ class Migrations {}get _shouldMigrate() {- return this.dbVersion < CURRENT_VERSION;+ return this.dbVersion < CURRENT_DATABASE_VERSION;}_migrationFunction(collectionId) {let migrationFunction = migrations[version][collectionId];if (!migrationFunction)- migrationFunction = migrations[CURRENT_VERSION][collectionId];+ migrationFunction = migrations[CURRENT_DATABASE_VERSION][collectionId];return migrationFunction;}diff --git a/packages/core/common.js b/packages/core/common.jsindex 888171834..9a585fe7a 100644--- a/packages/core/common.js+++ b/packages/core/common.js@@ -16,3 +16,5 @@ export const CHECK_IDS = {notebookAdd: "notebook:add",backupEncrypt: "backup:encrypt",};++export const CURRENT_DATABASE_VERSION = 3;diff --git a/packages/core/database/backup.js b/packages/core/database/backup.jsindex 0c45478eb..a9407f854 100644--- a/packages/core/database/backup.js+++ b/packages/core/database/backup.js@@ -1,11 +1,14 @@import Hashes from "jshashes";-import { CHECK_IDS, sendCheckUserStatusEvent } from "../common.js";+import {+ CHECK_IDS,+ sendCheckUserStatusEvent,+ CURRENT_DATABASE_VERSION,+} from "../common.js";import { migrations } from "../migrations.js";const md5 = new Hashes.MD5();const invalidKeys = ["user", "t", "lastBackupTime"];const validTypes = ["mobile", "web", "node"];-const CURRENT_BACKUP_VERSION = 3;export default class Backup {/***@@ -46,7 +49,7 @@ export default class Backup {await this._db.context.write("lastBackupTime", Date.now());return JSON.stringify({- version: CURRENT_BACKUP_VERSION,+ version: CURRENT_DATABASE_VERSION,type,date: Date.now(),data,@@ -83,13 +86,13 @@ export default class Backup {_migrateBackup(backup) {const { version = 0 } = backup;- if (version > CURRENT_BACKUP_VERSION)+ if (version > CURRENT_DATABASE_VERSION)throw new Error("This backup was made from a newer version of Notesnook. Cannot migrate.");switch (version) {- case CURRENT_BACKUP_VERSION:+ case CURRENT_DATABASE_VERSION:case 2: {return backup;}@@ -114,7 +117,7 @@ export default class Backup {async _migrateData(backup) {const { data, version = 0 } = backup;- if (version > CURRENT_BACKUP_VERSION)+ if (version > CURRENT_DATABASE_VERSION)throw new Error("This backup was made from a newer version of Notesnook. Cannot migrate.");@@ -147,7 +150,7 @@ export default class Backup {let migrationFunction = migrations[version][collectionId];if (!migrationFunction)migrationFunction =- migrations[CURRENT_BACKUP_VERSION][collectionId];+ migrations[CURRENT_DATABASE_VERSION][collectionId];await migrationFunction(this._db, item);}));