Completed
Push — master ( 91f8d1...9dd64d )
by
unknown
01:55 queued 39s
created

Version20251201173000::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 Version20251201173000 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Pre-fill resource_link.parent_id using the existing resource_node parent hierarchy per context.';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        /**
22
         * This query copies the existing tree structure from resource_node.parent_id
23
         * into resource_link.parent_id, per context
24
         * (course, session, usergroup, group, user, resource_type_group).
25
         *
26
         * For each resource_link (rl), we:
27
         *  - join its node (rn),
28
         *  - locate the parent node (rn.parent_id),
29
         *  - then find the corresponding resource_link (parent_rl) of that parent node
30
         *    in the same context,
31
         *  - and store parent_rl.id into rl.parent_id.
32
         *
33
         * Root nodes (rn.parent_id IS NULL) are left with parent_id = NULL.
34
         *
35
         * This keeps the same visible hierarchy as resource_node for all existing links,
36
         * and will later allow the tree to diverge per context when moving shared documents.
37
         */
38
        $sql = <<<SQL
39
UPDATE resource_link rl
40
INNER JOIN resource_node rn ON rn.id = rl.resource_node_id
41
LEFT JOIN resource_link parent_rl
42
    ON parent_rl.resource_node_id = rn.parent_id
43
   AND (
44
        (parent_rl.c_id = rl.c_id)
45
        OR (parent_rl.c_id IS NULL AND rl.c_id IS NULL)
46
   )
47
   AND (
48
        (parent_rl.session_id = rl.session_id)
49
        OR (parent_rl.session_id IS NULL AND rl.session_id IS NULL)
50
   )
51
   AND (
52
        (parent_rl.usergroup_id = rl.usergroup_id)
53
        OR (parent_rl.usergroup_id IS NULL AND rl.usergroup_id IS NULL)
54
   )
55
   AND (
56
        (parent_rl.group_id = rl.group_id)
57
        OR (parent_rl.group_id IS NULL AND rl.group_id IS NULL)
58
   )
59
   AND (
60
        (parent_rl.user_id = rl.user_id)
61
        OR (parent_rl.user_id IS NULL AND rl.user_id IS NULL)
62
   )
63
   AND parent_rl.resource_type_group = rl.resource_type_group
64
SET rl.parent_id = parent_rl.id
65
WHERE rn.parent_id IS NOT NULL
66
  AND rl.parent_id IS NULL
67
SQL;
68
69
        $this->addSql($sql);
70
    }
71
72
    public function down(Schema $schema): void
73
    {
74
        // Reset parent_id to NULL if we rollback this pre-fill.
75
        // This does not touch the schema (column and FK remain).
76
        $this->addSql('UPDATE resource_link SET parent_id = NULL');
77
    }
78
}
79