Skip to content

Commit 3565857

Browse files
committed
fix: Revert change to Tags
Let's keep this for a later refactor Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent 8281790 commit 3565857

4 files changed

Lines changed: 39 additions & 8 deletions

File tree

lib/private/AppFramework/ORM/EntityManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function getRepository(string $entityClass): Repository {
6464
* @template T of object
6565
* @psalm-param T $entity
6666
* @return T
67+
* @throws Exception
6768
*/
6869
public function insert(object $entity): object {
6970
$entityInfo = $this->getEntityInfo($entity::class);

lib/private/Tagging/TagMapper.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace OC\Tagging;
1010

11+
use OCP\AppFramework\Db\DoesNotExistException;
1112
use OCP\AppFramework\ORM\Repository;
1213

1314
/**
@@ -33,4 +34,17 @@ public function loadTags(array $owners, string $type): array {
3334
'name' => 'ASC',
3435
]));
3536
}
37+
38+
public function tagExists(Tag $tag): bool {
39+
try {
40+
$this->findOneBy([
41+
'owner' => $tag->owner,
42+
'type' => $tag->type,
43+
'name' => $tag->name,
44+
]);
45+
return true;
46+
} catch (DoesNotExistException) {
47+
return false;
48+
}
49+
}
3650
}

lib/private/Tags.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,8 @@ public function addMultiple(string|array $names, bool $sync = false, ?int $id =
320320
*/
321321
protected function save(): void {
322322
foreach ($this->tags as $tag) {
323-
try {
323+
if (!$this->mapper->tagExists($tag)) {
324324
$this->mapper->insert($tag);
325-
} catch (Exception $e) {
326-
if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
327-
$this->logger->error($e->getMessage(), [
328-
'exception' => $e,
329-
'app' => 'core',
330-
]);
331-
}
332325
}
333326
}
334327

lib/public/AppFramework/ORM/Repository.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ private function findJoinedEntity(IQueryBuilder $query, array $relations): objec
356356
*
357357
* @psalm-param T $entity
358358
* @return T
359+
* @throws Exception
359360
* @since 35.0.0
360361
*/
361362
public function insert(object $entity): object {
@@ -379,6 +380,28 @@ public function delete(object $entity): void {
379380
$this->entityManager->delete($entity);
380381
}
381382

383+
/**
384+
* Tries to create a new entry in the db from an entity and
385+
* updates an existing entry if duplicate keys are detected
386+
* by the database
387+
*
388+
* @param T $entity the entity that should be created/updated
389+
* @return T the saved entity with the (new) id
390+
* @throws Exception
391+
* @throws \InvalidArgumentException if entity has no id
392+
* @since 15.0.0
393+
*/
394+
public function insertOrUpdate(object $entity): object {
395+
try {
396+
return $this->insert($entity);
397+
} catch (Exception $ex) {
398+
if ($ex->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
399+
return $this->update($entity);
400+
}
401+
throw $ex;
402+
}
403+
}
404+
382405
/**
383406
* Finds entities by a set of criteria, keyed by property name.
384407
*

0 commit comments

Comments
 (0)