From 39006fb6b5cc9ecbe81c4c7eeedbaaa3d0ab5086 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Tue, 10 Aug 2021 08:04:03 +0000 Subject: [PATCH] [DB][Attachments] Use count function rathar than fetch and count, rename to refCount, rather than countDepencies --- components/Avatar/Avatar.php | 5 ++--- components/Avatar/Entity/Avatar.php | 2 +- src/Entity/Attachment.php | 16 +++++----------- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/components/Avatar/Avatar.php b/components/Avatar/Avatar.php index b2090262a6..27a3392609 100644 --- a/components/Avatar/Avatar.php +++ b/components/Avatar/Avatar.php @@ -75,10 +75,9 @@ class Avatar extends Component return Event::next; } - public function onAttachmentCountDependencies(int $attachment_id, int &$dependencies): bool + public function onAttachmentRefCount(int $attachment_id, int &$dependencies): bool { - $avatars = DB::findBy('avatar', ['attachment_id' => $attachment_id]); - $dependencies += count($avatars); + $dependencies += DB::count('avatar', ['attachment_id' => $attachment_id]); return Event::next; } diff --git a/components/Avatar/Entity/Avatar.php b/components/Avatar/Entity/Avatar.php index 8c226a3e16..9e20ab11c7 100644 --- a/components/Avatar/Entity/Avatar.php +++ b/components/Avatar/Entity/Avatar.php @@ -133,7 +133,7 @@ class Avatar extends Entity if ($cascade) { $attachment = $this->getAttachment(); // We can't use $attachment->isSafeDelete() because underlying findBy doesn't respect remove persistence - if ($attachment->countDependencies() - 1 === 0) { + if ($attachment->refCount() - 1 === 0) { $attachment->delete(cascade: true, flush: false); } } diff --git a/src/Entity/Attachment.php b/src/Entity/Attachment.php index d2feff0514..b5308eccb4 100644 --- a/src/Entity/Attachment.php +++ b/src/Entity/Attachment.php @@ -246,28 +246,22 @@ class Attachment extends Entity const URLHASH_ALGO = 'sha256'; const FILEHASH_ALGO = 'sha256'; - /** - * - * Warning: Underlying DB::findBy doesn't respect remove persistence - * @return int - */ - public function countDependencies(): int + public function refCount(): int { $attachment_id = $this->getId(); - $notes = DB::findBy('attachment_to_note', ['attachment_id' => $attachment_id]); - $dependencies = count($notes); - Event::handle('AttachmentCountDependencies', [$attachment_id, &$dependencies]); + $dependencies = DB::count('attachment_to_note', ['attachment_id' => $attachment_id]); + Event::handle('AttachmentRefCount', [$attachment_id, &$dependencies]); return $dependencies; } /** + * @depends Attachment->refCount() * - * @depends Attachment->countDependencies() * @return bool */ public function isSafeDelete(): bool { - return $this->countDependencies() === 0; + return $this->refCount() === 0; } /**