From faeafa9ae1fe39a1802d86190605589dd807dc90 Mon Sep 17 00:00:00 2001 From: BentiGorlich Date: Tue, 10 Dec 2024 15:00:34 +0100 Subject: [PATCH] Remove default local domain for entries When an entry is created it will be assigned a domain depending on its url. If it does not have one it previously fell back to the local domain. This commit removes this fallback as it can introduce long-running queries, when calculating the new number of entries in the domain Also added a command to remove all remote entries from this local domain (not a migration as we don't know which id the local domain has) --- ...oveRemoteEntriesFromLocalDomainCommand.php | 63 +++++++++++++++++++ src/Service/DomainManager.php | 10 +-- 2 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 src/Command/Update/RemoveRemoteEntriesFromLocalDomainCommand.php diff --git a/src/Command/Update/RemoveRemoteEntriesFromLocalDomainCommand.php b/src/Command/Update/RemoveRemoteEntriesFromLocalDomainCommand.php new file mode 100644 index 000000000..425300ce8 --- /dev/null +++ b/src/Command/Update/RemoveRemoteEntriesFromLocalDomainCommand.php @@ -0,0 +1,63 @@ +settingsManager->get('KBIN_DOMAIN'); + $domainName = preg_replace('/^www\./i', '', parse_url($domainName)['host']); + + $domain = $this->repository->findOneByName($domainName); + if (!$domain) { + $io->warning(\sprintf('There is no local domain like %s', $domainName)); + + return Command::SUCCESS; + } + + $countBeforeSql = 'SELECT COUNT(*) as ctn FROM entry WHERE domain_id = :dId'; + $stmt1 = $this->entityManager->getConnection()->prepare($countBeforeSql); + $countBefore = \intval($stmt1->executeQuery(['dId' => $domain->getId()])->fetchOne()); + + $sql = 'UPDATE entry SET domain_id = NULL WHERE domain_id = :dId AND ap_id IS NOT NULL'; + $stmt2 = $this->entityManager->getConnection()->prepare($sql); + $stmt2->executeStatement(['dId' => $domain->getId()]); + + $countAfterSql = 'SELECT COUNT(*) as ctn FROM entry WHERE domain_id = :dId'; + $stmt3 = $this->entityManager->getConnection()->prepare($countAfterSql); + $countAfter = \intval($stmt3->executeQuery(['dId' => $domain->getId()])->fetchOne()); + + $sql = 'UPDATE domain SET entry_count = :c WHERE id = :dId'; + $stmt4 = $this->entityManager->getConnection()->prepare($sql); + $stmt4->executeStatement(['c' => $countAfter, 'dId' => $domain->getId()]); + + $io->success(\sprintf('Removed %d entries from the domain %s, now only %d entries are left', $countBefore - $countAfter, $domainName, $countAfter)); + + return Command::SUCCESS; + } +} diff --git a/src/Service/DomainManager.php b/src/Service/DomainManager.php index 943112126..22b9a29fd 100644 --- a/src/Service/DomainManager.php +++ b/src/Service/DomainManager.php @@ -19,13 +19,15 @@ public function __construct( private readonly DomainRepository $repository, private readonly EventDispatcherInterface $dispatcher, private readonly EntityManagerInterface $entityManager, - private readonly SettingsManager $settingsManager ) { } - public function extract(DomainInterface $subject): DomainInterface + public function extract(DomainInterface $subject): void { - $domainName = $subject->getUrl() ?? 'https://'.$this->settingsManager->get('KBIN_DOMAIN'); + $domainName = $subject->getUrl(); + if (!$domainName) { + return; + } $domainName = preg_replace('/^www\./i', '', parse_url($domainName)['host']); @@ -41,8 +43,6 @@ public function extract(DomainInterface $subject): DomainInterface $domain->updateCounts(); $this->entityManager->flush(); - - return $subject; } public function subscribe(Domain $domain, User $user): void