core: fix duplicate tags created during migration

This commit is contained in:
Abdullah Atta
2024-03-22 10:15:53 +05:00
parent ec2a6f26e6
commit 9ad9c0c633
3 changed files with 10 additions and 3 deletions

View File

@@ -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");

View File

@@ -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

View File

@@ -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,