Skip to content

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

feat: add migration on sync

thecodrrd220d05c1f0a
diff --git a/packages/core/api/migrations.js b/packages/core/api/migrations.js
index 0b0b68f1f..910133f96 100644
--- a/packages/core/api/migrations.js
+++ b/packages/core/api/migrations.js
@@ -23,37 +23,30 @@ class Migrations {
const collections = [
{
- id: "notes",
index: this._db.notes.raw,
dbCollection: this._db.notes,
},
{
- id: "notebooks",
index: this._db.notebooks.raw,
dbCollection: this._db.notebooks,
},
{
- id: "tags",
index: this._db.tags.raw,
dbCollection: this._db.tags,
},
{
- id: "colors",
index: this._db.colors.raw,
dbCollection: this._db.colors,
},
{
- id: "trash",
index: this._db.trash.raw,
dbCollection: this._db.trash,
},
{
- id: "content",
index: content,
dbCollection: this._db.content,
},
{
- id: "settings",
index: [this._db.settings.raw],
dbCollection: this._db.settings,
},
diff --git a/packages/core/api/sync/__tests__/merger.test.js b/packages/core/api/sync/__tests__/merger.test.js
index 81ea584ca..00b48e5b9 100644
--- a/packages/core/api/sync/__tests__/merger.test.js
+++ b/packages/core/api/sync/__tests__/merger.test.js
@@ -9,6 +9,7 @@ import {
import { enableFetchMocks, disableFetchMocks } from "jest-fetch-mock";
import { tagsCollectionParams, mainCollectionParams } from "./utils";
import { login, getEncrypted } from "./utils";
+import { CURRENT_DATABASE_VERSION } from "../../../common";
const emptyServerResponse = {
notes: [],
@@ -38,13 +39,13 @@ test("null server response should return false", async () => {
const tests = [
mainCollectionParams("notes", "note", TEST_NOTE),
mainCollectionParams("notebooks", "notebook", TEST_NOTEBOOK),
- tagsCollectionParams("tags", "someTag"),
- tagsCollectionParams("colors", "red"),
+ tagsCollectionParams("tags", "someTag", "tag"),
+ tagsCollectionParams("colors", "red", "tag"),
];
describe.each(tests)(
"general %s syncing tests",
- (collection, add, edit, get) => {
+ (collection, add, edit, get, itemType) => {
beforeAll(() => {
enableFetchMocks();
});
@@ -61,7 +62,11 @@ describe.each(tests)(
const result = await merger.merge(
{
[collection]: [
- { id: testItem.id, ...(await getEncrypted(testItem)) },
+ {
+ id: testItem.id,
+ v: CURRENT_DATABASE_VERSION,
+ ...(await getEncrypted({ ...testItem, type: itemType })),
+ },
],
synced: false,
},
@@ -125,6 +130,7 @@ test("local delta updated after lastSyncedTimestamp should cause merge conflict"
content: [
{
id: contentId,
+ v: CURRENT_DATABASE_VERSION,
...(await getEncrypted({
id: contentId,
noteId: id,
diff --git a/packages/core/api/sync/__tests__/sync.test.js b/packages/core/api/sync/__tests__/sync.test.js
index 3793895a5..6ad07abdf 100644
--- a/packages/core/api/sync/__tests__/sync.test.js
+++ b/packages/core/api/sync/__tests__/sync.test.js
@@ -1,5 +1,6 @@
//import User from "../../models/user";
import { enableFetchMocks, disableFetchMocks } from "jest-fetch-mock";
+import { CURRENT_DATABASE_VERSION } from "../../../common";
import StorageInterface from "../../../__mocks__/storage.mock";
//import Sync from "../sync";
//import Collector from "../prepare";
@@ -48,6 +49,7 @@ test("sync without merge conflicts, cause merge conflicts, resolve them and then
const contentId = db.notes.note(noteId).data.contentId;
const content = {
id: contentId,
+ v: CURRENT_DATABASE_VERSION,
...(await getEncrypted({
id: contentId,
type: "delta",
diff --git a/packages/core/api/sync/__tests__/utils.js b/packages/core/api/sync/__tests__/utils.js
index 74f9c005d..496a3f8dc 100644
--- a/packages/core/api/sync/__tests__/utils.js
+++ b/packages/core/api/sync/__tests__/utils.js
@@ -32,10 +32,10 @@ function mainCollectionParams(collection, itemKey, item) {
return db[collection][itemKey](item.id).data;
}
- return [collection, addItem, editItem, getItem];
+ return [collection, addItem, editItem, getItem, itemKey];
}
-function tagsCollectionParams(collection, item) {
+function tagsCollectionParams(collection, item, type) {
async function addItem(db) {
const id = await db[collection].add(item, 20);
return db[collection].tag(id);
@@ -49,7 +49,7 @@ function tagsCollectionParams(collection, item) {
return db[collection].tag(item.id);
}
- return [collection, addItem, editItem, getItem];
+ return [collection, addItem, editItem, getItem, type];
}
function getEncrypted(item) {
diff --git a/packages/core/api/sync/merger.js b/packages/core/api/sync/merger.js
index e6defac3e..31d9d257f 100644
--- a/packages/core/api/sync/merger.js
+++ b/packages/core/api/sync/merger.js
@@ -1,3 +1,4 @@
+import { migrations } from "../../migrations";
import { areAllEmpty } from "./utils";
class Merger {
@@ -9,12 +10,19 @@ class Merger {
this._db = db;
}
+ _migrate(item, deserialized) {
+ const version = item.v || 0;
+ const migrate = migrations[version][item.type];
+ if (migrate) return migrate(deserialized);
+ return deserialized;
+ }
+
async _deserialize(item) {
const deserialized = JSON.parse(
await this._db.context.decrypt(this.key, item)
);
deserialized.remote = true;
- return deserialized;
+ return this._migrate(item, deserialized);
}
async _mergeItem(remoteItem, get, add) {
diff --git a/packages/core/database/backup.js b/packages/core/database/backup.js
index c1095561e..f6f41dc1c 100644
--- a/packages/core/database/backup.js
+++ b/packages/core/database/backup.js
@@ -126,42 +126,34 @@ export default class Backup {
const collections = [
{
- id: "notes",
index: data["notes"],
dbCollection: this._db.notes,
},
{
- id: "notebooks",
index: data["notebooks"],
dbCollection: this._db.notebooks,
},
{
- id: "tags",
index: data["tags"],
dbCollection: this._db.tags,
},
{
- id: "colors",
index: data["colors"],
dbCollection: this._db.colors,
},
{
- id: "trash",
index: data["trash"],
dbCollection: this._db.trash,
},
{
- id: "delta",
index: data["delta"],
dbCollection: this._db.content,
},
{
- id: "content",
index: data["content"],
dbCollection: this._db.content,
},
{
- id: "settings",
index: ["settings"],
dbCollection: this._db.settings,
},
diff --git a/packages/core/database/migrator.js b/packages/core/database/migrator.js
index e323d663d..8616af460 100644
--- a/packages/core/database/migrator.js
+++ b/packages/core/database/migrator.js
@@ -14,7 +14,7 @@ class Migrator {
if (item.deleted)
return await collection.dbCollection._collection.addItem(item);
- const migrate = migrations[version][item.type || collection.id];
+ const migrate = migrations[version][item.type || id];
if (migrate) item = migrate(item);
if (!!collection.dbCollection.merge) {
await collection.dbCollection.merge(item);
diff --git a/packages/core/migrations.js b/packages/core/migrations.js
index fa835ddcd..56bc23021 100644
--- a/packages/core/migrations.js
+++ b/packages/core/migrations.js
@@ -1,13 +1,17 @@
export const migrations = {
0: {
note: function (item) {
- const contentId = item.content.delta;
- const notebook = item.notebook;
- delete item.content;
- delete item.notebook;
- item.contentId = contentId;
+ if (item.content) {
+ const contentId = item.content.delta;
+ delete item.content;
+ item.contentId = contentId;
+ }
+ if (item.notebook) {
+ const notebook = item.notebook;
+ delete item.notebook;
+ item.notebooks = [notebook];
+ }
item.remote = true;
- if (notebook) item.notebooks = [notebook];
return item;
},
delta: function (item) {
@@ -24,7 +28,6 @@ export const migrations = {
}
return item;
},
- text: function () {},
},
2: {
note: function (item) {
@@ -38,13 +41,10 @@ export const migrations = {
},
3: {
note: false,
- notebooks: function (item) {
- if (item.favorite !== undefined) delete item.favorite;
- return item;
- },
+ notebooks: false,
tag: false,
trash: false,
- content: false,
+ delta: false,
settings: false,
},
};