Passed
Pull Request — master (#6506)
by Angel Fernando Quiroz
08:21
created

Version20240112103359::up()   A

Complexity

Conditions 6
Paths 24

Size

Total Lines 53
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 24
nop 1
dl 0
loc 53
rs 9.0111
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Version20240112103359 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
                    variable 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_variable (variable),
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
56
                break;
57
            }
58
        }
59
60
        if (!$fkExists) {
61
            $this->addSql('
62
                ALTER TABLE settings
63
                ADD CONSTRAINT FK_E545A0C5C72FB79B
64
                FOREIGN KEY (value_template_id) REFERENCES settings_value_template (id) ON DELETE SET NULL;
65
            ');
66
            $this->write('Added foreign key constraint from settings to settings_value_template.');
67
68
            $this->addSql('
69
                CREATE INDEX IDX_E545A0C5C72FB79B ON settings (value_template_id);
70
            ');
71
            $this->write('Created index IDX_E545A0C5C72FB79B on settings.value_template_id.');
72
        }
73
    }
74
75
    public function down(Schema $schema): void
76
    {
77
        $schemaManager = $this->connection->createSchemaManager();
78
79
        if ($schemaManager->tablesExist(['settings'])) {
80
            $foreignKeys = $schemaManager->listTableForeignKeys('settings');
81
            foreach ($foreignKeys as $fk) {
82
                if (\in_array('value_template_id', $fk->getLocalColumns(), true)) {
83
                    $this->addSql(\sprintf(
84
                        'ALTER TABLE settings DROP FOREIGN KEY %s',
85
                        $fk->getName()
86
                    ));
87
                    $this->write('Dropped foreign key from settings to settings_value_template.');
88
                }
89
            }
90
91
            $this->addSql('
92
                DROP INDEX IF EXISTS IDX_E545A0C5C72FB79B ON settings;
93
            ');
94
            $this->write('Dropped index IDX_E545A0C5C72FB79B on settings.');
95
        }
96
97
        $columns = $schemaManager->listTableColumns('settings');
98
        if (isset($columns['value_template_id'])) {
99
            $this->addSql('
100
                ALTER TABLE settings DROP COLUMN value_template_id;
101
            ');
102
            $this->write('Dropped value_template_id column from settings table.');
103
        }
104
105
        if ($schemaManager->tablesExist(['settings_value_template'])) {
106
            $this->addSql('DROP TABLE settings_value_template');
107
            $this->write('Dropped table settings_value_template.');
108
        }
109
    }
110
}
111