feat(migration): update blogs table to add nullable sources field and enforce JSON NOT NULL constraint

This commit is contained in:
hamed
2026-07-23 22:48:47 +03:30
parent 21bb434053
commit 0e429126de
+8 -1
View File
@@ -22,7 +22,14 @@ final class Version20260723171254 extends AbstractMigration
// Scoped to the blogs table only. Unrelated project-wide schema drift that
// doctrine:diff also emitted (messenger_messages, date_overrides, …) is
// deliberately left out — it belongs to other migrations, not this feature.
$this->addSql('ALTER TABLE blogs ADD sources JSON NOT NULL, ADD review_status VARCHAR(20) DEFAULT NULL, ADD reviewed_at INT DEFAULT NULL, ADD review_note VARCHAR(500) DEFAULT NULL, ADD topic_slug VARCHAR(255) DEFAULT NULL, ADD reviewer_id INT DEFAULT NULL');
// Nullable columns first (safe on a table with existing rows).
$this->addSql('ALTER TABLE blogs ADD review_status VARCHAR(20) DEFAULT NULL, ADD reviewed_at INT DEFAULT NULL, ADD review_note VARCHAR(500) DEFAULT NULL, ADD topic_slug VARCHAR(255) DEFAULT NULL, ADD reviewer_id INT DEFAULT NULL');
// `sources` is JSON NOT NULL: adding it directly to a table that already
// has rows fails the json_valid CHECK (empty backfill is invalid JSON).
// Add nullable, backfill '[]', then enforce NOT NULL.
$this->addSql('ALTER TABLE blogs ADD sources JSON DEFAULT NULL');
$this->addSql("UPDATE blogs SET sources = '[]'");
$this->addSql('ALTER TABLE blogs MODIFY sources JSON NOT NULL');
$this->addSql('ALTER TABLE blogs ADD CONSTRAINT FK_F41BCA7070574616 FOREIGN KEY (reviewer_id) REFERENCES users (id) ON DELETE SET NULL');
$this->addSql('CREATE UNIQUE INDEX UNIQ_F41BCA70A076FAD7 ON blogs (topic_slug)');
$this->addSql('CREATE INDEX IDX_F41BCA7070574616 ON blogs (reviewer_id)');