Passed
Pull Request — master (#7158)
by
unknown
11:01
created

Version20251212104300   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
dl 0
loc 70
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A up() 0 55 3
A down() 0 6 1
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 Version20251212104300 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Prefill resource_node.language_id from course language and copy it to resource_file.language_id when missing.';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        // Decide whether we can join directly via resource_node.cid (preferred)
22
        // or need a fallback via resource_link.c_id (when cid is not present).
23
        $hasCid = false;
24
25
        if ($schema->hasTable('resource_node')) {
26
            $table = $schema->getTable('resource_node');
27
            $hasCid = $table->hasColumn('cid');
28
        }
29
30
        if ($hasCid) {
31
            // Preferred: "created in course" is stored on resource_node.cid
32
            $sqlNode = <<<SQL
33
UPDATE resource_node rn
34
INNER JOIN course c ON c.id = rn.cid
35
INNER JOIN language l ON l.isocode = c.course_language
36
SET rn.language_id = l.id
37
WHERE rn.language_id IS NULL
38
  AND rn.cid IS NOT NULL
39
  AND c.course_language IS NOT NULL
40
  AND c.course_language <> ''
41
SQL;
42
            $this->addSql($sqlNode);
43
        } else {
44
            // Fallback: infer a course from existing links (best-effort).
45
            // We pick MIN(c_id) per resource_node_id to have a deterministic choice.
46
            $sqlNode = <<<SQL
47
UPDATE resource_node rn
48
INNER JOIN (
49
    SELECT rl.resource_node_id, MIN(rl.c_id) AS c_id
50
    FROM resource_link rl
51
    WHERE rl.c_id IS NOT NULL
52
    GROUP BY rl.resource_node_id
53
) x ON x.resource_node_id = rn.id
54
INNER JOIN course c ON c.id = x.c_id
55
INNER JOIN language l ON l.isocode = c.course_language
56
SET rn.language_id = l.id
57
WHERE rn.language_id IS NULL
58
  AND c.course_language IS NOT NULL
59
  AND c.course_language <> ''
60
SQL;
61
            $this->addSql($sqlNode);
62
        }
63
64
        // Copy node language to files when file language is not set yet.
65
        $sqlFile = <<<SQL
66
UPDATE resource_file rf
67
INNER JOIN resource_node rn ON rn.id = rf.resource_node_id
68
SET rf.language_id = rn.language_id
69
WHERE rf.language_id IS NULL
70
  AND rn.language_id IS NOT NULL
71
SQL;
72
73
        $this->addSql($sqlFile);
74
    }
75
76
    public function down(Schema $schema): void
77
    {
78
        // remove language inference.
79
        // Note: if later you start setting languages explicitly, a rollback would also unset them.
80
        $this->addSql('UPDATE resource_file SET language_id = NULL');
81
        $this->addSql('UPDATE resource_node SET language_id = NULL');
82
    }
83
}
84