|
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 Doctrine\DBAL\Schema\Schema; |
|
10
|
|
|
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
|
11
|
|
|
|
|
12
|
|
|
final class Version20240811221950 extends AbstractMigrationChamilo |
|
13
|
|
|
{ |
|
14
|
|
|
public function getDescription(): string |
|
15
|
|
|
{ |
|
16
|
|
|
return 'Migration to consolidate schema changes and handle foreign keys, indexes, columns with proper validations.'; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function up(Schema $schema): void |
|
20
|
|
|
{ |
|
21
|
|
|
$this->dropForeignKeyIfExists($schema, 'c_survey_question_option', 'FK_C4B6F5F1E27F6BF'); |
|
22
|
|
|
$this->dropIndexIfExists($schema, 'block', 'path'); |
|
23
|
|
|
$this->dropColumnIfExists($schema, 'c_survey_question_option', 'c_id'); |
|
24
|
|
|
$this->dropColumnIfExists($schema, 'c_survey_question_option', 'question_option_id'); |
|
25
|
|
|
|
|
26
|
|
|
// Drop and recreate foreign keys and indexes for notification_event_rel_user |
|
27
|
|
|
$this->dropIndexIfExists($schema, 'notification_event_rel_user', 'fk_event'); |
|
28
|
|
|
$this->dropIndexIfExists($schema, 'notification_event_rel_user', 'fk_user'); |
|
29
|
|
|
$this->dropForeignKeyIfExists($schema, 'notification_event_rel_user', 'FK_9F7995A671F7E88B'); |
|
30
|
|
|
$this->dropForeignKeyIfExists($schema, 'notification_event_rel_user', 'FK_9F7995A6A76ED395'); |
|
31
|
|
|
|
|
32
|
|
|
$this->addIndexIfNotExists($schema, 'notification_event_rel_user', 'IDX_9F7995A671F7E88B', ['event_id']); |
|
33
|
|
|
$this->addIndexIfNotExists($schema, 'notification_event_rel_user', 'IDX_9F7995A6A76ED395', ['user_id']); |
|
34
|
|
|
$this->addForeignKeyIfNotExists($schema, 'notification_event_rel_user', 'FK_9F7995A671F7E88B', 'event_id', 'notification_event', 'id'); |
|
35
|
|
|
$this->addForeignKeyIfNotExists($schema, 'notification_event_rel_user', 'FK_9F7995A6A76ED395', 'user_id', 'user', 'id'); |
|
36
|
|
|
|
|
37
|
|
|
// Other table modifications |
|
38
|
|
|
$this->dropIndexIfExists($schema, 'course_rel_user_catalogue', 'IDX_79CA412EA76ED395'); |
|
39
|
|
|
$this->dropIndexIfExists($schema, 'course_rel_user_catalogue', 'IDX_79CA412E91D79BD3'); |
|
40
|
|
|
|
|
41
|
|
|
if ($this->indexExists($schema, 'gradebook_category', 'FK_96A4C705C33F7837')) { |
|
42
|
|
|
$this->addSql('ALTER TABLE gradebook_category DROP INDEX FK_96A4C705C33F7837, ADD UNIQUE INDEX UNIQ_96A4C705C33F7837 (document_id);'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if ($this->columnExists($schema, 'notification_event', 'title') && |
|
46
|
|
|
$this->columnExists($schema, 'notification_event', 'content') && |
|
47
|
|
|
$this->columnExists($schema, 'notification_event', 'link') && |
|
48
|
|
|
$this->columnExists($schema, 'notification_event', 'event_type')) { |
|
49
|
|
|
$this->addSql('ALTER TABLE notification_event CHANGE title title VARCHAR(255) NOT NULL, CHANGE content content LONGTEXT DEFAULT NULL, CHANGE link link LONGTEXT DEFAULT NULL, CHANGE event_type event_type VARCHAR(255) NOT NULL;'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if ($this->foreignKeyExists($schema, 'c_survey_question_option', 'FK_C4B6F5F1E27F6BF')) { |
|
53
|
|
|
$this->addSql('ALTER TABLE c_survey_question_option ADD CONSTRAINT FK_C4B6F5F1E27F6BF FOREIGN KEY (question_id) REFERENCES c_survey_question (iid) ON DELETE SET NULL;'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$this->dropIndexIfExists($schema, 'c_blog_task_rel_user', 'user'); |
|
58
|
|
|
$this->dropIndexIfExists($schema, 'c_blog_task_rel_user', 'task'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function dropColumnIfExists(Schema $schema, string $tableName, string $columnName): void |
|
62
|
|
|
{ |
|
63
|
|
|
if ($this->columnExists($schema, $tableName, $columnName)) { |
|
64
|
|
|
$this->addSql(sprintf('ALTER TABLE %s DROP COLUMN %s;', $tableName, $columnName)); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function dropIndexIfExists(Schema $schema, string $tableName, string $indexName): void |
|
69
|
|
|
{ |
|
70
|
|
|
if ($this->indexExists($schema, $tableName, $indexName)) { |
|
71
|
|
|
$this->addSql(sprintf('DROP INDEX %s ON %s;', $indexName, $tableName)); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function dropForeignKeyIfExists(Schema $schema, string $tableName, string $foreignKeyName): void |
|
76
|
|
|
{ |
|
77
|
|
|
if ($this->foreignKeyExists($schema, $tableName, $foreignKeyName)) { |
|
78
|
|
|
$this->addSql(sprintf('ALTER TABLE %s DROP FOREIGN KEY %s;', $tableName, $foreignKeyName)); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function addIndexIfNotExists(Schema $schema, string $tableName, string $indexName, array $columns): void |
|
83
|
|
|
{ |
|
84
|
|
|
if (!$this->indexExists($schema, $tableName, $indexName)) { |
|
85
|
|
|
$this->addSql(sprintf('CREATE INDEX %s ON %s (%s);', $indexName, $tableName, implode(',', $columns))); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function addForeignKeyIfNotExists(Schema $schema, string $tableName, string $foreignKeyName, string $localColumn, string $referencedTable, string $referencedColumn): void |
|
90
|
|
|
{ |
|
91
|
|
|
if (!$this->foreignKeyExists($schema, $tableName, $foreignKeyName)) { |
|
92
|
|
|
$this->addSql(sprintf( |
|
93
|
|
|
'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s);', |
|
94
|
|
|
$tableName, |
|
95
|
|
|
$foreignKeyName, |
|
96
|
|
|
$localColumn, |
|
97
|
|
|
$referencedTable, |
|
98
|
|
|
$referencedColumn |
|
99
|
|
|
)); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function columnExists(Schema $schema, string $tableName, string $columnName): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->connection->fetchOne(sprintf( |
|
106
|
|
|
"SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = DATABASE() AND table_name='%s' AND column_name='%s';", |
|
107
|
|
|
$tableName, |
|
108
|
|
|
$columnName |
|
109
|
|
|
)) > 0; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function indexExists(Schema $schema, string $tableName, string $indexName): bool |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->connection->fetchOne(sprintf( |
|
115
|
|
|
"SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS WHERE table_schema = DATABASE() AND table_name='%s' AND index_name='%s';", |
|
116
|
|
|
$tableName, |
|
117
|
|
|
$indexName |
|
118
|
|
|
)) > 0; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function foreignKeyExists(Schema $schema, string $tableName, string $foreignKeyName): bool |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->connection->fetchOne(sprintf( |
|
124
|
|
|
"SELECT COUNT(1) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE table_schema = DATABASE() AND table_name='%s' AND constraint_name='%s';", |
|
125
|
|
|
$tableName, |
|
126
|
|
|
$foreignKeyName |
|
127
|
|
|
)) > 0; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function down(Schema $schema): void {} |
|
131
|
|
|
} |
|
132
|
|
|
|