Passed
Push — master ( 526730...ce0e49 )
by Julito
15:06 queued 06:42
created

Version20170904173000::up()   F

Complexity

Conditions 18
Paths > 20000

Size

Total Lines 85
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 51
c 0
b 0
f 0
nc 131072
nop 1
dl 0
loc 85
rs 0.7

How to fix   Long Method    Complexity   

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
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
6
7
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
8
use Doctrine\DBAL\Schema\Schema;
9
10
/**
11
 * Group changes.
12
 */
13
class Version20170904173000 extends AbstractMigrationChamilo
14
{
15
    public function up(Schema $schema): void
16
    {
17
        $table = $schema->getTable('c_group_info');
18
        if ($table->hasIndex('session_id')) {
19
            $this->addSql('DROP INDEX session_id ON c_group_info');
20
        }
21
        if ($table->hasIndex('course')) {
22
            $this->addSql('DROP INDEX course ON c_group_info');
23
        }
24
        if (!$table->hasColumn('document_access')) {
25
            $this->addSql('ALTER TABLE c_group_info ADD document_access INT DEFAULT 0 NOT NULL;');
26
        }
27
28
        if (false === $table->hasColumn('resource_node_id')) {
29
            $this->addSql('ALTER TABLE c_group_info ADD resource_node_id INT DEFAULT NULL');
30
            $this->addSql('ALTER TABLE c_group_info ADD CONSTRAINT FK_CE0653241BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE');
31
            $this->addSql('CREATE UNIQUE INDEX UNIQ_CE0653241BAD783F ON c_group_info (resource_node_id)');
32
        }
33
34
        if (false === $table->hasIndex('IDX_CE06532412469DE2')) {
35
            $this->addSql('CREATE INDEX IDX_CE06532412469DE2 ON c_group_info (category_id)');
36
        }
37
38
        $this->addSql('ALTER TABLE c_group_info CHANGE category_id category_id INT DEFAULT NULL');
39
40
        $table = $schema->getTable('c_group_rel_user');
41
42
        $this->addSql('DELETE FROM c_group_rel_user WHERE user_id NOT IN (SELECT id FROM user)');
43
        $this->addSql('DELETE FROM c_group_rel_user WHERE group_id NOT IN (SELECT iid FROM c_group_info)');
44
        if (false === $table->hasForeignKey('FK_C5D3D49FA76ED395')) {
45
            $this->addSql(
46
                'ALTER TABLE c_group_rel_user ADD CONSTRAINT FK_C5D3D49FA76ED395 FOREIGN KEY (user_id) REFERENCES user (id)'
47
            );
48
        }
49
50
        if (false === $table->hasForeignKey('FK_C5D3D49FFE54D947')) {
51
            $this->addSql(
52
                'ALTER TABLE c_group_rel_user ADD CONSTRAINT FK_C5D3D49FFE54D947 FOREIGN KEY (group_id) REFERENCES c_group_info (iid)'
53
            );
54
        }
55
        if (false === $table->hasIndex('IDX_C5D3D49FA76ED395')) {
56
            $this->addSql('CREATE INDEX IDX_C5D3D49FA76ED395 ON c_group_rel_user (user_id)');
57
        }
58
        if (false === $table->hasIndex('IDX_C5D3D49FFE54D947')) {
59
            $this->addSql('CREATE INDEX IDX_C5D3D49FFE54D947 ON c_group_rel_user (group_id)');
60
        }
61
62
        $this->addSql('DELETE FROM c_group_rel_tutor WHERE user_id NOT IN (SELECT id FROM user)');
63
        $this->addSql('DELETE FROM c_group_rel_tutor WHERE group_id NOT IN (SELECT iid FROM c_group_info)');
64
65
        $table = $schema->getTable('c_group_rel_tutor');
66
        if (false === $table->hasForeignKey('FK_F6FF71ABA76ED395')) {
67
            $this->addSql(
68
                'ALTER TABLE c_group_rel_tutor ADD CONSTRAINT FK_F6FF71ABA76ED395 FOREIGN KEY (user_id) REFERENCES user (id)'
69
            );
70
        }
71
        if (false === $table->hasForeignKey('FK_F6FF71ABFE54D947')) {
72
            $this->addSql(
73
                'ALTER TABLE c_group_rel_tutor ADD CONSTRAINT FK_F6FF71ABFE54D947 FOREIGN KEY (group_id) REFERENCES c_group_info (iid)'
74
            );
75
        }
76
        if (false === $table->hasIndex('IDX_F6FF71ABA76ED395')) {
77
            $this->addSql('CREATE INDEX IDX_F6FF71ABA76ED395 ON c_group_rel_tutor (user_id)');
78
        }
79
        if (false === $table->hasIndex('IDX_F6FF71ABFE54D947')) {
80
            $this->addSql('CREATE INDEX IDX_F6FF71ABFE54D947 ON c_group_rel_tutor (group_id)');
81
        }
82
83
        $table = $schema->getTable('c_group_category');
84
        if ($table->hasIndex('course')) {
85
            $this->addSql('DROP INDEX course ON c_group_category');
86
        }
87
88
        if (!$table->hasColumn('document_access')) {
89
            $this->addSql('ALTER TABLE c_group_category ADD document_access INT DEFAULT 0 NOT NULL;');
90
        }
91
92
        if (false === $table->hasColumn('resource_node_id')) {
93
            $this->addSql('ALTER TABLE c_group_category ADD resource_node_id INT DEFAULT NULL');
94
            $this->addSql('ALTER TABLE c_group_category ADD CONSTRAINT FK_F8E479F61BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE');
95
            $this->addSql('CREATE UNIQUE INDEX UNIQ_F8E479F61BAD783F ON c_group_category (resource_node_id)');
96
        }
97
98
        if (false === $schema->getTable('c_group_info')->hasForeignKey('FK_CE06532412469DE2')) {
99
            $this->addSql('ALTER TABLE c_group_info ADD CONSTRAINT FK_CE06532412469DE2 FOREIGN KEY (category_id) REFERENCES c_group_category (iid) ON DELETE CASCADE');
100
        }
101
    }
102
103
    public function down(Schema $schema): void
104
    {
105
    }
106
}
107