migrations/Version20260221130000.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. final class Version20260221130000 extends AbstractMigration
  7. {
  8.     public function getDescription(): string
  9.     {
  10.         return 'Add slug column to task table and backfill from existing titles';
  11.     }
  12.     public function up(Schema $schema): void
  13.     {
  14.         $this->addSql('ALTER TABLE task ADD slug VARCHAR(255) DEFAULT NULL');
  15.         // Backfill: generate a basic slug from the existing title for all rows.
  16.         // The PHP-side slugify does transliteration; here we do a simpler SQL version
  17.         // which is good enough for seeding — the app will overwrite on next save.
  18.         $this->addSql("
  19.             UPDATE task
  20.             SET slug = LOWER(
  21.                 TRIM(BOTH '-' FROM
  22.                     REGEXP_REPLACE(
  23.                         CONVERT(title USING ascii),
  24.                         '[^a-zA-Z0-9]+',
  25.                         '-'
  26.                     )
  27.                 )
  28.             )
  29.             WHERE slug IS NULL AND title IS NOT NULL AND title != ''
  30.         ");
  31.     }
  32.     public function down(Schema $schema): void
  33.     {
  34.         $this->addSql('ALTER TABLE task DROP slug');
  35.     }
  36. }