Skip to content

Commit bdbf00c

Browse files
authored
Merge pull request #45994 from nextcloud/backport/45580/stable28
[stable28] fix: avoid duplicate tag inserts by checking if the mapping exists already in db
2 parents aea9eaf + 6f9c67d commit bdbf00c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

lib/private/SystemTag/SystemTagObjectMapper.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,27 @@ public function assignTags(string $objId, string $objectType, $tagIds): void {
133133
}
134134

135135
$this->assertTagsExist($tagIds);
136+
$this->connection->beginTransaction();
137+
138+
$query = $this->connection->getQueryBuilder();
139+
$query->select('systemtagid')
140+
->from(self::RELATION_TABLE)
141+
->where($query->expr()->in('systemtagid', $query->createNamedParameter($tagIds, IQueryBuilder::PARAM_INT_ARRAY)))
142+
->andWhere($query->expr()->eq('objecttype', $query->createNamedParameter($objectType)))
143+
->andWhere($query->expr()->eq('objectid', $query->createNamedParameter($objId)));
144+
$result = $query->executeQuery();
145+
$rows = $result->fetchAll();
146+
$existingTags = [];
147+
foreach ($rows as $row) {
148+
$existingTags[] = $row['systemtagid'];
149+
}
150+
//filter only tags that do not exist in db
151+
$tagIds = array_diff($tagIds, $existingTags);
152+
if (empty($tagIds)) {
153+
// no tags to insert so return here
154+
$this->connection->commit();
155+
return;
156+
}
136157

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

177+
$this->connection->commit();
156178
if (empty($tagsAssigned)) {
157179
return;
158180
}

0 commit comments

Comments
 (0)