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 Version20250929114100 extends AbstractMigrationChamilo |
13
|
|
|
{ |
14
|
|
|
public function getDescription(): string |
15
|
|
|
{ |
16
|
|
|
return 'BlogAttachment: add nullable post_id and comment_id with FKs. Update c_blog_comment (post_id, parent_comment_id, legacy default). Add c_blog_rating.post_id with FK. Includes data cleanup to avoid FK failures.'; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function up(Schema $schema): void |
20
|
|
|
{ |
21
|
|
|
$this->addSql('ALTER TABLE c_blog_attachment ADD COLUMN IF NOT EXISTS post_id INT DEFAULT NULL'); |
22
|
|
|
$this->addSql('ALTER TABLE c_blog_attachment ADD COLUMN IF NOT EXISTS comment_id INT DEFAULT NULL'); |
23
|
|
|
|
24
|
|
|
$this->addSql('CREATE INDEX IF NOT EXISTS IDX_E769AADC4B89032C ON c_blog_attachment (post_id)'); |
25
|
|
|
$this->addSql('CREATE INDEX IF NOT EXISTS IDX_E769AADCF8697D13 ON c_blog_attachment (comment_id)'); |
26
|
|
|
|
27
|
|
|
$this->addSql('ALTER TABLE c_blog_attachment DROP FOREIGN KEY IF EXISTS FK_E769AADC4B89032C'); |
28
|
|
|
$this->addSql('ALTER TABLE c_blog_attachment DROP FOREIGN KEY IF EXISTS FK_E769AADCF8697D13'); |
29
|
|
|
|
30
|
|
|
$this->addSql('ALTER TABLE c_blog_attachment ADD CONSTRAINT FK_E769AADC4B89032C FOREIGN KEY (post_id) REFERENCES c_blog_post (iid) ON DELETE CASCADE'); |
31
|
|
|
$this->addSql('ALTER TABLE c_blog_attachment ADD CONSTRAINT FK_E769AADCF8697D13 FOREIGN KEY (comment_id) REFERENCES c_blog_comment (iid) ON DELETE CASCADE'); |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
// Ensure columns exist |
35
|
|
|
$this->addSql('ALTER TABLE c_blog_comment ADD COLUMN IF NOT EXISTS post_id INT DEFAULT NULL'); |
36
|
|
|
$this->addSql('ALTER TABLE c_blog_comment ADD COLUMN IF NOT EXISTS parent_comment_id INT DEFAULT NULL'); |
37
|
|
|
|
38
|
|
|
// Make them NULLABLE (some previous attempts may have left them NOT NULL) |
39
|
|
|
$this->addSql('ALTER TABLE c_blog_comment MODIFY post_id INT NULL'); |
40
|
|
|
$this->addSql('ALTER TABLE c_blog_comment MODIFY parent_comment_id INT NULL'); |
41
|
|
|
$this->addSql('ALTER TABLE c_blog_comment MODIFY comment_id INT NOT NULL DEFAULT 0'); |
42
|
|
|
|
43
|
|
|
// DATA CLEANUP |
44
|
|
|
$this->addSql('UPDATE c_blog_comment SET post_id = NULL WHERE post_id = 0'); |
45
|
|
|
$this->addSql('UPDATE c_blog_comment SET parent_comment_id = NULL WHERE parent_comment_id = 0'); |
46
|
|
|
|
47
|
|
|
$this->addSql(' |
48
|
|
|
UPDATE c_blog_comment c |
49
|
|
|
LEFT JOIN c_blog_post p ON c.post_id = p.iid |
50
|
|
|
SET c.post_id = NULL |
51
|
|
|
WHERE c.post_id IS NOT NULL AND p.iid IS NULL |
52
|
|
|
'); |
53
|
|
|
|
54
|
|
|
$this->addSql(' |
55
|
|
|
UPDATE c_blog_comment c |
56
|
|
|
LEFT JOIN c_blog_comment pc ON c.parent_comment_id = pc.iid |
57
|
|
|
SET c.parent_comment_id = NULL |
58
|
|
|
WHERE c.parent_comment_id IS NOT NULL AND pc.iid IS NULL |
59
|
|
|
'); |
60
|
|
|
|
61
|
|
|
// Indexes |
62
|
|
|
$this->addSql('CREATE INDEX IF NOT EXISTS IDX_CAA18F14B89032C ON c_blog_comment (post_id)'); |
63
|
|
|
$this->addSql('CREATE INDEX IF NOT EXISTS IDX_CAA18F1BF2AF943 ON c_blog_comment (parent_comment_id)'); |
64
|
|
|
|
65
|
|
|
// FKs |
66
|
|
|
$this->addSql('ALTER TABLE c_blog_comment DROP FOREIGN KEY IF EXISTS FK_CAA18F14B89032C'); |
67
|
|
|
$this->addSql('ALTER TABLE c_blog_comment DROP FOREIGN KEY IF EXISTS FK_CAA18F1BF2AF943'); |
68
|
|
|
|
69
|
|
|
$this->addSql('ALTER TABLE c_blog_comment ADD CONSTRAINT FK_CAA18F14B89032C FOREIGN KEY (post_id) REFERENCES c_blog_post (iid) ON DELETE CASCADE'); |
70
|
|
|
$this->addSql('ALTER TABLE c_blog_comment ADD CONSTRAINT FK_CAA18F1BF2AF943 FOREIGN KEY (parent_comment_id) REFERENCES c_blog_comment (iid) ON DELETE CASCADE'); |
71
|
|
|
|
72
|
|
|
// Ensure column exists |
73
|
|
|
$this->addSql('ALTER TABLE c_blog_rating ADD COLUMN IF NOT EXISTS post_id INT DEFAULT NULL'); |
74
|
|
|
|
75
|
|
|
// Make it NULLABLE in case it exists as NOT NULL |
76
|
|
|
$this->addSql('ALTER TABLE c_blog_rating MODIFY post_id INT NULL'); |
77
|
|
|
|
78
|
|
|
// Cleanup |
79
|
|
|
$this->addSql('UPDATE c_blog_rating SET post_id = NULL WHERE post_id = 0'); |
80
|
|
|
$this->addSql(' |
81
|
|
|
UPDATE c_blog_rating r |
82
|
|
|
LEFT JOIN c_blog_post p ON r.post_id = p.iid |
83
|
|
|
SET r.post_id = NULL |
84
|
|
|
WHERE r.post_id IS NOT NULL AND p.iid IS NULL |
85
|
|
|
'); |
86
|
|
|
|
87
|
|
|
// Index + FK |
88
|
|
|
$this->addSql('CREATE INDEX IF NOT EXISTS IDX_D4E307604B89032C ON c_blog_rating (post_id)'); |
89
|
|
|
$this->addSql('ALTER TABLE c_blog_rating DROP FOREIGN KEY IF EXISTS FK_D4E307604B89032C'); |
90
|
|
|
$this->addSql('ALTER TABLE c_blog_rating ADD CONSTRAINT FK_D4E307604B89032C FOREIGN KEY (post_id) REFERENCES c_blog_post (iid) ON DELETE CASCADE'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function down(Schema $schema): void |
94
|
|
|
{ |
95
|
|
|
$this->addSql('ALTER TABLE c_blog_rating DROP FOREIGN KEY IF EXISTS FK_D4E307604B89032C'); |
96
|
|
|
$this->addSql('DROP INDEX IF NOT EXISTS IDX_D4E307604B89032C ON c_blog_rating'); |
97
|
|
|
$this->addSql('ALTER TABLE c_blog_rating DROP COLUMN IF EXISTS post_id'); |
98
|
|
|
|
99
|
|
|
$this->addSql('ALTER TABLE c_blog_comment DROP FOREIGN KEY IF EXISTS FK_CAA18F14B89032C'); |
100
|
|
|
$this->addSql('ALTER TABLE c_blog_comment DROP FOREIGN KEY IF EXISTS FK_CAA18F1BF2AF943'); |
101
|
|
|
|
102
|
|
|
$this->addSql('DROP INDEX IF NOT EXISTS IDX_CAA18F14B89032C ON c_blog_comment'); |
103
|
|
|
$this->addSql('DROP INDEX IF NOT EXISTS IDX_CAA18F1BF2AF943 ON c_blog_comment'); |
104
|
|
|
|
105
|
|
|
$this->addSql('ALTER TABLE c_blog_comment DROP COLUMN IF EXISTS post_id'); |
106
|
|
|
$this->addSql('ALTER TABLE c_blog_comment DROP COLUMN IF EXISTS parent_comment_id'); |
107
|
|
|
|
108
|
|
|
$this->addSql('ALTER TABLE c_blog_comment MODIFY comment_id INT DEFAULT NULL'); |
109
|
|
|
|
110
|
|
|
$this->addSql('ALTER TABLE c_blog_attachment DROP FOREIGN KEY IF EXISTS FK_E769AADC4B89032C'); |
111
|
|
|
$this->addSql('ALTER TABLE c_blog_attachment DROP FOREIGN KEY IF EXISTS FK_E769AADCF8697D13'); |
112
|
|
|
|
113
|
|
|
$this->addSql('DROP INDEX IF NOT EXISTS IDX_E769AADC4B89032C ON c_blog_attachment'); |
114
|
|
|
$this->addSql('DROP INDEX IF NOT EXISTS IDX_E769AADCF8697D13 ON c_blog_attachment'); |
115
|
|
|
|
116
|
|
|
$this->addSql('ALTER TABLE c_blog_attachment DROP COLUMN IF EXISTS post_id'); |
117
|
|
|
$this->addSql('ALTER TABLE c_blog_attachment DROP COLUMN IF EXISTS comment_id'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|