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

Add migration script to delete unnecessary shared_folders for OFAJ - refs BT#21680 #5565

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
56 changes: 56 additions & 0 deletions src/CoreBundle/Migrations/Schema/V200/Version20240602231700.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);
ywarnier marked this conversation as resolved.
Show resolved Hide resolved

/* For licensing terms, see /license.txt */

namespace Chamilo\CoreBundle\Migrations\Schema\V200;

use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Chamilo\CourseBundle\Repository\CDocumentRepository;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;

final class Version20240602231700 extends AbstractMigrationChamilo
ywarnier marked this conversation as resolved.
Show resolved Hide resolved
{
public function getDescription(): string
ywarnier marked this conversation as resolved.
Show resolved Hide resolved
{
return 'Delete documents that do not have parents based on the path.';
}

public function up(Schema $schema): void
ywarnier marked this conversation as resolved.
Show resolved Hide resolved
{
$documentRepo = $this->container->get(CDocumentRepository::class);

// Query to get the documentIids
$sql = "SELECT cd1.iid, cd1.path, cd1.c_id
FROM c_document cd1
LEFT JOIN c_document cd2 ON cd2.c_id = cd1.c_id AND cd2.path = SUBSTRING_INDEX(cd1.path, '/', 2)
WHERE cd1.path LIKE '/shared_folder_session_%'
AND cd2.iid IS NULL";
$result = $this->connection->executeQuery($sql);
$orphans = $result->fetchAllAssociative();

if (empty($orphans)) {
echo 'No orphan documents found.' . PHP_EOL;
return;
ywarnier marked this conversation as resolved.
Show resolved Hide resolved
}

foreach ($orphans as $itemData) {
echo 'Deleting document with iid: ' . $itemData['iid'] . PHP_EOL;
$document = $documentRepo->find($itemData['iid']);
if ($document) {
if ($document->getResourceNode()) {
$this->entityManager->remove($document->getResourceNode());
}
$this->entityManager->remove($document);
echo 'Deleted document with iid: ' . $itemData['iid'] . PHP_EOL;
} else {
echo 'Document with iid ' . $itemData['iid'] . ' not found.' . PHP_EOL;
}
}

$this->entityManager->flush();
$this->entityManager->clear();
}
}
Loading