Skip to content

Commit

Permalink
Merge branch 'jb51873' into 'master'
Browse files Browse the repository at this point in the history
[qtcontacts-sqlite] Delete metadata for deleted collections. Contributes to JB#51873

See merge request mer-core/qtcontacts-sqlite!54
  • Loading branch information
chriadam committed Nov 24, 2020
2 parents 38553af + 22fb8ec commit 141af12
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/engine/contactwriter.cpp
Expand Up @@ -720,6 +720,16 @@ QContactManager::Error ContactWriter::save(

QContactManager::Error ContactWriter::removeCollection(const QContactCollectionId &collectionId, bool onlyIfFlagged)
{
const QString removeCollectionMetadataStatement(QStringLiteral(
" DELETE FROM CollectionsMetadata WHERE collectionId = :collectionId %1"
).arg(onlyIfFlagged ? QStringLiteral("AND collectionId IN (SELECT collectionId FROM Collections WHERE changeFlags >= 4)") : QString())); // ChangeFlags::IsDeleted
ContactsDatabase::Query removeMetadata(m_database.prepare(removeCollectionMetadataStatement));
removeMetadata.bindValue(QStringLiteral(":collectionId"), ContactCollectionId::databaseId(collectionId));
if (!ContactsDatabase::execute(removeMetadata)) {
removeMetadata.reportError("Failed to remove collection");
return QContactManager::UnspecifiedError;
}

const QString removeCollectionStatement(QStringLiteral(
" DELETE FROM Collections WHERE collectionId = :collectionId %1"
).arg(onlyIfFlagged ? QStringLiteral("AND changeFlags >= 4") : QString())); // ChangeFlags::IsDeleted
Expand All @@ -729,6 +739,7 @@ QContactManager::Error ContactWriter::removeCollection(const QContactCollectionI
remove.reportError("Failed to remove collection");
return QContactManager::UnspecifiedError;
}

return QContactManager::NoError;
}

Expand Down
46 changes: 46 additions & 0 deletions tests/auto/synctransactions/tst_synctransactions.cpp
Expand Up @@ -398,6 +398,7 @@ void tst_synctransactions::singleCollection_multipleCycles()
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_APPLICATIONNAME, "tst_synctransactions");
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_ACCOUNTID, 5);
remoteAddressbook.setExtendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_REMOTEPATH, "/addressbooks/test");
remoteAddressbook.setExtendedMetaData(QStringLiteral("SyncToken"), "synctoken-one");

QContact syncAlice;
QContactName an;
Expand Down Expand Up @@ -487,6 +488,27 @@ void tst_synctransactions::singleCollection_multipleCycles()

// now perform a second sync cycle.
// first, retrieve local changes we need to push to remote server.
QList<QContactCollection> addedCollections;
QList<QContactCollection> modifiedCollections;
QList<QContactCollection> deletedCollections;
QList<QContactCollection> unmodifiedCollections;
QVERIFY(cme->fetchCollectionChanges(
5,
"tst_synctransactions",
&addedCollections,
&modifiedCollections,
&deletedCollections,
&unmodifiedCollections,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedCollections.count(), 0);
QCOMPARE(modifiedCollections.count(), 0);
QCOMPARE(deletedCollections.count(), 0);
QCOMPARE(unmodifiedCollections.count(), 1);
QCOMPARE(unmodifiedCollections.first().extendedMetaData().value(
QStringLiteral("SyncToken")).toString(),
QStringLiteral("synctoken-one"));

QList<QContact> addedContacts;
QList<QContact> modifiedContacts;
QList<QContact> deletedContacts;
Expand Down Expand Up @@ -540,6 +562,9 @@ void tst_synctransactions::singleCollection_multipleCycles()
cf.setFlag(QContactStatusFlags::IsDeleted, true);
syncCharlie.saveDetail(&cf, QContact::IgnoreAccessConstraints);

// specify an updated ctag for the addressbook.
remoteAddressbook.setExtendedMetaData(QStringLiteral("SyncToken"), "synctoken-two");

// write the remote changes to the local database.
additions.clear();
modifications.clear();
Expand Down Expand Up @@ -568,6 +593,27 @@ void tst_synctransactions::singleCollection_multipleCycles()
// now perform another sync cycle.
// there should be no local changes reported since the last clearChangeFlags()
// (in this case, since the last storeChanges() call).
addedCollections.clear();
modifiedCollections.clear();
deletedCollections.clear();
unmodifiedCollections.clear();
QVERIFY(cme->fetchCollectionChanges(
5,
"tst_synctransactions",
&addedCollections,
&modifiedCollections,
&deletedCollections,
&unmodifiedCollections,
&err));
QCOMPARE(err, QContactManager::NoError);
QCOMPARE(addedCollections.count(), 0);
QCOMPARE(modifiedCollections.count(), 0);
QCOMPARE(deletedCollections.count(), 0);
QCOMPARE(unmodifiedCollections.count(), 1);
QCOMPARE(unmodifiedCollections.first().extendedMetaData().value(
QStringLiteral("SyncToken")).toString(),
QStringLiteral("synctoken-two"));

addedContacts.clear();
modifiedContacts.clear();
deletedContacts.clear();
Expand Down

0 comments on commit 141af12

Please sign in to comment.