Skip to content

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

core: fix duplicate tags created during migration

Abdullah Atta9ad9c0c633db
diff --git a/packages/core/__tests__/migrations.test.ts b/packages/core/__tests__/migrations.test.ts
index 6e3d09b45..7613cb692 100644
--- a/packages/core/__tests__/migrations.test.ts
+++ b/packages/core/__tests__/migrations.test.ts
@@ -326,7 +326,8 @@ describe.concurrent("[5.9] make tags syncable", () => {
title: "oldone"
};
expect(await migrateItem(tag, 5.9, 6.0, "tag", db, "backup")).toBe(true);
- expect(tag.id).not.toBe(makeId("oldone"));
+ expect(tag.id).toBe(makeId("oldone"));
+ expect(tag.dateCreated).toBeGreaterThan(0);
expect(tag.noteIds).toBeUndefined();
expect(tag.alias).toBeUndefined();
expect(tag.title).toBe("oldone");
diff --git a/packages/core/src/collections/tags.ts b/packages/core/src/collections/tags.ts
index 45dad046c..8dbcec394 100644
--- a/packages/core/src/collections/tags.ts
+++ b/packages/core/src/collections/tags.ts
@@ -52,7 +52,7 @@ export class Tags implements ICollection {
async add(item: Partial<Tag>) {
item.title = item.title ? Tags.sanitize(item.title) : item.title;
- const id = item.id || getId(item.dateCreated);
+ const id = item.id || getId();
const oldTag = item.id
? await this.tag(item.id)
: item.title
diff --git a/packages/core/src/migrations.ts b/packages/core/src/migrations.ts
index 55c99d141..7b9c89840 100644
--- a/packages/core/src/migrations.ts
+++ b/packages/core/src/migrations.ts
@@ -238,7 +238,7 @@ const migrations: Migration[] = [
// there's a case where dateCreated is null in tags
item.dateCreated = item.dateCreated || Date.now();
item.title = alias || item.title;
- item.id = getId(item.dateCreated);
+ item.id = makeId(item.title);
delete item.localOnly;
delete item.noteIds;
@@ -257,6 +257,9 @@ const migrations: Migration[] = [
const newTagId =
newTag?.id ||
(await db.tags.add({
+ // IMPORTANT: the id must be deterministic to avoid creating
+ // duplicate colors when migrating on different devices
+ id: makeId(alias || tag),
dateCreated: oldTag?.dateCreated,
dateModified: oldTag?.dateModified,
title: alias || tag,
@@ -278,6 +281,9 @@ const migrations: Migration[] = [
const newColorId =
newColor?.id ||
(await db.colors.add({
+ // IMPORTANT: the id must be deterministic to avoid creating
+ // duplicate colors when migrating on different devices
+ id: makeId(alias || item.color),
dateCreated: oldColor?.dateCreated,
dateModified: oldColor?.dateModified,
title: alias || item.color,