app/DoctrineMigrations/Version20250917130000.php line 1

Open in your IDE?
  1. <?php
    
    declare(strict_types=1);
    
    namespace DoctrineMigrations;
    
    use Doctrine\DBAL\Schema\Schema;
    use Doctrine\Migrations\AbstractMigration;
    
    /**
     * 商品マスタに男女別商品の絞り込み機能を追加するためのマイグレーション
     * dtb_product_schoolテーブルにgender_typeフィールドを追加
     */
    final class Version20250917130000 extends AbstractMigration
    {
        public function getDescription(): string
        {
            return 'dtb_product_schoolテーブルにgender_type(smallint)を追加 - 男女別商品の絞り込み機能用';
        }
    
        public function up(Schema $schema): void
        {
            // gender_typeカラムが存在するかチェック
            $schemaManager = $this->connection->createSchemaManager();
            
            // テーブルが存在するかチェック
            if (!$schemaManager->tablesExist(['dtb_product_school'])) {
                // テーブルが存在しない場合はスキップ
                return;
            }
            
            $columns = $schemaManager->listTableColumns('dtb_product_school');
            
            $hasGenderType = false;
            
            foreach ($columns as $column) {
                if ($column->getName() === 'gender_type') {
                    $hasGenderType = true;
                    break;
                }
            }
            
            // gender_typeカラムが存在しない場合のみ追加
            if (!$hasGenderType) {
                $this->addSql('ALTER TABLE dtb_product_school ADD gender_type SMALLINT DEFAULT NULL');
            }
        }
    
        public function down(Schema $schema): void
        {
            // rollback処理 - カラムが存在する場合のみ削除
            $schemaManager = $this->connection->createSchemaManager();
            
            // テーブルが存在するかチェック
            if (!$schemaManager->tablesExist(['dtb_product_school'])) {
                return;
            }
            
            $columns = $schemaManager->listTableColumns('dtb_product_school');
            
            $hasGenderType = false;
            
            foreach ($columns as $column) {
                if ($column->getName() === 'gender_type') {
                    $hasGenderType = true;
                    break;
                }
            }
            
            if ($hasGenderType) {
                $this->addSql('ALTER TABLE dtb_product_school DROP gender_type');
            }
        }
    }