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
|
|
|
final class Version20250930125200 extends AbstractMigrationChamilo |
13
|
|
|
{ |
14
|
|
|
public function getDescription(): string |
15
|
|
|
{ |
16
|
|
|
return 'Blog tasks & relations: author_id on c_blog_task, status on rel table, unique indexes, harden NOT NULLs, enforce rating/post FKs.'; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function up(Schema $schema): void |
20
|
|
|
{ |
21
|
|
|
/* ========================= |
22
|
|
|
* c_blog_task |
23
|
|
|
* ========================= */ |
24
|
|
|
// Add author_id if missing |
25
|
|
|
$this->addSql('ALTER TABLE c_blog_task ADD COLUMN IF NOT EXISTS author_id INT DEFAULT NULL'); |
26
|
|
|
|
27
|
|
|
// Normalize defaults/constraints |
28
|
|
|
$this->addSql("ALTER TABLE c_blog_task MODIFY blog_id INT NOT NULL"); |
29
|
|
|
$this->addSql("ALTER TABLE c_blog_task MODIFY task_id INT NOT NULL DEFAULT 0"); |
30
|
|
|
$this->addSql("ALTER TABLE c_blog_task MODIFY description LONGTEXT NOT NULL DEFAULT ''"); |
31
|
|
|
$this->addSql("ALTER TABLE c_blog_task MODIFY color VARCHAR(10) NOT NULL DEFAULT '#0ea5e9'"); |
32
|
|
|
$this->addSql("ALTER TABLE c_blog_task MODIFY system_task TINYINT(1) NOT NULL DEFAULT 0"); |
33
|
|
|
|
34
|
|
|
// FK + index for author_id |
35
|
|
|
$this->addSql('DROP INDEX IF EXISTS IDX_BE09DF0BF675F31B ON c_blog_task'); |
36
|
|
|
$this->addSql('CREATE INDEX IDX_BE09DF0BF675F31B ON c_blog_task (author_id)'); |
37
|
|
|
$this->addSql('ALTER TABLE c_blog_task DROP FOREIGN KEY IF EXISTS FK_BE09DF0BF675F31B'); |
38
|
|
|
$this->addSql('ALTER TABLE c_blog_task ADD CONSTRAINT FK_BE09DF0BF675F31B FOREIGN KEY (author_id) REFERENCES user (id) ON DELETE SET NULL'); |
39
|
|
|
|
40
|
|
|
/* ========================= |
41
|
|
|
* c_blog_task_rel_user |
42
|
|
|
* ========================= */ |
43
|
|
|
// Add status if missing |
44
|
|
|
$this->addSql('ALTER TABLE c_blog_task_rel_user ADD COLUMN IF NOT EXISTS status SMALLINT NOT NULL DEFAULT 0'); |
45
|
|
|
|
46
|
|
|
// Harden NOT NULLs |
47
|
|
|
$this->addSql('ALTER TABLE c_blog_task_rel_user MODIFY blog_id INT NOT NULL'); |
48
|
|
|
$this->addSql('ALTER TABLE c_blog_task_rel_user MODIFY user_id INT NOT NULL'); |
49
|
|
|
$this->addSql('ALTER TABLE c_blog_task_rel_user MODIFY task_id INT NOT NULL'); |
50
|
|
|
|
51
|
|
|
// Deduplicate to safely create unique index |
52
|
|
|
$this->addSql(' |
53
|
|
|
DELETE t1 FROM c_blog_task_rel_user t1 |
54
|
|
|
INNER JOIN c_blog_task_rel_user t2 |
55
|
|
|
ON t1.task_id = t2.task_id |
56
|
|
|
AND t1.user_id = t2.user_id |
57
|
|
|
AND t1.blog_id = t2.blog_id |
58
|
|
|
AND t1.target_date = t2.target_date |
59
|
|
|
AND t1.iid > t2.iid |
60
|
|
|
'); |
61
|
|
|
$this->addSql('DROP INDEX IF EXISTS uniq_task_user_blog_date ON c_blog_task_rel_user'); |
62
|
|
|
$this->addSql('CREATE UNIQUE INDEX uniq_task_user_blog_date ON c_blog_task_rel_user (task_id, user_id, blog_id, target_date)'); |
63
|
|
|
|
64
|
|
|
/* ========================= |
65
|
|
|
* c_blog_rel_user |
66
|
|
|
* ========================= */ |
67
|
|
|
// Harden NOT NULLs |
68
|
|
|
$this->addSql('ALTER TABLE c_blog_rel_user MODIFY blog_id INT NOT NULL'); |
69
|
|
|
$this->addSql('ALTER TABLE c_blog_rel_user MODIFY user_id INT NOT NULL'); |
70
|
|
|
|
71
|
|
|
// Deduplicate then unique index |
72
|
|
|
$this->addSql(' |
73
|
|
|
DELETE r1 FROM c_blog_rel_user r1 |
74
|
|
|
INNER JOIN c_blog_rel_user r2 |
75
|
|
|
ON r1.blog_id = r2.blog_id |
76
|
|
|
AND r1.user_id = r2.user_id |
77
|
|
|
AND r1.iid > r2.iid |
78
|
|
|
'); |
79
|
|
|
$this->addSql('DROP INDEX IF EXISTS uniq_blog_user ON c_blog_rel_user'); |
80
|
|
|
$this->addSql('CREATE UNIQUE INDEX uniq_blog_user ON c_blog_rel_user (blog_id, user_id)'); |
81
|
|
|
|
82
|
|
|
/* ========================= |
83
|
|
|
* c_blog_comment (post_id NOT NULL) |
84
|
|
|
* ========================= */ |
85
|
|
|
$this->addSql('UPDATE c_blog_comment SET post_id = NULL WHERE post_id = 0'); |
86
|
|
|
$this->addSql(' |
87
|
|
|
UPDATE c_blog_comment c |
88
|
|
|
LEFT JOIN c_blog_post p ON c.post_id = p.iid |
89
|
|
|
SET c.post_id = NULL |
90
|
|
|
WHERE c.post_id IS NOT NULL AND p.iid IS NULL |
91
|
|
|
'); |
92
|
|
|
$this->addSql('DELETE FROM c_blog_comment WHERE post_id IS NULL'); |
93
|
|
|
|
94
|
|
|
$this->addSql('ALTER TABLE c_blog_comment MODIFY post_id INT NOT NULL'); |
95
|
|
|
|
96
|
|
|
/* ========================= |
97
|
|
|
* c_blog_rating (post_id NOT NULL + FK a post) |
98
|
|
|
* ========================= */ |
99
|
|
|
$this->addSql('UPDATE c_blog_rating SET post_id = NULL WHERE post_id = 0'); |
100
|
|
|
$this->addSql(' |
101
|
|
|
UPDATE c_blog_rating r |
102
|
|
|
LEFT JOIN c_blog_post p ON r.post_id = p.iid |
103
|
|
|
SET r.post_id = NULL |
104
|
|
|
WHERE r.post_id IS NOT NULL AND p.iid IS NULL |
105
|
|
|
'); |
106
|
|
|
$this->addSql('DELETE FROM c_blog_rating WHERE post_id IS NULL'); |
107
|
|
|
|
108
|
|
|
// NOT NULL + FK |
109
|
|
|
$this->addSql('ALTER TABLE c_blog_rating MODIFY post_id INT NOT NULL'); |
110
|
|
|
$this->addSql('ALTER TABLE c_blog_rating DROP FOREIGN KEY IF EXISTS FK_D4E307604B89032C'); |
111
|
|
|
$this->addSql('CREATE INDEX IF NOT EXISTS IDX_D4E307604B89032C ON c_blog_rating (post_id)'); |
112
|
|
|
$this->addSql('ALTER TABLE c_blog_rating ADD CONSTRAINT FK_D4E307604B89032C FOREIGN KEY (post_id) REFERENCES c_blog_post (iid) ON DELETE CASCADE'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function down(Schema $schema): void |
116
|
|
|
{ |
117
|
|
|
/* c_blog_rating */ |
118
|
|
|
$this->addSql('ALTER TABLE c_blog_rating DROP FOREIGN KEY IF EXISTS FK_D4E307604B89032C'); |
119
|
|
|
$this->addSql('DROP INDEX IF EXISTS IDX_D4E307604B89032C ON c_blog_rating'); |
120
|
|
|
// Allow NULL again for safe rollback |
121
|
|
|
$this->addSql('ALTER TABLE c_blog_rating MODIFY post_id INT NULL'); |
122
|
|
|
|
123
|
|
|
/* c_blog_comment */ |
124
|
|
|
// Allow NULL again for rollback |
125
|
|
|
$this->addSql('ALTER TABLE c_blog_comment MODIFY post_id INT NULL'); |
126
|
|
|
|
127
|
|
|
/* c_blog_rel_user */ |
128
|
|
|
$this->addSql('DROP INDEX IF EXISTS uniq_blog_user ON c_blog_rel_user'); |
129
|
|
|
|
130
|
|
|
/* c_blog_task_rel_user */ |
131
|
|
|
$this->addSql('DROP INDEX IF EXISTS uniq_task_user_blog_date ON c_blog_task_rel_user'); |
132
|
|
|
$this->addSql('ALTER TABLE c_blog_task_rel_user DROP COLUMN IF EXISTS status'); |
133
|
|
|
|
134
|
|
|
/* c_blog_task */ |
135
|
|
|
$this->addSql('ALTER TABLE c_blog_task DROP FOREIGN KEY IF EXISTS FK_BE09DF0BF675F31B'); |
136
|
|
|
$this->addSql('DROP INDEX IF EXISTS IDX_BE09DF0BF675F31B ON c_blog_task'); |
137
|
|
|
$this->addSql('ALTER TABLE c_blog_task DROP COLUMN IF EXISTS author_id'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|