app/DoctrineMigrations/Version20260526000000.php line 1

Open in your IDE?
  1. <?php
    
    declare(strict_types=1);
    
    namespace DoctrineMigrations;
    
    use Doctrine\DBAL\Schema\Schema;
    use Doctrine\Migrations\AbstractMigration;
    
    /**
     * Fix dtb_order sex_id NULL values by copying from dtb_customer
     *
     * This migration handles the case where dtb_order.sex_id is NULL
     * by copying the valid sex_id from dtb_customer.
     *
     * Rules:
     * - If customer.sex_id is NULL or 4 (select_unselected), order.sex_id becomes NULL
     * - Otherwise, copy customer.sex_id to order.sex_id
     */
    final class Version20260526000000 extends AbstractMigration
    {
        public function getDescription(): string
        {
            return 'Fix dtb_order sex_id NULL values by copying from dtb_customer';
        }
    
        public function up(Schema $schema): void
        {
            // Update orders where sex_id is NULL but customer has valid sex
            $this->addSql('UPDATE dtb_order o 
                INNER JOIN dtb_customer c ON o.customer_id = c.id 
                SET o.sex_id = c.sex_id 
                WHERE o.sex_id IS NULL 
                AND c.sex_id IS NOT NULL 
                AND c.sex_id != 4');
    
            // Ensure orders with customer sex_id = 4 (select_unselected) or NULL have NULL sex_id
            $this->addSql('UPDATE dtb_order o 
                INNER JOIN dtb_customer c ON o.customer_id = c.id 
                SET o.sex_id = NULL 
                WHERE (c.sex_id IS NULL OR c.sex_id = 4) 
                AND o.sex_id IS NOT NULL');
        }
    
        public function down(Schema $schema): void
        {
            // This migration cannot be safely reverted as we cannot determine
            // what the original values were before the migration
            throw new \Exception('This migration cannot be reverted as it modifies existing data based on customer information.');
        }
    }