Passed
Push — master ( 702f24...55adc1 )
by Yannick
09:28
created

Version20250708002500::down()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 18
nc 8
nop 1
dl 0
loc 33
rs 9.0444
c 1
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
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 Version20250708002500 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Adds value_template_id column to settings table and creates settings_value_template table for JSON templates.';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $schemaManager = $this->connection->createSchemaManager();
22
23
        // Create table settings_value_template
24
        if (!$schemaManager->tablesExist(['settings_value_template'])) {
25
            $this->addSql("
26
            CREATE TABLE settings_value_template (
27
                id INT UNSIGNED AUTO_INCREMENT NOT NULL,
28
                name VARCHAR(190) NOT NULL,
29
                description LONGTEXT DEFAULT NULL,
30
                json_example LONGTEXT DEFAULT NULL,
31
                created_at DATETIME DEFAULT NULL COMMENT '(DC2Type:datetime)',
32
                updated_at DATETIME DEFAULT NULL COMMENT '(DC2Type:datetime)',
33
                UNIQUE INDEX UNIQ_settings_value_template_name (name),
34
                PRIMARY KEY(id)
35
            ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC;
36
        ");
37
            $this->write("Created table settings_value_template.");
38
        }
39
40
        // Add column value_template_id to settings
41
        $columns = $schemaManager->listTableColumns('settings');
42
        if (!isset($columns['value_template_id'])) {
43
            $this->addSql("
44
            ALTER TABLE settings ADD value_template_id INT UNSIGNED DEFAULT NULL;
45
        ");
46
            $this->write("Added value_template_id column to settings table.");
47
        }
48
49
        // Add FK constraint
50
        $foreignKeys = $schemaManager->listTableForeignKeys('settings');
51
        $fkExists = false;
52
        foreach ($foreignKeys as $fk) {
53
            if (in_array('value_template_id', $fk->getLocalColumns(), true)) {
54
                $fkExists = true;
55
                break;
56
            }
57
        }
58
59
        if (!$fkExists) {
60
            $this->addSql("
61
            ALTER TABLE settings
62
                ADD CONSTRAINT FK_E545A0C5C72FB79B
63
                FOREIGN KEY (value_template_id) REFERENCES settings_value_template (id) ON DELETE SET NULL;
64
        ");
65
            $this->write("Added foreign key constraint from settings to settings_value_template.");
66
67
            $this->addSql("
68
            CREATE INDEX IDX_E545A0C5C72FB79B ON settings (value_template_id);
69
        ");
70
            $this->write("Created index IDX_E545A0C5C72FB79B on settings.value_template_id.");
71
        }
72
    }
73
74
    public function down(Schema $schema): void
75
    {
76
        $schemaManager = $this->connection->createSchemaManager();
77
78
        if ($schemaManager->tablesExist(['settings'])) {
79
            $foreignKeys = $schemaManager->listTableForeignKeys('settings');
80
            foreach ($foreignKeys as $fk) {
81
                if (in_array('value_template_id', $fk->getLocalColumns(), true)) {
82
                    $this->addSql(sprintf(
83
                        "ALTER TABLE settings DROP FOREIGN KEY %s",
84
                        $fk->getName()
85
                    ));
86
                    $this->write("Dropped foreign key from settings to settings_value_template.");
87
                }
88
            }
89
90
            $this->addSql("
91
            DROP INDEX IF EXISTS IDX_E545A0C5C72FB79B ON settings;
92
        ");
93
            $this->write("Dropped index IDX_E545A0C5C72FB79B on settings.");
94
        }
95
96
        $columns = $schemaManager->listTableColumns('settings');
97
        if (isset($columns['value_template_id'])) {
98
            $this->addSql("
99
            ALTER TABLE settings DROP COLUMN value_template_id;
100
        ");
101
            $this->write("Dropped value_template_id column from settings table.");
102
        }
103
104
        if ($schemaManager->tablesExist(['settings_value_template'])) {
105
            $this->addSql("DROP TABLE settings_value_template");
106
            $this->write("Dropped table settings_value_template.");
107
        }
108
    }
109
}
110