Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup versions entities in versions:clean command #43263

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/files_versions/lib/Command/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
namespace OCA\Files_Versions\Command;

use OCA\Files_Versions\Db\VersionsMapper;
use OCP\Files\IRootFolder;
use OCP\IUserBackend;
use OCP\IUserManager;
Expand All @@ -37,6 +38,7 @@ class CleanUp extends Command {
public function __construct(
protected IRootFolder $rootFolder,
protected IUserManager $userManager,
protected VersionsMapper $versionMapper,
) {
parent::__construct();
}
Expand Down Expand Up @@ -119,6 +121,9 @@ protected function deleteVersions(string $user, ?string $path = null): void {
\OC_Util::tearDownFS();
\OC_Util::setupFS($user);

$userHomeStorageId = $this->rootFolder->getUserFolder($user)->getStorage()->getCache()->getNumericStorageId();
artonge marked this conversation as resolved.
Show resolved Hide resolved
$this->versionMapper->deleteAllVersionsForUser($userHomeStorageId, $path);
Fixed Show fixed Hide fixed

$fullPath = '/' . $user . '/files_versions' . ($path ? '/' . $path : '');
if ($this->rootFolder->nodeExists($fullPath)) {
$this->rootFolder->get($fullPath)->delete();
artonge marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
35 changes: 35 additions & 0 deletions apps/files_versions/lib/Db/VersionsMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace OCA\Files_Versions\Db;

use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

/**
Expand Down Expand Up @@ -83,4 +84,38 @@ public function deleteAllVersionsForFileId(int $fileId): int {
->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
->executeStatement();
}

public function deleteAllVersionsForUser(int $storageId, string $path = null): void {
$fileIdsGenerator = $this->getFileIdsGenerator($storageId, $path);

$versionEntitiesDeleteQuery = $this->db->getQueryBuilder();
$versionEntitiesDeleteQuery->delete($this->getTableName())
->where($versionEntitiesDeleteQuery->expr()->in('file_id', $versionEntitiesDeleteQuery->createParameter('file_ids')));

foreach ($fileIdsGenerator as $fileIds) {
$versionEntitiesDeleteQuery->setParameter('file_ids', $fileIds, IQueryBuilder::PARAM_INT_ARRAY);
$versionEntitiesDeleteQuery->executeStatement();
}
}

private function getFileIdsGenerator(int $storageId, ?string $path): \Generator {
$offset = 0;
do {
$filesIdsSelect = $this->db->getQueryBuilder();
$filesIdsSelect->select('fileid')
->from('filecache')
->where($filesIdsSelect->expr()->eq('storage', $filesIdsSelect->createNamedParameter($storageId, IQueryBuilder::PARAM_STR)))
->andWhere($filesIdsSelect->expr()->like('path', $filesIdsSelect->createNamedParameter('files' . ($path ? '/' . $this->db->escapeLikeParameter($path) : '') . '/%', IQueryBuilder::PARAM_STR)))
->andWhere($filesIdsSelect->expr()->gt('fileid', $filesIdsSelect->createParameter('offset')))
->setMaxResults(1000)
->orderBy('fileid', 'ASC');

$filesIdsSelect->setParameter('offset', $offset, IQueryBuilder::PARAM_INT);
$result = $filesIdsSelect->executeQuery();
$fileIds = $result->fetchAll(\PDO::FETCH_COLUMN);
$offset = end($fileIds);

yield $fileIds;
} while (!empty($fileIds));
}
}
30 changes: 26 additions & 4 deletions apps/files_versions/tests/Command/CleanupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@

use OC\User\Manager;
use OCA\Files_Versions\Command\CleanUp;
use OCP\Files\Cache\ICache;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Storage\IStorage;
use Test\TestCase;

/**
Expand All @@ -48,16 +51,20 @@ class CleanupTest extends TestCase {
/** @var \PHPUnit\Framework\MockObject\MockObject | IRootFolder */
protected $rootFolder;

/** @var \PHPUnit\Framework\MockObject\MockObject | VersionsMapper */
protected $versionMapper;

protected function setUp(): void {
parent::setUp();

$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')
->disableOriginalConstructor()->getMock();
$this->userManager = $this->getMockBuilder('OC\User\Manager')
->disableOriginalConstructor()->getMock();
$this->versionMapper = $this->getMockBuilder('OCA\Files_Versions\Db\VersionsMapper')
->disableOriginalConstructor()->getMock();


$this->cleanup = new CleanUp($this->rootFolder, $this->userManager);
$this->cleanup = new CleanUp($this->rootFolder, $this->userManager, $this->versionMapper);
}

/**
Expand All @@ -70,6 +77,21 @@ public function testDeleteVersions($nodeExists) {
->with('/testUser/files_versions')
->willReturn($nodeExists);

$userFolder = $this->createMock(Folder::class);
$userHomeStorage = $this->createMock(IStorage::class);
$userHomeStorageCache = $this->createMock(ICache::class);
$this->rootFolder->expects($this->once())
->method('getUserFolder')
->willReturn($userFolder);
$userFolder->expects($this->once())
->method('getStorage')
->willReturn($userHomeStorage);
$userHomeStorage->expects($this->once())
->method('getCache')
->willReturn($userHomeStorageCache);
$userHomeStorageCache->expects($this->once())
->method('getNumericStorageId')
->willReturn(1);

if ($nodeExists) {
$this->rootFolder->expects($this->once())
Expand Down Expand Up @@ -104,7 +126,7 @@ public function testExecuteDeleteListOfUsers() {

$instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp')
->setMethods(['deleteVersions'])
->setConstructorArgs([$this->rootFolder, $this->userManager])
->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper])
->getMock();
$instance->expects($this->exactly(count($userIds)))
->method('deleteVersions')
Expand Down Expand Up @@ -136,7 +158,7 @@ public function testExecuteAllUsers() {

$instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp')
->setMethods(['deleteVersions'])
->setConstructorArgs([$this->rootFolder, $this->userManager])
->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper])
->getMock();

$backend = $this->getMockBuilder(\OCP\UserInterface::class)
Expand Down
7 changes: 6 additions & 1 deletion tests/Core/Command/TwoFactorAuth/CleanupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace Core\Command\TwoFactorAuth;

use OC\Core\Command\TwoFactorAuth\Cleanup;
use OCA\Files_Versions\Db\VersionsMapper;
use OCP\Authentication\TwoFactorAuth\IRegistry;
use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -40,6 +41,9 @@ class CleanupTest extends TestCase {
/** @var IUserManager|MockObject */
private $userManager;

/** @var VersionsMapper|MockObject */
private $versionMapper;

/** @var CommandTester */
private $cmd;

Expand All @@ -48,8 +52,9 @@ protected function setUp(): void {

$this->registry = $this->createMock(IRegistry::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->versionMapper = $this->createMock(VersionsMapper::class);

$cmd = new Cleanup($this->registry, $this->userManager);
$cmd = new Cleanup($this->registry, $this->userManager, $this->versionMapper);
$this->cmd = new CommandTester($cmd);
}

Expand Down
Loading