From 8782b8724aa01ea7dbce91817b352613726cd133 Mon Sep 17 00:00:00 2001 From: yemkareems Date: Thu, 30 May 2024 14:39:58 +0530 Subject: [PATCH 1/3] fix: do a select in systemtag_object_mapping to see if tag exists already in db. if it does not exist alone insert the same or else do nothing Signed-off-by: yemkareems --- .../SystemTag/SystemTagObjectMapper.php | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/private/SystemTag/SystemTagObjectMapper.php b/lib/private/SystemTag/SystemTagObjectMapper.php index 58a9e6a23da85..78e21d0cb2747 100644 --- a/lib/private/SystemTag/SystemTagObjectMapper.php +++ b/lib/private/SystemTag/SystemTagObjectMapper.php @@ -116,6 +116,15 @@ public function assignTags(string $objId, string $objectType, $tagIds): void { $this->assertTagsExist($tagIds); + $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(); + $query = $this->connection->getQueryBuilder(); $query->insert(self::RELATION_TABLE) ->values([ @@ -126,12 +135,17 @@ public function assignTags(string $objId, string $objectType, $tagIds): void { $tagsAssigned = []; foreach ($tagIds as $tagId) { - try { - $query->setParameter('tagid', $tagId); - $query->execute(); - $tagsAssigned[] = $tagId; - } catch (UniqueConstraintViolationException $e) { - // ignore existing relations + if(!in_array($tagId, array_column($rows, 'systemtagid'))) { + // tag not in db so create new one + try { + $query->setParameter('tagid', $tagId); + $query->execute(); + $tagsAssigned[] = $tagId; + } catch (UniqueConstraintViolationException $e) { + // ignore existing relations + } + } else { + //tag exists already don't insert } } From d0883b5e85f757aafacfb6d67062f34299a25b34 Mon Sep 17 00:00:00 2001 From: yemkareems Date: Thu, 30 May 2024 15:38:06 +0530 Subject: [PATCH 2/3] fix: filtering tags based on existing tags and doing a transaction around the select and insert Signed-off-by: yemkareems --- .../SystemTag/SystemTagObjectMapper.php | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/private/SystemTag/SystemTagObjectMapper.php b/lib/private/SystemTag/SystemTagObjectMapper.php index 78e21d0cb2747..852c3a2813524 100644 --- a/lib/private/SystemTag/SystemTagObjectMapper.php +++ b/lib/private/SystemTag/SystemTagObjectMapper.php @@ -115,6 +115,7 @@ public function assignTags(string $objId, string $objectType, $tagIds): void { } $this->assertTagsExist($tagIds); + $this->connection->beginTransaction(); $query = $this->connection->getQueryBuilder(); $query->select('systemtagid') @@ -124,6 +125,12 @@ public function assignTags(string $objId, string $objectType, $tagIds): void { ->andWhere($query->expr()->eq('objectid', $query->createNamedParameter($objId))); $result = $query->executeQuery(); $rows = $result->fetchAll(); + $existingTags = []; + foreach ($rows as $k => $row) { + $existingTags[] = $row['systemtagid']; + } + //filter only tags that do not exist in db + $tagIds = array_diff($tagIds, $existingTags); $query = $this->connection->getQueryBuilder(); $query->insert(self::RELATION_TABLE) @@ -135,20 +142,16 @@ public function assignTags(string $objId, string $objectType, $tagIds): void { $tagsAssigned = []; foreach ($tagIds as $tagId) { - if(!in_array($tagId, array_column($rows, 'systemtagid'))) { - // tag not in db so create new one - try { - $query->setParameter('tagid', $tagId); - $query->execute(); - $tagsAssigned[] = $tagId; - } catch (UniqueConstraintViolationException $e) { - // ignore existing relations - } - } else { - //tag exists already don't insert + try { + $query->setParameter('tagid', $tagId); + $query->execute(); + $tagsAssigned[] = $tagId; + } catch (UniqueConstraintViolationException $e) { + // ignore existing relations } } + $this->connection->commit(); if (empty($tagsAssigned)) { return; } From 87cd78413c89550af3d2e9cd8b2aeca5bc92a9d4 Mon Sep 17 00:00:00 2001 From: yemkareems Date: Wed, 19 Jun 2024 12:29:44 +0530 Subject: [PATCH 3/3] fix: removed unused variable and check if tag ids are empty before starting the inserts Signed-off-by: yemkareems --- lib/private/SystemTag/SystemTagObjectMapper.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/private/SystemTag/SystemTagObjectMapper.php b/lib/private/SystemTag/SystemTagObjectMapper.php index 852c3a2813524..10117eebeaa02 100644 --- a/lib/private/SystemTag/SystemTagObjectMapper.php +++ b/lib/private/SystemTag/SystemTagObjectMapper.php @@ -126,11 +126,16 @@ public function assignTags(string $objId, string $objectType, $tagIds): void { $result = $query->executeQuery(); $rows = $result->fetchAll(); $existingTags = []; - foreach ($rows as $k => $row) { + 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)