migrations/Version20251022120000.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. /**
  7.  * Fügt 'active' Flag zur Person-Tabelle hinzu für Soft-Delete Funktionalität
  8.  */
  9. final class Version20251022120000 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Adds active flag to person table for soft-delete functionality';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // Prüfe, ob Spalte bereits existiert
  18.         $this->addSql("
  19.             SET @col_exists = (
  20.                 SELECT COUNT(*) 
  21.                 FROM INFORMATION_SCHEMA.COLUMNS 
  22.                 WHERE TABLE_SCHEMA = DATABASE() 
  23.                 AND TABLE_NAME = 'person' 
  24.                 AND COLUMN_NAME = 'active'
  25.             );
  26.         ");
  27.         
  28.         // Füge Spalte nur hinzu, wenn sie noch nicht existiert
  29.         $this->addSql("
  30.             SET @sql = IF(@col_exists = 0, 
  31.                 'ALTER TABLE person ADD active TINYINT(1) DEFAULT 1 NULL AFTER is_customer', 
  32.                 'SELECT 1'
  33.             );
  34.             PREPARE stmt FROM @sql;
  35.             EXECUTE stmt;
  36.             DEALLOCATE PREPARE stmt;
  37.         ");
  38.         
  39.         // Setze alle bestehenden Personen auf active = 1
  40.         $this->addSql("UPDATE person SET active = 1 WHERE active IS NULL");
  41.     }
  42.     public function down(Schema $schema): void
  43.     {
  44.         // Entferne Spalte nur, wenn sie existiert
  45.         $this->addSql("
  46.             SET @col_exists = (
  47.                 SELECT COUNT(*) 
  48.                 FROM INFORMATION_SCHEMA.COLUMNS 
  49.                 WHERE TABLE_SCHEMA = DATABASE() 
  50.                 AND TABLE_NAME = 'person' 
  51.                 AND COLUMN_NAME = 'active'
  52.             );
  53.         ");
  54.         
  55.         $this->addSql("
  56.             SET @sql = IF(@col_exists > 0, 
  57.                 'ALTER TABLE person DROP COLUMN active', 
  58.                 'SELECT 1'
  59.             );
  60.             PREPARE stmt FROM @sql;
  61.             EXECUTE stmt;
  62.             DEALLOCATE PREPARE stmt;
  63.         ");
  64.     }
  65. }