Passed
Push — master ( 777bb3...b79720 )
by
unknown
22:29 queued 11:09
created

Version20251216095100::up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 19
rs 10
c 1
b 1
f 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 Version20251216095100 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Add FK + index for search_engine_ref.resource_node_id (resource_node.id).';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        // Ensure the FK can be created (remove orphan references).
22
        $this->addSql("
23
            UPDATE search_engine_ref ser
24
            SET resource_node_id = NULL
25
            WHERE ser.resource_node_id IS NOT NULL
26
              AND NOT EXISTS (
27
                  SELECT 1
28
                  FROM resource_node rn
29
                  WHERE rn.id = ser.resource_node_id
30
              )
31
        ");
32
33
        // Add index for faster joins and stable naming.
34
        $this->addSql('CREATE INDEX IDX_473F03781BAD783F ON search_engine_ref (resource_node_id)');
35
36
        // Add FK constraint.
37
        $this->addSql("
38
            ALTER TABLE search_engine_ref
39
            ADD CONSTRAINT FK_473F03781BAD783F
40
            FOREIGN KEY (resource_node_id)
41
            REFERENCES resource_node (id)
42
            ON DELETE CASCADE
43
        ");
44
    }
45
46
    public function down(Schema $schema): void
47
    {
48
        $this->addSql('ALTER TABLE search_engine_ref DROP FOREIGN KEY FK_473F03781BAD783F');
49
        $this->addSql('DROP INDEX IDX_473F03781BAD783F ON search_engine_ref');
50
    }
51
}
52