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

Version20240826220000   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 99
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 51 5
A down() 0 32 6
A getDescription() 0 3 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 Version20240826220000 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Migration to create justification_document and justification_document_rel_users tables with their foreign key relationships.';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $schemaManager = $this->connection->createSchemaManager();
22
23
        // Create justification_document table
24
        if (!$schemaManager->tablesExist(['justification_document'])) {
25
            $this->addSql('
26
                CREATE TABLE justification_document (
27
                    id INT AUTO_INCREMENT NOT NULL,
28
                    code LONGTEXT DEFAULT NULL,
29
                    name LONGTEXT DEFAULT NULL,
30
                    validity_duration INT DEFAULT NULL,
31
                    comment LONGTEXT DEFAULT NULL,
32
                    date_manual_on INT DEFAULT NULL,
33
                    PRIMARY KEY(id)
34
                ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC;
35
            ');
36
        }
37
38
        // Create justification_document_rel_users table
39
        if (!$schemaManager->tablesExist(['justification_document_rel_users'])) {
40
            $this->addSql('
41
                CREATE TABLE justification_document_rel_users (
42
                    id INT AUTO_INCREMENT NOT NULL,
43
                    justification_document_id INT DEFAULT NULL,
44
                    user_id INT DEFAULT NULL,
45
                    file_path VARCHAR(255) DEFAULT NULL,
46
                    date_validity DATE DEFAULT NULL,
47
                    INDEX IDX_D1BB19421F2B6144 (justification_document_id),
48
                    INDEX IDX_D1BB1942A76ED395 (user_id),
49
                    PRIMARY KEY(id)
50
                ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC;
51
            ');
52
        }
53
54
        // Add foreign key constraints
55
        $foreignKeys = $schemaManager->listTableForeignKeys('justification_document_rel_users');
56
        $foreignKeyNames = array_map(fn($fk) => $fk->getName(), $foreignKeys);
57
58
        if (!in_array('FK_D1BB19421F2B6144', $foreignKeyNames, true)) {
59
            $this->addSql('
60
                ALTER TABLE justification_document_rel_users
61
                ADD CONSTRAINT FK_D1BB19421F2B6144
62
                FOREIGN KEY (justification_document_id)
63
                REFERENCES justification_document (id)
64
                ON DELETE CASCADE;
65
            ');
66
        }
67
68
        if (!in_array('FK_D1BB1942A76ED395', $foreignKeyNames, true)) {
69
            $this->addSql('
70
                ALTER TABLE justification_document_rel_users
71
                ADD CONSTRAINT FK_D1BB1942A76ED395
72
                FOREIGN KEY (user_id)
73
                REFERENCES user (id)
74
                ON DELETE CASCADE;
75
            ');
76
        }
77
    }
78
79
    public function down(Schema $schema): void
80
    {
81
        $schemaManager = $this->connection->createSchemaManager();
82
83
        // Drop foreign key constraints
84
        if ($schemaManager->tablesExist(['justification_document_rel_users'])) {
85
            $foreignKeys = $schemaManager->listTableForeignKeys('justification_document_rel_users');
86
            $foreignKeyNames = array_map(fn($fk) => $fk->getName(), $foreignKeys);
87
88
            if (in_array('FK_D1BB19421F2B6144', $foreignKeyNames, true)) {
89
                $this->addSql('
90
                    ALTER TABLE justification_document_rel_users
91
                    DROP FOREIGN KEY FK_D1BB19421F2B6144;
92
                ');
93
            }
94
95
            if (in_array('FK_D1BB1942A76ED395', $foreignKeyNames, true)) {
96
                $this->addSql('
97
                    ALTER TABLE justification_document_rel_users
98
                    DROP FOREIGN KEY FK_D1BB1942A76ED395;
99
                ');
100
            }
101
        }
102
103
        // Drop justification_document_rel_users table
104
        if ($schemaManager->tablesExist(['justification_document_rel_users'])) {
105
            $this->addSql('DROP TABLE justification_document_rel_users;');
106
        }
107
108
        // Drop justification_document table
109
        if ($schemaManager->tablesExist(['justification_document'])) {
110
            $this->addSql('DROP TABLE justification_document;');
111
        }
112
    }
113
}
114