Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid duplicate tag inserts by checking if the mapping exists already in db #45580

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/private/SystemTag/SystemTagObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ public function assignTags(string $objId, string $objectType, $tagIds): void {
}

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

$query = $this->connection->getQueryBuilder();
yemkareems marked this conversation as resolved.
Show resolved Hide resolved
$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);
yemkareems marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -135,6 +156,7 @@ public function assignTags(string $objId, string $objectType, $tagIds): void {
}
}

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