|
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
|
|
|
$this->dropIndexIfExists($schema, 'course_rel_user_catalogue', 'IDX_79CA412EA76ED395'); |
|
27
|
|
|
$this->dropIndexIfExists($schema, 'course_rel_user_catalogue', 'IDX_79CA412E91D79BD3'); |
|
28
|
|
|
|
|
29
|
|
|
if ($this->indexExists($schema, 'gradebook_category', 'FK_96A4C705C33F7837')) { |
|
30
|
|
|
$this->addSql('ALTER TABLE gradebook_category DROP INDEX FK_96A4C705C33F7837, ADD UNIQUE INDEX UNIQ_96A4C705C33F7837 (document_id);'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if ($this->columnExists($schema, 'notification_event', 'title') && |
|
34
|
|
|
$this->columnExists($schema, 'notification_event', 'content') && |
|
35
|
|
|
$this->columnExists($schema, 'notification_event', 'link') && |
|
36
|
|
|
$this->columnExists($schema, 'notification_event', 'event_type')) { |
|
37
|
|
|
$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;'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($this->foreignKeyExists($schema, 'c_survey_question_option', 'FK_C4B6F5F1E27F6BF')) { |
|
41
|
|
|
$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;'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$this->dropIndexIfExists($schema, 'c_blog_task_rel_user', 'user'); |
|
45
|
|
|
$this->dropIndexIfExists($schema, 'c_blog_task_rel_user', 'task'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function dropColumnIfExists(Schema $schema, string $tableName, string $columnName): void |
|
49
|
|
|
{ |
|
50
|
|
|
if ($this->columnExists($schema, $tableName, $columnName)) { |
|
51
|
|
|
$this->addSql(sprintf('ALTER TABLE %s DROP COLUMN %s;', $tableName, $columnName)); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function dropIndexIfExists(Schema $schema, string $tableName, string $indexName): void |
|
56
|
|
|
{ |
|
57
|
|
|
if ($this->indexExists($schema, $tableName, $indexName)) { |
|
58
|
|
|
$this->addSql(sprintf('DROP INDEX %s ON %s;', $indexName, $tableName)); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function dropForeignKeyIfExists(Schema $schema, string $tableName, string $foreignKeyName): void |
|
63
|
|
|
{ |
|
64
|
|
|
if ($this->foreignKeyExists($schema, $tableName, $foreignKeyName)) { |
|
65
|
|
|
$this->addSql(sprintf('ALTER TABLE %s DROP FOREIGN KEY %s;', $tableName, $foreignKeyName)); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function columnExists(Schema $schema, string $tableName, string $columnName): bool |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->connection->fetchOne(sprintf( |
|
72
|
|
|
"SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = DATABASE() AND table_name='%s' AND column_name='%s';", |
|
73
|
|
|
$tableName, |
|
74
|
|
|
$columnName |
|
75
|
|
|
)) > 0; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function indexExists(Schema $schema, string $tableName, string $indexName): bool |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->connection->fetchOne(sprintf( |
|
81
|
|
|
"SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS WHERE table_schema = DATABASE() AND table_name='%s' AND index_name='%s';", |
|
82
|
|
|
$tableName, |
|
83
|
|
|
$indexName |
|
84
|
|
|
)) > 0; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function foreignKeyExists(Schema $schema, string $tableName, string $foreignKeyName): bool |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->connection->fetchOne(sprintf( |
|
90
|
|
|
"SELECT COUNT(1) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE table_schema = DATABASE() AND table_name='%s' AND constraint_name='%s';", |
|
91
|
|
|
$tableName, |
|
92
|
|
|
$foreignKeyName |
|
93
|
|
|
)) > 0; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function down(Schema $schema): void {} |
|
97
|
|
|
} |
|
98
|
|
|
|