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

Version20240826220000::down()   A

Complexity

Conditions 6
Paths 20

Size

Total Lines 32
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 20
nop 1
dl 0
loc 32
rs 9.2222
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, and adjust related indexes and columns.';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $schemaManager = $this->connection->createSchemaManager();
22
23
        if (!$schemaManager->tablesExist(['justification_document'])) {
24
            $this->addSql('
25
                CREATE TABLE justification_document (
26
                    id INT AUTO_INCREMENT NOT NULL,
27
                    code LONGTEXT DEFAULT NULL,
28
                    name LONGTEXT DEFAULT NULL,
29
                    validity_duration INT DEFAULT NULL,
30
                    comment LONGTEXT DEFAULT NULL,
31
                    date_manual_on INT DEFAULT NULL,
32
                    PRIMARY KEY(id)
33
                ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC;
34
            ');
35
        } else {
36
            $this->addSql('
37
                ALTER TABLE justification_document
38
                CHANGE id id INT AUTO_INCREMENT NOT NULL,
39
                CHANGE code code LONGTEXT DEFAULT NULL,
40
                CHANGE name name LONGTEXT DEFAULT NULL,
41
                CHANGE comment comment LONGTEXT DEFAULT NULL;
42
            ');
43
        }
44
45
        if (!$schemaManager->tablesExist(['justification_document_rel_users'])) {
46
            $this->addSql('
47
                CREATE TABLE justification_document_rel_users (
48
                    id INT AUTO_INCREMENT NOT NULL,
49
                    justification_document_id INT DEFAULT NULL,
50
                    user_id INT DEFAULT NULL,
51
                    file_path VARCHAR(255) DEFAULT NULL,
52
                    date_validity DATE DEFAULT NULL,
53
                    INDEX IDX_D1BB19421F2B6144 (justification_document_id),
54
                    INDEX IDX_D1BB1942A76ED395 (user_id),
55
                    PRIMARY KEY(id)
56
                ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC;
57
            ');
58
        } else {
59
            $this->addSql('
60
                ALTER TABLE justification_document_rel_users
61
                CHANGE id id INT AUTO_INCREMENT NOT NULL;
62
            ');
63
64
            $this->addSql('ALTER TABLE justification_document_rel_users DROP FOREIGN KEY IF EXISTS FK_D1BB1942A76ED395;');
65
            $this->addSql('ALTER TABLE justification_document_rel_users DROP FOREIGN KEY IF EXISTS FK_D1BB19421F2B6144;');
66
67
            $this->addSql('DROP INDEX IF EXISTS IDX_D1BB19421F2B6144 ON justification_document_rel_users;');
68
            $this->addSql('DROP INDEX IF EXISTS IDX_D1BB1942A76ED395 ON justification_document_rel_users;');
69
        }
70
71
        $this->addSql('CREATE INDEX IDX_D1BB19421F2B6144 ON justification_document_rel_users (justification_document_id);');
72
        $this->addSql('CREATE INDEX IDX_D1BB1942A76ED395 ON justification_document_rel_users (user_id);');
73
74
        $this->addSql('
75
            ALTER TABLE justification_document_rel_users
76
            ADD CONSTRAINT FK_D1BB1942A76ED395 FOREIGN KEY (user_id)
77
            REFERENCES user (id) ON DELETE CASCADE;
78
        ');
79
80
        $this->addSql('
81
            ALTER TABLE justification_document_rel_users
82
            ADD CONSTRAINT FK_D1BB19421F2B6144 FOREIGN KEY (justification_document_id)
83
            REFERENCES justification_document (id) ON DELETE CASCADE;
84
        ');
85
    }
86
87
    public function down(Schema $schema): void
88
    {
89
        $schemaManager = $this->connection->createSchemaManager();
90
91
        // Drop foreign key constraints
92
        if ($schemaManager->tablesExist(['justification_document_rel_users'])) {
93
            $foreignKeys = $schemaManager->listTableForeignKeys('justification_document_rel_users');
94
            $foreignKeyNames = array_map(fn($fk) => $fk->getName(), $foreignKeys);
95
96
            if (in_array('FK_D1BB19421F2B6144', $foreignKeyNames, true)) {
97
                $this->addSql('
98
                    ALTER TABLE justification_document_rel_users
99
                    DROP FOREIGN KEY FK_D1BB19421F2B6144;
100
                ');
101
            }
102
103
            if (in_array('FK_D1BB1942A76ED395', $foreignKeyNames, true)) {
104
                $this->addSql('
105
                    ALTER TABLE justification_document_rel_users
106
                    DROP FOREIGN KEY FK_D1BB1942A76ED395;
107
                ');
108
            }
109
        }
110
111
        // Drop justification_document_rel_users table
112
        if ($schemaManager->tablesExist(['justification_document_rel_users'])) {
113
            $this->addSql('DROP TABLE justification_document_rel_users;');
114
        }
115
116
        // Drop justification_document table
117
        if ($schemaManager->tablesExist(['justification_document'])) {
118
            $this->addSql('DROP TABLE justification_document;');
119
        }
120
    }
121
}
122