Passed
Push — master ( f762ef...190e8a )
by Julito
09:11
created

Version20150603181728::up()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 78
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 48
nc 64
nop 1
dl 0
loc 78
rs 8.2012
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
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Migrations\Schema\V110;
5
6
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
7
use Doctrine\DBAL\Schema\Schema;
8
9
/**
10
 * Lp changes
11
 */
12
class Version20150603181728 extends AbstractMigrationChamilo
13
{
14
    /**
15
     * @param Schema $schema
16
     */
17
    public function preUp(Schema $schema)
18
    {
19
        $this->addSql("ALTER TABLE course ENGINE=InnoDB");
20
        $this->addSql("ALTER TABLE c_group_info ENGINE=InnoDB");
21
        $this->addSql("ALTER TABLE session ENGINE=InnoDB");
22
        $this->addSql("ALTER TABLE user ENGINE=InnoDB");
23
        $this->addSql("ALTER TABLE c_item_property ENGINE=InnoDB");
24
    }
25
26
    /**
27
     * @param Schema $schema
28
     */
29
    public function up(Schema $schema)
30
    {
31
        $table = $schema->getTable('c_lp');
32
        if (!$table->hasColumn('max_attempts')) {
33
            $this->addSql('ALTER TABLE c_lp ADD max_attempts INT NOT NULL');
34
35
        }
36
37
        if (!$table->hasColumn('subscribe_users')) {
38
            $this->addSql('ALTER TABLE c_lp ADD subscribe_users INT NOT NULL DEFAULT 0');
39
        }
40
41
        $this->addSql('ALTER TABLE c_item_property MODIFY c_id INT DEFAULT NULL');
42
        $this->addSql('ALTER TABLE c_item_property MODIFY lastedit_user_id INT NOT NULL');
43
        $this->addSql('ALTER TABLE c_item_property MODIFY to_group_id INT NULL');
44
        $this->addSql('ALTER TABLE c_item_property MODIFY insert_user_id INT DEFAULT NULL');
45
        $this->addSql('ALTER TABLE c_item_property MODIFY start_visible DATETIME DEFAULT NULL');
46
        $this->addSql('ALTER TABLE c_item_property MODIFY end_visible DATETIME DEFAULT NULL');
47
        $this->addSql('ALTER TABLE c_item_property MODIFY session_id INT DEFAULT NULL');
48
        $this->addSql('ALTER TABLE c_item_property MODIFY to_user_id INT NULL');
49
        $this->addSql("UPDATE c_item_property SET start_visible = NULL WHERE start_visible = '0000-00-00 00:00:00'");
50
        $this->addSql("UPDATE c_item_property SET end_visible = NULL WHERE end_visible = '0000-00-00 00:00:00'");
51
52
        // Remove inconsistencies about non-existing courses
53
        $this->addSql("DELETE FROM c_item_property WHERE session_id IS NOT NULL and session_id <> 0 AND session_id NOT IN (SELECT id FROM session)");
54
        $this->addSql("DELETE FROM c_item_property WHERE to_user_id IS NOT NULL and to_user_id <> 0 AND to_user_id NOT IN (SELECT id FROM user)");
55
        $this->addSql("DELETE FROM c_item_property WHERE to_user_id IS NOT NULL AND to_user_id <> 0 AND to_user_id NOT IN (SELECT id FROM user)");
56
57
        // Sometimes the user was deleted but we need to keep the document.
58
        // Taking first admin
59
        $this->addSql("UPDATE c_item_property SET insert_user_id = (SELECT u.user_id FROM admin a INNER JOIN user u ON (u.user_id = a.user_id AND u.active = 1) LIMIT 1) WHERE insert_user_id IS NOT NULL AND insert_user_id <> 0 AND insert_user_id NOT IN (SELECT id FROM user)");
60
61
        // Remove inconsistencies about non-existing users
62
        $this->addSql("DELETE FROM c_item_property WHERE c_id NOT IN (SELECT id FROM course)");
63
        // Remove inconsistencies about non-existing users
64
        $this->addSql("DELETE FROM course_rel_user WHERE user_id NOT IN (SELECT id FROM user)");
65
        // Remove inconsistencies about non-existing courses
66
        $this->addSql("DELETE FROM c_item_property WHERE c_id NOT IN (SELECT id FROM course)");
67
        // Fix to_group_id
68
        $this->addSql("UPDATE c_item_property SET to_group_id = NULL WHERE to_group_id = 0");
69
        $this->addSql('UPDATE c_item_property SET to_user_id = NULL WHERE to_user_id = 0');
70
        $this->addSql('UPDATE c_item_property SET insert_user_id = NULL WHERE insert_user_id = 0');
71
        $this->addSql('UPDATE c_item_property SET session_id = NULL WHERE session_id = 0');
72
73
        $table = $schema->getTable('c_group_info');
74
        if ($table->hasIndex('idx_cginfo_id') == false) {
75
            $this->addSql('ALTER TABLE c_group_info ADD INDEX idx_cginfo_id (id);');
76
        }
77
78
        if ($table->hasIndex('idx_cginfo_cid') == false) {
79
            $this->addSql('ALTER TABLE c_group_info ADD INDEX idx_cginfo_cid (c_id);');
80
        }
81
82
        $table = $schema->getTable('c_item_property');
83
        if ($table->hasIndex('idx_cip_tgid') == false) {
84
            $this->addSql('ALTER TABLE c_item_property ADD INDEX idx_cip_tgid (to_group_id);');
85
        }
86
87
        if ($table->hasIndex('idx_cip_cid') == false) {
88
            $this->addSql('ALTER TABLE c_item_property ADD INDEX idx_cip_cid (c_id);');
89
        }
90
91
        $this->addSql('ALTER TABLE c_item_property ADD CONSTRAINT FK_1D84C18191D79BD3 FOREIGN KEY (c_id) REFERENCES course(id)');
92
        $this->addSql('ALTER TABLE c_item_property ADD CONSTRAINT FK_1D84C18129F6EE60 FOREIGN KEY (to_user_id) REFERENCES user (id)');
93
        $this->addSql('ALTER TABLE c_item_property ADD CONSTRAINT FK_1D84C1819C859CC3 FOREIGN KEY (insert_user_id) REFERENCES user (id)');
94
        $this->addSql('ALTER TABLE c_item_property ADD CONSTRAINT FK_1D84C181613FECDF FOREIGN KEY (session_id) REFERENCES session (id)');
95
96
        $this->addSql('CREATE INDEX IDX_1D84C18191D79BD3 ON c_item_property (c_id)');
97
        $this->addSql('CREATE INDEX IDX_1D84C18129F6EE60 ON c_item_property (to_user_id)');
98
        $this->addSql('CREATE INDEX IDX_1D84C1819C859CC3 ON c_item_property (insert_user_id)');
99
        $this->addSql('CREATE INDEX IDX_1D84C181613FECDF ON c_item_property (session_id)');
100
101
        // Update c_item_property.to_group_id
102
        $this->addSql('UPDATE c_item_property cip SET cip.to_group_id = (SELECT cgi.iid FROM c_group_info cgi WHERE cgi.c_id = cip.c_id AND cgi.id = cip.to_group_id)');
103
        $this->addSql("DELETE FROM c_item_property WHERE to_group_id IS NOT NULL AND to_group_id <> 0 AND to_group_id NOT IN (SELECT iid FROM c_group_info)");
104
105
        $this->addSql('ALTER TABLE c_item_property ADD CONSTRAINT FK_1D84C181330D47E9 FOREIGN KEY (to_group_id) REFERENCES c_group_info (iid)');
106
        $this->addSql('CREATE INDEX IDX_1D84C181330D47E9 ON c_item_property (to_group_id)');
107
    }
108
109
    /**
110
     * @param Schema $schema
111
     */
112
    public function down(Schema $schema)
113
    {
114
        $this->addSql('ALTER TABLE c_lp DROP max_attempts, DROP subscribe_users');
115
        $this->addSql('ALTER TABLE c_item_property DROP FOREIGN KEY FK_1D84C18191D79BD3');
116
        $this->addSql('ALTER TABLE c_item_property DROP FOREIGN KEY FK_1D84C181330D47E9');
117
        $this->addSql('ALTER TABLE c_item_property DROP FOREIGN KEY FK_1D84C18129F6EE60');
118
        $this->addSql('ALTER TABLE c_item_property DROP FOREIGN KEY FK_1D84C1819C859CC3');
119
        $this->addSql('ALTER TABLE c_item_property DROP FOREIGN KEY FK_1D84C181613FECDF');
120
        $this->addSql('DROP INDEX IDX_1D84C18191D79BD3 ON c_item_property');
121
        $this->addSql('DROP INDEX IDX_1D84C181330D47E9 ON c_item_property');
122
        $this->addSql('DROP INDEX IDX_1D84C18129F6EE60 ON c_item_property');
123
        $this->addSql('DROP INDEX IDX_1D84C1819C859CC3 ON c_item_property');
124
        $this->addSql('DROP INDEX IDX_1D84C181613FECDF ON c_item_property');
125
        $this->addSql('ALTER TABLE c_item_property CHANGE c_id c_id INT NOT NULL, CHANGE insert_user_id insert_user_id INT NOT NULL, CHANGE session_id session_id INT NOT NULL, CHANGE start_visible start_visible DATETIME NOT NULL, CHANGE end_visible end_visible DATETIME NOT NULL');
126
    }
127
}
128