migrations/Version20241105000000.php line 1

Open in your IDE?
  1. <?php
    
    declare(strict_types=1);
    
    namespace DoctrineMigrations;
    
    use Doctrine\DBAL\Schema\Schema;
    use Doctrine\Migrations\AbstractMigration;
    
    final class Version20241105000000 extends AbstractMigration
    {
        public function getDescription(): string
        {
            return 'Add advanced targeting and event fields to candidate_communication';
        }
    
        /**
         * Petit helper : vérifier si une colonne existe déjà dans une table
         */
        private function columnExists(string $table, string $column): bool
        {
            $schemaManager = $this->connection->createSchemaManager(); // DBAL 3.x
            $columns       = $schemaManager->listTableColumns($table);
    
            return \array_key_exists($column, $columns);
        }
    
        public function up(Schema $schema): void
        {
            $table = 'candidate_communication';
    
            // level : on NE TOUCHE PAS, il existe déjà dans ton schéma
            // event_type
            if (!$this->columnExists($table, 'event_type')) {
                $this->addSql("
                    ALTER TABLE {$table}
                    ADD event_type VARCHAR(50) DEFAULT NULL
                ");
            }
    
            // event_label
            if (!$this->columnExists($table, 'event_label')) {
                $this->addSql("
                    ALTER TABLE {$table}
                    ADD event_label VARCHAR(255) DEFAULT NULL
                ");
            }
    
            // event_deadline_at
            if (!$this->columnExists($table, 'event_deadline_at')) {
                $this->addSql("
                    ALTER TABLE {$table}
                    ADD event_deadline_at DATETIME DEFAULT NULL
                        COMMENT '(DC2Type:datetime_immutable)'
                ");
            }
    
            // targeting_criteria
            if (!$this->columnExists($table, 'targeting_criteria')) {
                $this->addSql("
                    ALTER TABLE {$table}
                    ADD targeting_criteria JSON DEFAULT NULL
                ");
            }
    
            // status
            if (!$this->columnExists($table, 'status')) {
                $this->addSql("
                    ALTER TABLE {$table}
                    ADD status VARCHAR(20) NOT NULL DEFAULT 'active'
                ");
            }
    
            // updated_at
            if (!$this->columnExists($table, 'updated_at')) {
                $this->addSql("
                    ALTER TABLE {$table}
                    ADD updated_at DATETIME DEFAULT NULL
                        COMMENT '(DC2Type:datetime_immutable)'
                ");
            }
    
            // Mise à jour du statut **uniquement si la colonne status existe**
            if ($this->columnExists($table, 'status')) {
                // et si scheduled_at existe aussi, le CASE fonctionnera
                $this->addSql("
                    UPDATE {$table}
                    SET status = CASE
                        WHEN scheduled_at IS NOT NULL THEN 'scheduled'
                        ELSE 'active'
                    END
                ");
            }
        }
    
        public function down(Schema $schema): void
        {
            $table = 'candidate_communication';
    
            if ($this->columnExists($table, 'event_type')) {
                $this->addSql("ALTER TABLE {$table} DROP event_type");
            }
    
            if ($this->columnExists($table, 'event_label')) {
                $this->addSql("ALTER TABLE {$table} DROP event_label");
            }
    
            if ($this->columnExists($table, 'event_deadline_at')) {
                $this->addSql("ALTER TABLE {$table} DROP event_deadline_at");
            }
    
            if ($this->columnExists($table, 'targeting_criteria')) {
                $this->addSql("ALTER TABLE {$table} DROP targeting_criteria");
            }
    
            if ($this->columnExists($table, 'status')) {
                $this->addSql("ALTER TABLE {$table} DROP status");
            }
    
            if ($this->columnExists($table, 'updated_at')) {
                $this->addSql("ALTER TABLE {$table} DROP updated_at");
            }
    
            // On ne touche toujours pas à "level"
        }
    }