Passed
Pull Request — master (#5558)
by
unknown
07:13
created

Version20201212203600::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
10
use Doctrine\DBAL\Schema\Schema;
11
12
final class Version20201212203600 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Validate and delete orphaned records from c_document with path starting with /shared_folder_session_';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        // Validate orphaned records
22
        $sql = "SELECT cd1.iid, cd1.path, cd1.c_id
23
                FROM c_document cd1
24
                LEFT JOIN c_document cd2 ON cd2.c_id = cd1.c_id AND cd2.path = SUBSTRING_INDEX(cd1.path, '/', 2)
25
                WHERE cd1.path LIKE '/shared_folder_session_%'
26
                AND cd2.iid IS NULL";
27
        $result = $this->connection->executeQuery($sql);
28
        $orphans = $result->fetchAllAssociative();
29
30
        if (!empty($orphans)) {
31
            // Delete orphaned records
32
            $deleteSql = "DELETE cd1 FROM c_document cd1
33
                    LEFT JOIN c_document cd2 ON cd2.c_id = cd1.c_id AND cd2.path = SUBSTRING_INDEX(cd1.path, '/', 2)
34
                    WHERE cd1.path LIKE '/shared_folder_session_%'
35
                    AND cd2.iid IS NULL";
36
            $this->addSql($deleteSql);
37
        }
38
    }
39
40
    public function down(Schema $schema): void
41
    {
42
        // No down migration provided
43
    }
44
}
45