Skip to content

Commit

Permalink
Merge pull request #45995 from nextcloud/backport/45580/stable29
Browse files Browse the repository at this point in the history
[stable29] fix: avoid duplicate tag inserts by checking if the mapping exists already in db
  • Loading branch information
artonge authored Jun 25, 2024
2 parents 465ad2f + c47432f commit a0dc778
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/private/SystemTag/SystemTagObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ public function assignTags(string $objId, string $objectType, $tagIds): void {
}

$this->assertTagsExist($tagIds);
$this->connection->beginTransaction();

$query = $this->connection->getQueryBuilder();
$query->select('systemtagid')
->from(self::RELATION_TABLE)
->where($query->expr()->in('systemtagid', $query->createNamedParameter($tagIds, IQueryBuilder::PARAM_INT_ARRAY)))
->andWhere($query->expr()->eq('objecttype', $query->createNamedParameter($objectType)))
->andWhere($query->expr()->eq('objectid', $query->createNamedParameter($objId)));
$result = $query->executeQuery();
$rows = $result->fetchAll();
$existingTags = [];
foreach ($rows as $row) {
$existingTags[] = $row['systemtagid'];
}
//filter only tags that do not exist in db
$tagIds = array_diff($tagIds, $existingTags);
if (empty($tagIds)) {
// no tags to insert so return here
$this->connection->commit();
return;
}

$query = $this->connection->getQueryBuilder();
$query->insert(self::RELATION_TABLE)
Expand All @@ -153,6 +174,7 @@ public function assignTags(string $objId, string $objectType, $tagIds): void {
}
}

$this->connection->commit();
if (empty($tagsAssigned)) {
return;
}
Expand Down

0 comments on commit a0dc778

Please sign in to comment.