Skip to content

Commit

Permalink
Cleanup versions entity in during versions:clean command
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed Feb 1, 2024
1 parent 4993dac commit 9df8844
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 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 @@ -120,6 +122,7 @@ protected function deleteVersions(string $user, ?string $path = null): void {
\OC_Util::setupFS($user);

$fullPath = '/' . $user . '/files_versions' . ($path ? '/' . $path : '');
$this->versionMapper->deleteAllVersionsForUser($user, $path);
if ($this->rootFolder->nodeExists($fullPath)) {
$this->rootFolder->get($fullPath)->delete();
}
Expand Down
17 changes: 17 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,20 @@ public function deleteAllVersionsForFileId(int $fileId): int {
->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
->executeStatement();
}

public function deleteAllVersionsForUser(string $userId, string $path = null): int {
$deleteQuery = $this->db->getQueryBuilder();
$filesVersionSelect = $this->db->getQueryBuilder();
$filesVersionSelect->select('fileid')
->from('filecache', 'f')
->join('f', 'mounts', 'm', $filesVersionSelect->expr()->eq('f.storage', 'm.storage_id'))
->where($filesVersionSelect->expr()->like('f.path', $deleteQuery->createNamedParameter('files'.($path ? '/' . $path : '').'/%', IQueryBuilder::PARAM_STR)))
->andWhere($filesVersionSelect->expr()->eq('m.user_id', $deleteQuery->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
->andWhere($filesVersionSelect->expr()->eq('m.mount_point', $deleteQuery->createNamedParameter("/$userId/", IQueryBuilder::PARAM_STR)));

$deleteQuery->delete($this->getTableName())
->where($deleteQuery->expr()->in('file_id', $deleteQuery->createFunction($filesVersionSelect->getSQL()), IQueryBuilder::PARAM_INT_ARRAY));

return $deleteQuery->executeStatement();
}
}

0 comments on commit 9df8844

Please sign in to comment.