Passed
Pull Request — master (#5720)
by
unknown
07:02
created

Version20240811221750   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 90
rs 10
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 1 1
A getDescription() 0 3 1
C up() 0 65 11
A hasForeignKey() 0 8 3
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 Doctrine\DBAL\Schema\Schema;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
12
final class Version20240811221750 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Migration to add foreign key constraints to LTI-related tables.';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $schemaManager = $this->connection->createSchemaManager();
22
23
        if ($schema->hasTable('lti_external_tool')) {
24
            $foreignKeys = $schemaManager->listTableForeignKeys('lti_external_tool');
25
26
            if (!$this->hasForeignKey($foreignKeys, 'FK_DB0E04E41BAD783F')) {
27
                $this->addSql('
28
                    ALTER TABLE lti_external_tool
29
                    ADD CONSTRAINT FK_DB0E04E41BAD783F FOREIGN KEY (resource_node_id)
30
                    REFERENCES resource_node (id) ON DELETE CASCADE;
31
                ');
32
            }
33
34
            if (!$this->hasForeignKey($foreignKeys, 'FK_DB0E04E491D79BD3')) {
35
                $this->addSql('
36
                    ALTER TABLE lti_external_tool
37
                    ADD CONSTRAINT FK_DB0E04E491D79BD3 FOREIGN KEY (c_id)
38
                    REFERENCES course (id);
39
                ');
40
            }
41
42
            if (!$this->hasForeignKey($foreignKeys, 'FK_DB0E04E482F80D8B')) {
43
                $this->addSql('
44
                    ALTER TABLE lti_external_tool
45
                    ADD CONSTRAINT FK_DB0E04E482F80D8B FOREIGN KEY (gradebook_eval_id)
46
                    REFERENCES gradebook_evaluation (id) ON DELETE SET NULL;
47
                ');
48
            }
49
50
            if (!$this->hasForeignKey($foreignKeys, 'FK_DB0E04E4727ACA70')) {
51
                $this->addSql('
52
                    ALTER TABLE lti_external_tool
53
                    ADD CONSTRAINT FK_DB0E04E4727ACA70 FOREIGN KEY (parent_id)
54
                    REFERENCES lti_external_tool (id);
55
                ');
56
            }
57
        }
58
59
        if ($schema->hasTable('lti_token')) {
60
            $foreignKeys = $schemaManager->listTableForeignKeys('lti_token');
61
62
            if (!$this->hasForeignKey($foreignKeys, 'FK_EA71C468F7B22CC')) {
63
                $this->addSql('
64
                    ALTER TABLE lti_token
65
                    ADD CONSTRAINT FK_EA71C468F7B22CC FOREIGN KEY (tool_id)
66
                    REFERENCES lti_external_tool (id) ON DELETE CASCADE;
67
                ');
68
            }
69
        }
70
71
        if ($schema->hasTable('lti_lineitem')) {
72
            $foreignKeys = $schemaManager->listTableForeignKeys('lti_lineitem');
73
74
            if (!$this->hasForeignKey($foreignKeys, 'FK_5C76B75D8F7B22CC')) {
75
                $this->addSql('
76
                    ALTER TABLE lti_lineitem
77
                    ADD CONSTRAINT FK_5C76B75D8F7B22CC FOREIGN KEY (tool_id)
78
                    REFERENCES lti_external_tool (id) ON DELETE CASCADE;
79
                ');
80
            }
81
82
            if (!$this->hasForeignKey($foreignKeys, 'FK_5C76B75D1323A575')) {
83
                $this->addSql('
84
                    ALTER TABLE lti_lineitem
85
                    ADD CONSTRAINT FK_5C76B75D1323A575 FOREIGN KEY (evaluation)
86
                    REFERENCES gradebook_evaluation (id) ON DELETE CASCADE;
87
                ');
88
            }
89
        }
90
    }
91
92
    public function down(Schema $schema): void {}
93
94
    private function hasForeignKey(array $foreignKeys, string $foreignKeyName): bool
95
    {
96
        foreach ($foreignKeys as $foreignKey) {
97
            if ($foreignKey->getName() === $foreignKeyName) {
98
                return true;
99
            }
100
        }
101
        return false;
102
    }
103
}
104