Passed
Push — master ( 6c23c3...c1688f )
by Julito
09:45
created

Version20170625123000::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 1
b 0
f 0
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
class Version20170625123000 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'c_attendance changes';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $em = $this->getEntityManager();
22
        $container = $this->getContainer();
23
24
        // CAttendance
25
        $table = $schema->getTable('c_attendance');
26
        if ($table->hasIndex('course')) {
27
            $this->addSql('DROP INDEX course ON c_attendance');
28
        }
29
        if ($table->hasIndex('session_id')) {
30
            $this->addSql('DROP INDEX session_id ON c_attendance');
31
        }
32
33
        $this->addSql('ALTER TABLE c_attendance CHANGE active active INT NOT NULL');
34
35
        if (false === $table->hasColumn('resource_node_id')) {
36
            $this->addSql('ALTER TABLE c_attendance ADD resource_node_id INT DEFAULT NULL');
37
            $this->addSql('ALTER TABLE c_attendance ADD CONSTRAINT FK_413634921BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE');
38
            $this->addSql('CREATE UNIQUE INDEX UNIQ_413634921BAD783F ON c_attendance (resource_node_id)');
39
        }
40
41
        $table = $schema->getTable('c_attendance_calendar');
42
        if ($table->hasIndex('course')) {
43
            $this->addSql('DROP INDEX course ON c_attendance_calendar');
44
        }
45
46
        if ($table->hasIndex('attendance_id')) {
47
            $this->addSql('DROP INDEX attendance_id ON c_attendance_calendar');
48
        }
49
50
        $this->addSql('ALTER TABLE c_attendance_calendar CHANGE attendance_id attendance_id INT DEFAULT NULL;');
51
52
        if (false === $table->hasForeignKey('FK_AA3A9AB8163DDA15')) {
53
            $this->addSql('ALTER TABLE c_attendance_calendar ADD CONSTRAINT FK_AA3A9AB8163DDA15 FOREIGN KEY (attendance_id) REFERENCES c_attendance (iid);');
54
        }
55
56
        if (false === $table->hasIndex('IDX_AA3A9AB8163DDA15')) {
57
            $this->addSql('CREATE INDEX IDX_AA3A9AB8163DDA15 ON c_attendance_calendar (attendance_id);');
58
        }
59
60
        $table = $schema->getTable('c_attendance_sheet');
61
62
        if ($table->hasIndex('course')) {
63
            $this->addSql('DROP INDEX course ON c_attendance_sheet;');
64
        }
65
66
        if ($table->hasIndex('user')) {
67
            $this->addSql('DROP INDEX user ON c_attendance_sheet;');
68
        }
69
70
        $this->addSql('UPDATE c_attendance_sheet SET attendance_calendar_id = NULL WHERE attendance_calendar_id = 0;');
71
        $this->addSql('UPDATE c_attendance_sheet SET attendance_calendar_id = NULL WHERE attendance_calendar_id = 0;');
72
        $this->addSql('ALTER TABLE c_attendance_sheet CHANGE user_id user_id INT DEFAULT NULL, CHANGE attendance_calendar_id attendance_calendar_id INT DEFAULT NULL');
73
74
        //ALTER TABLE c_attendance_sheet DROP c_id
75
76
        $this->addSql('DELETE FROM c_attendance_sheet WHERE user_id NOT IN (SELECT id FROM user)');
77
        if (false === $table->hasForeignKey('FK_AD1394FAA76ED395')) {
78
            $this->addSql('ALTER TABLE c_attendance_sheet ADD CONSTRAINT FK_AD1394FAA76ED395 FOREIGN KEY (user_id) REFERENCES user (id);');
79
        }
80
81
        if (false === $table->hasForeignKey('FK_AD1394FA19EA43C3')) {
82
            $this->addSql('ALTER TABLE c_attendance_sheet ADD CONSTRAINT FK_AD1394FA19EA43C3 FOREIGN KEY (attendance_calendar_id) REFERENCES c_attendance_calendar (iid);');
83
        }
84
85
        if (false === $table->hasIndex('IDX_AD1394FA19EA43C3')) {
86
            $this->addSql('CREATE INDEX IDX_AD1394FA19EA43C3 ON c_attendance_sheet (attendance_calendar_id);');
87
        }
88
89
        if (false === $table->hasIndex('IDX_AD1394FAA76ED395')) {
90
            $this->addSql('CREATE INDEX IDX_AD1394FAA76ED395 ON c_attendance_sheet (user_id);');
91
        }
92
93
        $table = $schema->getTable('c_attendance_sheet_log');
94
95
        if ($table->hasIndex('course')) {
96
            $this->addSql('DROP INDEX course ON c_attendance_sheet_log;');
97
        }
98
99
        if ($table->hasIndex('user')) {
100
            $this->addSql('DROP INDEX user ON c_attendance_sheet;');
101
        }
102
103
        $this->addSql('DELETE FROM c_attendance_sheet_log WHERE attendance_id = 0');
104
        $this->addSql('DELETE FROM c_attendance_sheet_log WHERE lastedit_user_id = 0');
105
106
        $this->addSql('ALTER TABLE c_attendance_sheet_log CHANGE attendance_id attendance_id INT DEFAULT NULL');
107
        $this->addSql('ALTER TABLE c_attendance_sheet_log CHANGE lastedit_user_id lastedit_user_id INT DEFAULT NULL');
