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

Version20151101082300::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
use Doctrine\DBAL\Types\Type;
9
10
/**
11
 * Calendar color
12
 */
13
class Version20151101082300 extends AbstractMigrationChamilo
14
{
15
    /**
16
     * @param Schema $schema
17
     */
18
    public function up(Schema $schema)
19
    {
20
        $accessUrl = $schema->getTable('access_url');
21
        $accessUrl->getColumn('id')->setUnsigned(false);
22
23
        $this->connection->executeQuery('UPDATE access_url_rel_course SET access_url_id = NULL WHERE access_url_id NOT IN (SELECT id FROM access_url)');
24
25
        $accessUrlRelCourse = $schema->getTable('access_url_rel_course');
26
        $accessUrlRelCourse->getColumn('access_url_id')->setUnsigned(false);
27
        $accessUrlRelCourse->addForeignKeyConstraint('access_url', ['access_url_id'], ['id']);
28
        $accessUrlRelCourse->addForeignKeyConstraint('course', ['c_id'], ['id']);
29
30
        if ($schema->hasTable('class')) {
31
            $schema->renameTable('class', 'class_item');
32
        }
33
34
        if ($schema->hasTable('class_user')) {
35
            $classUser = $schema->getTable('class_user');
36
            $classUser->getColumn('class_id')->setUnsigned(false);
37
            $classUser->getColumn('user_id')->setUnsigned(false);
38
        }
39
40
        $course = $schema->getTable('course');
41
        $course->getColumn('course_type_id')->setUnsigned(false);
42
        $course->addForeignKeyConstraint('room', ['room_id'], ['id']);
43
44
        if ($schema->hasTable('course_rel_class')) {
45
            $courseRelClass = $schema->getTable('course_rel_class');
46
            $courseRelClass->getColumn('class_id')->setUnsigned(false)->setType(Type::getType(Type::INTEGER));
47
        }
48
49
        $courseRelUser = $schema->getTable('course_rel_user');
50
        $courseRelUser->addForeignKeyConstraint('course', ['c_id'], ['id']);
51
        $courseRelUser->addForeignKeyConstraint('user', ['user_id'], ['id']);
52
53
        if ($schema->hasTable('course_type')) {
54
            $courseType = $schema->getTable('course_type');
55
            $courseType->getColumn('id')->setUnsigned(false);
56
        }
57
58
        $schema->getTable('c_announcement')->addIndex(['c_id']);
59
        $schema->getTable('c_announcement_attachment')->addIndex(['c_id']);
60
        $schema->getTable('c_attendance')->addIndex(['c_id']);
61
        $schema->getTable('c_attendance_calendar')->addIndex(['c_id']);
62
63
        $cAttendanceCalendarRelGroup = $schema->getTable('c_attendance_calendar_rel_group');
64
        $cAttendanceCalendarRelGroup->addIndex(['c_id']);
65
        $cAttendanceCalendarRelGroup->addIndex(['group_id']);
66
67
        $schema->getTable('c_attendance_result')->addIndex(['c_id']);
68
69
        $cAttendanceSheet = $schema->getTable('c_attendance_sheet');
70
        $cAttendanceSheet->addIndex(['c_id']);
71
        $cAttendanceSheet->addIndex(['user_id']);
72
73
        $schema->getTable('c_attendance_sheet_log')->addIndex(['c_id']);
74
        $schema->getTable('c_blog')->addIndex(['c_id']);
75
        $schema->getTable('c_blog_attachment')->addIndex(['c_id']);
76
        $schema->getTable('c_blog_comment')->addIndex(['c_id']);
77
        $schema->getTable('c_blog_post')->addIndex(['c_id']);
78
        $schema->getTable('c_blog_rating')->addIndex(['c_id']);
79
80
        $cBlogRelUser = $schema->getTable('c_blog_rel_user');
81
        $cBlogRelUser->getColumn('blog_id')->setUnsigned(false);
82
        $cBlogRelUser->getColumn('user_id')->setUnsigned(false);
83
        $cBlogRelUser->addIndex(['c_id']);
84
85
        $schema->getTable('c_blog_task')->addIndex(['c_id']);
86
87
        $cBlogTaskRelUser = $schema->getTable('c_blog_task_rel_user');
88
        $cBlogTaskRelUser->getColumn('blog_id')->setUnsigned(false);
89
        $cBlogTaskRelUser->getColumn('user_id')->setUnsigned(false);
90
        $cBlogTaskRelUser->getColumn('task_id')->setUnsigned(false);
91
        $cBlogTaskRelUser->addIndex(['c_id']);
92
        $cBlogTaskRelUser->addIndex(['user_id']);
93
        $cBlogTaskRelUser->addIndex(['task_id']);
94
95
        $cCalendarEvent = $schema->getTable('c_calendar_event');
96
        $cCalendarEvent->addIndex(['c_id']);
97
98
        $schema->getTable('c_calendar_event_attachment')->addIndex(['c_id']);
99
        $schema->getTable('c_calendar_event_repeat')->addIndex(['c_id']);
100
        $schema->getTable('c_calendar_event_repeat_not')->addIndex(['c_id']);
101
102
        $cChatConnected = $schema->getTable('c_chat_connected');
103
        $cChatConnected->addIndex(['c_id']);
104
        $cChatConnected->addIndex(['user_id']);
105
106
        $schema->getTable('c_course_setting')->addIndex(['c_id']);
107
        $schema->getTable('c_document')->addIndex(['c_id']);
108
        $schema->getTable('c_dropbox_category')->addIndex(['c_id']);
109
        $schema->getTable('c_dropbox_feedback')->addIndex(['c_id']);
110
        $schema->getTable('c_dropbox_file')->addIndex(['c_id']);
111
112
        $cDropboxPerson = $schema->getTable('c_dropbox_person');
113
        $cDropboxPerson->addIndex(['c_id']);
114
        $cDropboxPerson->addIndex(['user_id']);
115
116
        $cDropboxPost = $schema->getTable('c_dropbox_post');
117
        $cDropboxPost->addIndex(['c_id']);
118
        $cDropboxPost->addIndex(['dest_user_id']);
119
120
        $schema->getTable('c_forum_attachment')->addIndex(['c_id']);
121
        $schema->getTable('c_forum_category')->addIndex(['c_id']);
122
        $schema->getTable('c_forum_forum')->addIndex(['c_id']);
123
124
        $cForumMailcue = $schema->getTable('c_forum_mailcue');
125
        $cForumMailcue->addIndex(['c_id']);
126
        $cForumMailcue->addIndex(['thread_id']);
127
        $cForumMailcue->addIndex(['user_id']);
128
        $cForumMailcue->addIndex(['post_id']);
129
130
        $cForumNotification = $schema->getTable('c_forum_notification');
131
        $cForumNotification->addIndex(['c_id']);
132
        $cForumNotification->addIndex(['thread_id']);
133
        $cForumNotification->addIndex(['post_id']);
134
135
        $schema->getTable('c_forum_post')->addIndex(['c_id']);
136
        $schema->getTable('c_forum_thread')->addIndex(['c_id']);
137
        $schema->getTable('c_forum_thread_qualify')->addIndex(['c_id']);
138
        $schema->getTable('c_forum_thread_qualify_log')->addIndex(['c_id']);
139
        $schema->getTable('c_glossary')->addIndex(['c_id']);
140
        $schema->getTable('c_group_category')->addIndex(['c_id']);
141
        $schema->getTable('c_group_info')->addIndex(['c_id']);
142
        $schema->getTable('c_group_rel_tutor')->addIndex(['c_id']);
143
144
        $schema->getTable('c_group_rel_user')->addIndex(['c_id']);
145
        $schema->getTable('c_link')->addIndex(['c_id']);
146
        $schema->getTable('c_link_category')->addIndex(['c_id']);
147
        $schema->getTable('c_lp')->addIndex(['c_id']);
148
        $schema->getTable('c_lp_category')->addIndex(['c_id']);
149
        $schema->getTable('c_lp_item')->addIndex(['c_id']);
150
        $schema->getTable('c_lp_item_view')->addIndex(['c_id']);
151
        $schema->getTable('c_lp_iv_interaction')->addIndex(['c_id']);
152
        $schema->getTable('c_lp_iv_objective')->addIndex(['c_id']);
153
        $schema->getTable('c_lp_view')->addIndex(['c_id']);
154
        $schema->getTable('c_notebook')->addIndex(['c_id']);
155
        $schema->getTable('c_online_connected')->addIndex(['c_id']);
156
        $schema->getTable('c_online_link')->addIndex(['c_id']);
157
        $schema->getTable('c_permission_group')->addIndex(['c_id']);
158
        $schema->getTable('c_permission_task')->addIndex(['c_id']);
159
        $schema->getTable('c_permission_user')->addIndex(['c_id']);
160
        $schema->getTable('c_quiz')->addIndex(['c_id']);
161
        $schema->getTable('c_quiz_answer')->addIndex(['c_id']);
162
        $schema->getTable('c_quiz_question')->addIndex(['c_id']);
163
        $schema->getTable('c_quiz_question_category')->addIndex(['c_id']);
164
        $schema->getTable('c_quiz_question_option')->addIndex(['c_id']);
165
        $schema->getTable('c_quiz_question_rel_category')->addIndex(['c_id']);
166
167
        $cQuizRelQuestion = $schema->getTable('c_quiz_rel_question');
168
        $cQuizRelQuestion->addIndex(['c_id']);
169
        $cQuizRelQuestion->addIndex(['question_id']);
170
        $cQuizRelQuestion->addIndex(['exercice_id']);
171
172
        $schema->getTable('c_resource')->addIndex(['c_id']);
173
174
        $schema->getTable('c_role')->addIndex(['c_id']);
175
176
        $cRoleGroup = $schema->getTable('c_role_group');
177
        $cRoleGroup->addIndex(['c_id']);
178
        $cRoleGroup->addIndex(['group_id']);
179
180
        $cRolePermissions = $schema->getTable('c_role_permissions');
181
        $cRolePermissions->addIndex(['c_id']);
182
        $cRolePermissions->addIndex(['role_id']);
183
184
        $cRoleUser = $schema->getTable('c_role_user');
185
        $cRoleUser->addIndex(['c_id']);
186
        $cRoleUser->addIndex(['user_id']);
187
188
        $schema->getTable('c_student_publication')->addIndex(['c_id']);
189
        $schema->getTable('c_student_publication_assignment')->addIndex(['c_id']);
190
191
        $cStudentPublicationComment = $schema->getTable('c_student_publication_comment');
192
        $cStudentPublicationComment->addIndex(['c_id']);
193
        $cStudentPublicationComment->addIndex(['user_id']);
194
        $cStudentPublicationComment->addIndex(['work_id']);
195
196
        $cStudentPublicationComment = $schema->getTable('c_student_publication_rel_document');
197
        $cStudentPublicationComment->addIndex(['c_id']);
198
        $cStudentPublicationComment->addIndex(['work_id']);
199
        $cStudentPublicationComment->addIndex(['document_id']);
200
201
        $cStudentPublicationComment = $schema->getTable('c_student_publication_rel_user');
202
        $cStudentPublicationComment->addIndex(['c_id']);
203
        $cStudentPublicationComment->addIndex(['work_id']);
204
        $cStudentPublicationComment->addIndex(['user_id']);
205
206
        $schema->getTable('c_survey')->addIndex(['c_id']);
207
        $schema->getTable('c_survey_answer')->addIndex(['c_id']);
208
        $schema->getTable('c_survey_group')->addIndex(['c_id']);
209
        $schema->getTable('c_survey_invitation')->addIndex(['c_id']);
210
        $schema->getTable('c_survey_question')->addIndex(['c_id']);
211
        $schema->getTable('c_survey_question_option')->addIndex(['c_id']);
212
        $schema->getTable('c_thematic')->addIndex(['c_id']);
213
        $schema->getTable('c_thematic_advance')->addIndex(['c_id']);
214
        $schema->getTable('c_thematic_plan')->addIndex(['c_id']);
215
        $schema->getTable('c_tool')->addIndex(['c_id']);
216
        $schema->getTable('c_tool_intro')->addIndex(['c_id']);
217
        $schema->getTable('c_userinfo_content')->addIndex(['c_id']);
218
        $schema->getTable('c_userinfo_def')->addIndex(['c_id']);
219
        $schema->getTable('c_wiki')->addIndex(['c_id']);
220
        $schema->getTable('c_wiki_conf')->addIndex(['c_id']);
221
        $schema->getTable('c_wiki_discuss')->addIndex(['c_id']);
222
223
        $cWikiMailcue = $schema->getTable('c_wiki_mailcue');
224
        $cWikiMailcue->addIndex(['c_id']);
225
        $cWikiMailcue->addIndex(['user_id']);
226
227
        $schema->getTable('extra_field_values')->addForeignKeyConstraint('extra_field', ['field_id'], ['id']);
228
229
        $session = $schema->getTable('session');
230
        $session->getColumn('id_coach')->setUnsigned(false);
231
        $session->addIndex(['session_category_id']);
232
        $session->addIndex(['id_coach']);
233
        $session->addForeignKeyConstraint('session_category', ['session_category_id'], ['id']);
234
        $session->addForeignKeyConstraint('user', ['id_coach'], ['id']);
235
236
        $this->connection->executeQuery('UPDATE session_category SET access_url_id = 1 WHERE access_url_id NOT IN (SELECT id FROM access_url)');
237
238
        $sessionCategory = $schema->getTable('session_category');
239
        $sessionCategory->addIndex(['access_url_id']);
240
        $sessionCategory->addForeignKeyConstraint('access_url', ['access_url_id'], ['id']);
241
242
        $sessionRelCourse = $schema->getTable('session_rel_course');
243
        $sessionRelCourse->dropColumn('course_code');
244
        $sessionRelCourse->addColumn('id', Type::INTEGER)->setAutoincrement(true);
245
        $sessionRelCourse->getColumn('c_id')->setUnsigned(false);
246
        $sessionRelCourse->setPrimaryKey(['id']);
247
        $sessionRelCourse->addIndex(['c_id']);
248
        $sessionRelCourse->addIndex(['session_id']);
249
        $sessionRelCourse->addForeignKeyConstraint('course', ['c_id'], ['id']);
250
        $sessionRelCourse->addForeignKeyConstraint('session', ['session_id'], ['id']);
251
252
        $this->connection->executeQuery('DELETE FROM session_rel_course_rel_user WHERE c_id NOT IN (SELECT id FROM course)');
253
        $this->connection->executeQuery('DELETE FROM session_rel_course_rel_user WHERE session_id NOT IN (SELECT id FROM session)');
254
        $this->connection->executeQuery('DELETE FROM session_rel_course_rel_user WHERE user_id NOT IN (SELECT id FROM user)');
255
256
        $sessionRelCourseRelUser = $schema->getTable('session_rel_course_rel_user');
257
        $sessionRelCourseRelUser->dropColumn('course_code');
258
        $sessionRelCourseRelUser->addColumn('id', Type::INTEGER)->setAutoincrement(true);
259
        $sessionRelCourseRelUser->getColumn('c_id')->setUnsigned(false);
260
        $sessionRelCourseRelUser->setPrimaryKey(['id']);
261
        $sessionRelCourseRelUser->addIndex(['c_id']);
262
        $sessionRelCourseRelUser->addIndex(['session_id']);
263
        $sessionRelCourseRelUser->addForeignKeyConstraint('course', ['c_id'], ['id']);
264
        $sessionRelCourseRelUser->addForeignKeyConstraint('session', ['session_id'], ['id']);
265
        $sessionRelCourseRelUser->addForeignKeyConstraint('user', ['user_id'], ['id']);
266
267
        $this->connection->executeQuery('DELETE FROM session_rel_user WHERE user_id NOT IN (SELECT id FROM user)');
268
        $this->connection->executeQuery('DELETE FROM session_rel_user WHERE session_id NOT IN (SELECT id FROM session)');
269
270
        $sessionRelUser = $schema->getTable('session_rel_user');
271
        $sessionRelUser->addColumn('moved_to', Type::INTEGER)->setNotnull(false);
272
        $sessionRelUser->addColumn('moved_status', Type::INTEGER)->setNotnull(false);
273
        $sessionRelUser->addColumn('moved_at', Type::DATETIME)->setNotnull(false);
274
        $sessionRelUser->addIndex(['session_id']);
275
        $sessionRelUser->addIndex(['user_id']);
276
        $sessionRelUser->addIndex(['user_id', 'moved_to']);
277
        $sessionRelUser->addForeignKeyConstraint('user', ['user_id'], ['id']);
278
        $sessionRelUser->addForeignKeyConstraint('session', ['session_id'], ['id']);
279
280
        $settingsCurrent = $schema->getTable('settings_current');
281
        $settingsCurrent->addUniqueIndex(['variable', 'subkey', 'access_url']);
282
283
        $settingsCurrent = $schema->getTable('settings_options');
284
        $settingsCurrent->dropIndex('id');
285
        $settingsCurrent->addUniqueIndex(['variable', 'value']);
286
287
        $schema->getTable('track_e_access')->addIndex(['c_id']);
288
        $schema->getTable('track_e_attempt')->addIndex(['c_id']);
289
        $schema->getTable('track_e_course_access')->addIndex(['c_id']);
290
291
        $trackEDefault = $schema->getTable('track_e_default');
292
        $trackEDefault->addIndex(['c_id']);
293
        $trackEDefault->addIndex(['session_id']);
294
295
        $schema->getTable('track_e_downloads')->addIndex(['c_id']);
296
        $schema->getTable('track_e_exercises')->addIndex(['c_id']);
297
        $schema->getTable('track_e_hotpotatoes')->addIndex(['c_id']);
298
        $schema->getTable('track_e_lastaccess')->addIndex(['c_id']);
299
        $schema->getTable('track_e_links')->addIndex(['c_id']);
300
        $schema->getTable('track_e_online')->addIndex(['c_id']);
301
        $schema->getTable('track_e_uploads')->addIndex(['c_id']);
302
        $schema->getTable('user')->addUniqueIndex(['username_canonical']);
303
304
        $this->connection->executeQuery('DELETE FROM usergroup_rel_user WHERE user_id NOT IN (SELECT id FROM user)');
305
        $this->connection->executeQuery('DELETE FROM usergroup_rel_user WHERE usergroup_id NOT IN (SELECT id FROM usergroup)');
306
307
        $usergroupRelUSer = $schema->getTable('usergroup_rel_user');
308
        $usergroupRelUSer->addIndex(['user_id']);
309
        $usergroupRelUSer->addIndex(['usergroup_id']);
310
        $usergroupRelUSer->addForeignKeyConstraint('usergroup', ['usergroup_id'], ['id']);
311
        $usergroupRelUSer->addForeignKeyConstraint('user', ['user_id'], ['id']);
312
    }
313
314
    /**
315
     * @param Schema $schema
316
     */
317
    public function down(Schema $schema)
318
    {
319
    }
320
}
321