Skip to content

Commit

Permalink
add app:workspace:delete cli with debug log (-vvv) (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
jygaulier authored Jan 13, 2025
1 parent 6bd1d5c commit 2b53eb6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 9 deletions.
70 changes: 70 additions & 0 deletions databox/api/src/Command/DeleteWorkspaceCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace App\Command;

use App\Doctrine\Delete\WorkspaceDelete;
use App\Entity\Core\Workspace;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

#[AsCommand('app:workspace:delete')]
class DeleteWorkspaceCommand extends Command
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly WorkspaceDelete $workspaceDelete,
private readonly LoggerInterface $logger)
{
parent::__construct();
}

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

$this
->setDescription('Delete a workspace')
->addArgument('workspace-id', InputArgument::REQUIRED)
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force the deletion without asking for confirmation')
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$workspaceId = $input->getArgument('workspace-id');
$workspace = $this->em->find(Workspace::class, $workspaceId);
if (!$workspace instanceof Workspace) {
throw new \InvalidArgumentException(sprintf('Workspace "%s" not found', $workspaceId));
}

if(!$input->getOption('force')) {
$output->writeln(sprintf('This action will delete the workspace "%s" (%s) and all its content.', $workspace->getName(), $workspace->getId()));
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('Continue? (y/n) ', false);
if (!$helper->ask($input, $output, $question)) {
return 0;
}
}

$output->writeln(sprintf('Deleting workspace "%s" (%s)...', $workspace->getName(), $workspace->getId()));

if (null === $workspace->getDeletedAt()) {
$workspace->setDeletedAt(new \DateTimeImmutable());
$this->em->flush();
}

$this->workspaceDelete->deleteWorkspace($workspaceId, $this->logger);
$output->writeln('Done.');

return 0;
}
}
37 changes: 28 additions & 9 deletions databox/api/src/Doctrine/Delete/WorkspaceDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
use App\Entity\Core\RenditionDefinition;
use App\Entity\Core\Tag;
use App\Entity\Core\Workspace;
use App\Entity\Integration\WorkspaceIntegration;
use App\Entity\Template\AssetDataTemplate;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;

final readonly class WorkspaceDelete
{
public function __construct(
private EntityManagerInterface $em,
private CollectionDelete $collectionDelete,
private IndexCleaner $indexCleaner,
private SoftDeleteToggler $softDeleteToggler,
private CollectionDelete $collectionDelete,
private IndexCleaner $indexCleaner,
private SoftDeleteToggler $softDeleteToggler,
private LoggerInterface $logger,
) {
}

Expand All @@ -38,6 +41,7 @@ public function deleteWorkspace(string $workspaceId): void
throw new \InvalidArgumentException(sprintf('Workspace "%s" is not marked as deleted', $workspace->getId()));
}

$this->logger->debug('Cleaning index.');
$this->indexCleaner->removeWorkspaceFromIndex($workspaceId);

DeferredIndexListener::disable();
Expand All @@ -46,19 +50,20 @@ public function deleteWorkspace(string $workspaceId): void
$this->em->beginTransaction();

$configuration = $this->em->getConnection()->getConfiguration();
$logger = $configuration->getSQLLogger();
$sqlLogger = $configuration->getSQLLogger();
$configuration->setSQLLogger();
try {
$collections = $this->em->getRepository(Collection::class)
->createQueryBuilder('t')
->select('t.id')
->select('t.id, t.title')
->andWhere('t.parent IS NULL')
->andWhere('t.workspace = :ws')
->setParameter('ws', $workspaceId)
->getQuery()
->toIterable();

foreach ($collections as $c) {
$this->logger->debug(sprintf('Deleting collection "%s" (%s).', $c['title'] ?: '', $c['id']));
$this->collectionDelete->deleteCollection((string) $c['id'], true);
}

Expand All @@ -68,6 +73,15 @@ public function deleteWorkspace(string $workspaceId): void
$this->deleteDependencies(AttributeDefinition::class, $workspaceId);
$this->deleteDependencies(AttributeClass::class, $workspaceId);
$this->deleteDependencies(AssetDataTemplate::class, $workspaceId);
$this->deleteDependencies(WorkspaceIntegration::class, $workspaceId);

$nFiles = $this->em->getRepository(File::class)
->createQueryBuilder('t')
->select('COUNT(t.id)')
->andWhere('t.workspace = :ws')
->setParameter('ws', $workspaceId)
->getQuery()
->getSingleScalarResult();

$files = $this->em->getRepository(File::class)
->createQueryBuilder('t')
Expand All @@ -77,13 +91,15 @@ public function deleteWorkspace(string $workspaceId): void
->getQuery()
->toIterable();

foreach ($files as $f) {
$workspace = $this->em->find(File::class, $f['id']);
$this->em->remove($workspace);
$this->logger->debug(sprintf('Deleting %d Files', $nFiles));
foreach ($files as $file) {
$f = $this->em->find(File::class, $file['id']);
$this->em->remove($f);
$this->em->flush();
}

$workspace = $this->em->find(Workspace::class, $workspaceId);
$this->logger->debug('Deleting workspace.');
$this->em->remove($workspace);
$this->em->flush();
$this->em->commit();
Expand All @@ -93,12 +109,15 @@ public function deleteWorkspace(string $workspaceId): void
} finally {
DeferredIndexListener::enable();
$this->softDeleteToggler->enable();
$configuration->setSQLLogger($logger);
$configuration->setSQLLogger($sqlLogger);
}
}

private function deleteDependencies(string $entityClass, string $workspaceId): void
{
$p = explode('\\', $entityClass);
$this->logger->debug(sprintf('Deleting %s(s)', array_pop($p)));

$items = $this->em->getRepository($entityClass)->findBy([
'workspace' => $workspaceId,
]);
Expand Down

0 comments on commit 2b53eb6

Please sign in to comment.