108
109
        if (!$table->hasForeignKey('FK_181D0917163DDA15')) {
110
            $this->addSql(
111
                'ALTER TABLE c_attendance_sheet_log ADD CONSTRAINT FK_181D0917163DDA15 FOREIGN KEY (attendance_id) REFERENCES c_attendance (iid) ON DELETE CASCADE'
112
            );
113
        }
114
        if (!$table->hasForeignKey('FK_181D091731BA5DD')) {
115
            $this->addSql(
116
                'ALTER TABLE c_attendance_sheet_log ADD CONSTRAINT FK_181D091731BA5DD FOREIGN KEY (lastedit_user_id) REFERENCES user (id) ON DELETE CASCADE'
117
            );
118
        }
119
        if (!$table->hasIndex('IDX_181D0917163DDA15')) {
120
            $this->addSql('CREATE INDEX IDX_181D0917163DDA15 ON c_attendance_sheet_log (attendance_id)');
121
        }
122
        if (!$table->hasIndex('IDX_181D091731BA5DD')) {
123
            $this->addSql('CREATE INDEX IDX_181D091731BA5DD ON c_attendance_sheet_log (lastedit_user_id)');
124
        }
125
126
        $table = $schema->getTable('c_attendance_result');
127
128
        if ($table->hasIndex('course')) {
129
            $this->addSql('DROP INDEX course ON c_attendance_result');
130
        }
131
132
        if ($table->hasIndex('user_id')) {
133
            $this->addSql('DROP INDEX user_id ON c_attendance_result');
134
        }
135
136
        if ($table->hasIndex('attendance_id')) {
137
            $this->addSql('DROP INDEX attendance_id ON c_attendance_result;');
138
        }
139
140
        $this->addSql('UPDATE c_attendance_result SET attendance_id = NULL WHERE attendance_id = 0');
141
        $this->addSql('UPDATE c_attendance_result SET user_id = NULL WHERE user_id = 0');
142
143
        $this->addSql('DELETE FROM c_attendance_result WHERE user_id NOT IN (SELECT id FROM user)');
144
145
        //ALTER TABLE c_attendance_result DROP c_id, ;
146
        $this->addSql('ALTER TABLE c_attendance_result CHANGE user_id user_id INT DEFAULT NULL, CHANGE attendance_id attendance_id INT DEFAULT NULL ');
147
148
        //ALTER TABLE c_attendance_sheet DROP c_id
149
150
        if (false === $table->hasForeignKey('FK_2C7640A76ED395')) {
151
            $this->addSql('ALTER TABLE c_attendance_result ADD CONSTRAINT FK_2C7640A76ED395 FOREIGN KEY (user_id) REFERENCES user (id);');
152
        }
153
        if (false === $table->hasForeignKey('FK_2C7640163DDA15')) {
154
            $this->addSql('ALTER TABLE c_attendance_result ADD CONSTRAINT FK_2C7640163DDA15 FOREIGN KEY (attendance_id) REFERENCES c_attendance (iid);');
155
        }
156
        if (false === $table->hasIndex('IDX_2C7640A76ED395')) {
157
            $this->addSql('CREATE INDEX IDX_2C7640A76ED395 ON c_attendance_result (user_id);');
158
        }
159
160
        if (false === $table->hasIndex('IDX_2C7640163DDA15')) {
161
            $this->addSql('CREATE INDEX IDX_2C7640163DDA15 ON c_attendance_result (attendance_id);');
162
        }
163
164
        $table = $schema->getTable('c_attendance_calendar_rel_group');
165
166
        if ($table->hasIndex('course')) {
167
            $this->addSql('DROP INDEX course ON c_attendance_calendar_rel_group');
168
        }
169
170
        //ALTER TABLE c_attendance_calendar_rel_group DROP c_id,
171
        $this->addSql('UPDATE c_attendance_calendar_rel_group SET group_id = NULL WHERE group_id = 0');
172
        $this->addSql('UPDATE c_attendance_calendar_rel_group SET calendar_id = NULL WHERE calendar_id = 0');
173
        $this->addSql('ALTER TABLE c_attendance_calendar_rel_group CHANGE group_id group_id INT DEFAULT NULL, CHANGE calendar_id calendar_id INT DEFAULT NULL;');
174
175
        if (false === $table->hasForeignKey('FK_C2AB1FACFE54D947')) {
176
            if ($table->hasIndex('group')) {
177
                $this->addSql('DROP INDEX `group` ON c_attendance_calendar_rel_group');
178
            }
179
            $this->addSql('ALTER TABLE c_attendance_calendar_rel_group ADD CONSTRAINT FK_C2AB1FACFE54D947 FOREIGN KEY (group_id) REFERENCES c_group_info (iid);');
180
        }
181
182
        if (false === $table->hasForeignKey('FK_C2AB1FACA40A2C8')) {
183
            $this->addSql('ALTER TABLE c_attendance_calendar_rel_group ADD CONSTRAINT FK_C2AB1FACA40A2C8 FOREIGN KEY (calendar_id) REFERENCES c_attendance_calendar (iid);');
184
        }
185
186
        if (false === $table->hasIndex('IDX_C2AB1FACA40A2C8')) {
187
            $this->addSql('CREATE INDEX IDX_C2AB1FACA40A2C8 ON c_attendance_calendar_rel_group (calendar_id);');
188
        }
189
190
        if (false === $table->hasIndex('IDX_C2AB1FACFE54D947')) {
191
            $this->addSql('CREATE INDEX IDX_C2AB1FACFE54D947 ON c_attendance_calendar_rel_group (group_id);');
192
        }
193
    }
194
195
    public function down(Schema $schema): void
196
    {
197
    }
198
}
199