feat(migration): update blog schema to add new fields and improve migration handling

This commit is contained in:
hamed
2026-07-23 23:13:15 +03:30
parent e7c2c2f06e
commit e766407bd1
3 changed files with 45 additions and 18 deletions
+20 -8
View File
@@ -17,23 +17,35 @@ final class Version20260723171254 extends AbstractMigration
return 'Add medical-review gate fields to blogs (sources, review_status, reviewer, reviewed_at, review_note, topic_slug)';
}
// MariaDB auto-commits on DDL, so wrapping this migration in a transaction only
// creates savepoints that the first ALTER destroys ("SAVEPOINT DOCTRINE_2 does
// not exist"). Run it outside a transaction; the statements are idempotent.
public function isTransactional(): bool
{
return false;
}
public function up(Schema $schema): void
{
// 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.
// Idempotent (MariaDB IF [NOT] EXISTS): a prior failed --all-or-nothing run
// can leave columns applied while the version record was rolled back, so a
// re-run must heal a partial state instead of erroring on "duplicate column".
// 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).
$this->addSql('ALTER TABLE blogs ADD COLUMN IF NOT EXISTS review_status VARCHAR(20) DEFAULT NULL, ADD COLUMN IF NOT EXISTS reviewed_at INT DEFAULT NULL, ADD COLUMN IF NOT EXISTS review_note VARCHAR(500) DEFAULT NULL, ADD COLUMN IF NOT EXISTS topic_slug VARCHAR(255) DEFAULT NULL, ADD COLUMN IF NOT EXISTS 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 ADD COLUMN IF NOT EXISTS sources JSON DEFAULT NULL');
$this->addSql("UPDATE blogs SET sources = '[]' WHERE sources IS NULL");
$this->addSql('ALTER TABLE blogs MODIFY sources JSON NOT NULL');
$this->addSql('ALTER TABLE blogs DROP FOREIGN KEY IF EXISTS FK_F41BCA7070574616');
$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)');
$this->addSql('CREATE INDEX idx_blogs_review_status ON blogs (review_status, created_at)');
$this->addSql('CREATE UNIQUE INDEX IF NOT EXISTS UNIQ_F41BCA70A076FAD7 ON blogs (topic_slug)');
$this->addSql('CREATE INDEX IF NOT EXISTS IDX_F41BCA7070574616 ON blogs (reviewer_id)');
$this->addSql('CREATE INDEX IF NOT EXISTS idx_blogs_review_status ON blogs (review_status, created_at)');
}
public function down(Schema $schema): void