Skip to content

Commit

Permalink
Merge custom data
Browse files Browse the repository at this point in the history
  • Loading branch information
varjolintu committed Apr 13, 2019
1 parent c3ae446 commit 542300a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/core/Merger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,6 @@ Merger::ChangeList Merger::mergeMetadata(const MergeContext& context)
// TODO HNH: missing handling of recycle bin, names, templates for groups and entries,
// public data (entries of newer dict override keys of older dict - ignoring
// their own age - it is enough if one entry of the whole dict is newer) => possible lost update
// TODO HNH: CustomData is merged with entries of the new customData overwrite entries
// of the older CustomData - the dict with the newest entry is considered
// newer regardless of the age of the other entries => possible lost update
ChangeList changes;
auto* sourceMetadata = context.m_sourceDb->metadata();
auto* targetMetadata = context.m_targetDb->metadata();
Expand All @@ -624,5 +621,15 @@ Merger::ChangeList Merger::mergeMetadata(const MergeContext& context)
changes << tr("Adding missing icon %1").arg(QString::fromLatin1(customIconId.toRfc4122().toHex()));
}
}

const auto customDataKeys = sourceMetadata->customData()->keys();
for (const auto& key : customDataKeys) {
if (!targetMetadata->customData()->contains(key)) {
auto value = sourceMetadata->customData()->value(key);
targetMetadata->customData()->set(key, value);
changes << tr("Adding custom data %1 [%2]").arg(key, value);
}
}

return changes;
}
30 changes: 30 additions & 0 deletions tests/TestMerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,36 @@ void TestMerge::testMetadata()
// will be used - exception is the target has no recycle bin activated
}

void TestMerge::testCustomdata()
{
QScopedPointer<Database> dbDestination(new Database());
QScopedPointer<Database> dbSource(createTestDatabase());

m_clock->advanceSecond(1);

dbSource->metadata()->customData()->set("key1", "value1");
dbSource->metadata()->customData()->set("key2", "value2");
dbSource->metadata()->customData()->set("key3", "newValue");
dbSource->metadata()->customData()->set("Browser", "n'8=3W@L^6d->d.]St_>]");
dbDestination->metadata()->customData()->set("key3", "oldValue");
// Sanity check.
QVERIFY(!dbSource->metadata()->customData()->isEmpty());

m_clock->advanceSecond(1);

Merger merger(dbSource.data(), dbDestination.data());
merger.merge();

QVERIFY(!dbDestination->metadata()->customData()->isEmpty());
QVERIFY(dbDestination->metadata()->customData()->contains("key1"));
QVERIFY(dbDestination->metadata()->customData()->contains("key2"));
QVERIFY(dbDestination->metadata()->customData()->contains("Browser"));
QCOMPARE(dbDestination->metadata()->customData()->value("key1"), "value1");
QCOMPARE(dbDestination->metadata()->customData()->value("key2"), "value2");
QCOMPARE(dbDestination->metadata()->customData()->value("Browser"), "n'8=3W@L^6d->d.]St_>]");
QCOMPARE(dbDestination->metadata()->customData()->value("key3"), "oldValue"); // Old value should not be overwritten
}

void TestMerge::testDeletedEntry()
{
QScopedPointer<Database> dbDestination(createTestDatabase());
Expand Down
1 change: 1 addition & 0 deletions tests/TestMerge.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private slots:
void testMergeCustomIcons();
void testMergeDuplicateCustomIcons();
void testMetadata();
void testCustomdata();
void testDeletedEntry();
void testDeletedGroup();
void testDeletedRevertedEntry();
Expand Down

0 comments on commit 542300a

Please sign in to comment.