diff --git a/core/BackgroundJobs/PreviewMigrationJob.php b/core/BackgroundJobs/PreviewMigrationJob.php index e5bd2b4a49450..8ddb21966e495 100644 --- a/core/BackgroundJobs/PreviewMigrationJob.php +++ b/core/BackgroundJobs/PreviewMigrationJob.php @@ -9,6 +9,7 @@ namespace OC\Core\BackgroundJobs; +use OC\Preview\Db\Preview; use OC\Preview\PreviewMigrationService; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; diff --git a/core/Command/Preview/Cleanup.php b/core/Command/Preview/Cleanup.php index b4cab0b1c9c88..0fe6efbcc1e46 100644 --- a/core/Command/Preview/Cleanup.php +++ b/core/Command/Preview/Cleanup.php @@ -30,18 +30,20 @@ public function __construct( parent::__construct(); } + #[\Override] protected function configure(): void { $this ->setName('preview:cleanup') ->setDescription('Removes existing preview files'); } + #[\Override] protected function execute(InputInterface $input, OutputInterface $output): int { - if ($this->deletePreviewFromPreviewTable($output) !== 0) { + if ($this->deletePreviewFromFileCacheTable($output) !== 0) { return 1; } - return $this->deletePreviewFromFileCacheTable($output); + return $this->deletePreviewFromPreviewTable($output); } /** @@ -75,9 +77,8 @@ private function deletePreviewFromFileCacheTable(OutputInterface $output): int { $previewFolder = $appDataFolder->get('preview'); } catch (NotFoundException $e) { - $this->logger->error("Previews can't be removed: appdata folder can't be found", ['exception' => $e]); - $output->writeln("Previews can't be removed: preview folder isn't deletable"); - return 1; + $this->logger->info("Legacy previews can't be removed: appdata folder can't be found", ['exception' => $e]); + return 0; } if (!$previewFolder->isDeletable()) { @@ -100,16 +101,6 @@ private function deletePreviewFromFileCacheTable(OutputInterface $output): int { return 1; } - try { - $appDataFolder->newFolder('preview'); - $this->logger->debug('Preview folder recreated'); - $output->writeln('Preview folder recreated', OutputInterface::VERBOSITY_VERBOSE); - } catch (NotPermittedException $e) { - $output->writeln("Preview folder was deleted, but you don't have the permission to create preview folder"); - $this->logger->error("Preview folder was deleted, but you don't have the permission to create preview folder", ['exception' => $e]); - return 1; - } - $output->writeln('Previews removed'); return 0; } diff --git a/lib/private/Preview/PreviewMigrationService.php b/lib/private/Preview/PreviewMigrationService.php index 29893363b38f3..377950da84952 100644 --- a/lib/private/Preview/PreviewMigrationService.php +++ b/lib/private/Preview/PreviewMigrationService.php @@ -94,8 +94,9 @@ public function migrateFileId(int $fileId, bool $flatPath, ?array $entries = nul ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($fileId))) ->setMaxResults(1); - $result = $qb->executeQuery(); - $result = $result->fetchAssociative(); + $cursor = $qb->executeQuery(); + $result = $cursor->fetchAssociative(); + $cursor->closeCursor(); if ($result !== false) { $oldFileIdsToDelete = []; diff --git a/lib/public/Migration/Attributes/IndexMigrationAttribute.php b/lib/public/Migration/Attributes/IndexMigrationAttribute.php index 31b9e2da2778e..a67642cc726c1 100644 --- a/lib/public/Migration/Attributes/IndexMigrationAttribute.php +++ b/lib/public/Migration/Attributes/IndexMigrationAttribute.php @@ -11,7 +11,7 @@ use OCP\AppFramework\Attribute\Consumable; /** - * generic class related to migration attribute about index changes + * Generic class related to migration attribute about index changes */ #[Consumable(since: '30.0.0')] class IndexMigrationAttribute extends MigrationAttribute { @@ -19,7 +19,7 @@ class IndexMigrationAttribute extends MigrationAttribute { * @param string $table name of the database table * @param IndexType|null $type type of the index * @param string $description description of the migration - * @param array $notes notes abour the migration/index + * @param array $notes notes about the migration/index * @since 30.0.0 */ public function __construct( @@ -56,6 +56,7 @@ public function getType(): ?IndexType { * @return $this * @since 30.0.0 */ + #[\Override] public function import(array $data): self { parent::import($data); $this->setType(IndexType::tryFrom($data['type'] ?? '')); @@ -66,6 +67,7 @@ public function import(array $data): self { * @return array * @since 30.0.0 */ + #[\Override] public function jsonSerialize(): array { return array_merge( parent::jsonSerialize(), diff --git a/tests/Core/Command/Preview/CleanupTest.php b/tests/Core/Command/Preview/CleanupTest.php index f1373eff48871..579d5abf6d23d 100644 --- a/tests/Core/Command/Preview/CleanupTest.php +++ b/tests/Core/Command/Preview/CleanupTest.php @@ -26,6 +26,7 @@ class CleanupTest extends TestCase { private PreviewService&MockObject $previewService; private Cleanup $repair; + #[\Override] protected function setUp(): void { parent::setUp(); $this->rootFolder = $this->createMock(IRootFolder::class); @@ -54,7 +55,6 @@ public function testCleanup(): void { $appDataFolder = $this->createMock(Folder::class); $appDataFolder->expects($this->once())->method('get')->with('preview')->willReturn($previewFolder); - $appDataFolder->expects($this->once())->method('newFolder')->with('preview'); $this->rootFolder->expects($this->once()) ->method('getAppDataDirectoryName') @@ -65,13 +65,12 @@ public function testCleanup(): void { ->with('appdata_some_id') ->willReturn($appDataFolder); - $this->output->expects($this->exactly(3))->method('writeln') + $this->output->expects($this->exactly(2))->method('writeln') ->with(self::callback(function (string $message): bool { static $i = 0; return match (++$i) { 1 => $message === 'Preview folder deleted', - 2 => $message === 'Preview folder recreated', - 3 => $message === 'Previews removed' + 2 => $message === 'Previews removed' }; })); @@ -79,8 +78,6 @@ public function testCleanup(): void { } public function testCleanupWhenNotDeletable(): void { - $this->previewService->expects($this->once())->method('deleteAll'); - $previewFolder = $this->createMock(Folder::class); $previewFolder->expects($this->once()) ->method('isDeletable') @@ -91,7 +88,6 @@ public function testCleanupWhenNotDeletable(): void { $appDataFolder = $this->createMock(Folder::class); $appDataFolder->expects($this->once())->method('get')->with('preview')->willReturn($previewFolder); - $appDataFolder->expects($this->never())->method('newFolder')->with('preview'); $this->rootFolder->expects($this->once()) ->method('getAppDataDirectoryName') @@ -110,8 +106,6 @@ public function testCleanupWhenNotDeletable(): void { #[\PHPUnit\Framework\Attributes\DataProvider('dataForTestCleanupWithDeleteException')] public function testCleanupWithDeleteException(string $exceptionClass, string $errorMessage): void { - $this->previewService->expects($this->once())->method('deleteAll'); - $previewFolder = $this->createMock(Folder::class); $previewFolder->expects($this->once()) ->method('isDeletable') @@ -123,7 +117,6 @@ public function testCleanupWithDeleteException(string $exceptionClass, string $e $appDataFolder = $this->createMock(Folder::class); $appDataFolder->expects($this->once())->method('get')->with('preview')->willReturn($previewFolder); - $appDataFolder->expects($this->never())->method('newFolder')->with('preview'); $this->rootFolder->expects($this->once()) ->method('getAppDataDirectoryName') @@ -147,53 +140,16 @@ public static function dataForTestCleanupWithDeleteException(): array { ]; } - public function testCleanupWithCreateException(): void { - $this->previewService->expects($this->once())->method('deleteAll'); - - $previewFolder = $this->createMock(Folder::class); - $previewFolder->expects($this->once()) - ->method('isDeletable') - ->willReturn(true); - - $previewFolder->expects($this->once()) - ->method('delete'); - - $appDataFolder = $this->createMock(Folder::class); - $appDataFolder->expects($this->once())->method('get')->with('preview')->willReturn($previewFolder); - $appDataFolder->expects($this->once())->method('newFolder')->with('preview')->willThrowException(new NotPermittedException()); - - $this->rootFolder->expects($this->once()) - ->method('getAppDataDirectoryName') - ->willReturn('appdata_some_id'); - - $this->rootFolder->expects($this->once()) - ->method('get') - ->with('appdata_some_id') - ->willReturn($appDataFolder); - - $this->output->expects($this->exactly(2))->method('writeln') - ->with(self::callback(function (string $message): bool { - static $i = 0; - return match (++$i) { - 1 => $message === 'Preview folder deleted', - 2 => $message === "Preview folder was deleted, but you don't have the permission to create preview folder", - }; - })); - - $this->logger->expects($this->once())->method('error')->with("Preview folder was deleted, but you don't have the permission to create preview folder"); - - $this->assertEquals(1, $this->repair->run($this->input, $this->output)); - } - public function testCleanupWithPreviewServiceException(): void { + $this->rootFolder->method('getAppDataDirectoryName') + ->willThrowException(new NotFoundException()); + $this->previewService->expects($this->once())->method('deleteAll') ->willThrowException(new NotPermittedException('abc')); + $this->logger->expects($this->once())->method('info')->with("Legacy previews can't be removed: appdata folder can't be found"); $this->logger->expects($this->once())->method('error')->with("Previews can't be removed: exception occurred: abc"); - $this->rootFolder->expects($this->never()) - ->method('get'); - $this->assertEquals(1, $this->repair->run($this->input, $this->output)); } }