app/DoctrineMigrations/Version20250917130000.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.  * 商品マスタに男女別商品の絞り込み機能を追加するためのマイグレーション
  8.  * dtb_product_schoolテーブルにgender_typeフィールドを追加
  9.  */
  10. final class Version20250917130000 extends AbstractMigration
  11. {
  12.     public function getDescription(): string
  13.     {
  14.         return 'dtb_product_schoolテーブルにgender_type(smallint)を追加 - 男女別商品の絞り込み機能用';
  15.     }
  16.     public function up(Schema $schema): void
  17.     {
  18.         // gender_typeカラムが存在するかチェック
  19.         $schemaManager $this->connection->createSchemaManager();
  20.         
  21.         // テーブルが存在するかチェック
  22.         if (!$schemaManager->tablesExist(['dtb_product_school'])) {
  23.             // テーブルが存在しない場合はスキップ
  24.             return;
  25.         }
  26.         
  27.         $columns $schemaManager->listTableColumns('dtb_product_school');
  28.         
  29.         $hasGenderType false;
  30.         
  31.         foreach ($columns as $column) {
  32.             if ($column->getName() === 'gender_type') {
  33.                 $hasGenderType true;
  34.                 break;
  35.             }
  36.         }
  37.         
  38.         // gender_typeカラムが存在しない場合のみ追加
  39.         if (!$hasGenderType) {
  40.             $this->addSql('ALTER TABLE dtb_product_school ADD gender_type SMALLINT DEFAULT NULL');
  41.         }
  42.     }
  43.     public function down(Schema $schema): void
  44.     {
  45.         // rollback処理 - カラムが存在する場合のみ削除
  46.         $schemaManager $this->connection->createSchemaManager();
  47.         
  48.         // テーブルが存在するかチェック
  49.         if (!$schemaManager->tablesExist(['dtb_product_school'])) {
  50.             return;
  51.         }
  52.         
  53.         $columns $schemaManager->listTableColumns('dtb_product_school');
  54.         
  55.         $hasGenderType false;
  56.         
  57.         foreach ($columns as $column) {
  58.             if ($column->getName() === 'gender_type') {
  59.                 $hasGenderType true;
  60.                 break;
  61.             }
  62.         }
  63.         
  64.         if ($hasGenderType) {
  65.             $this->addSql('ALTER TABLE dtb_product_school DROP gender_type');
  66.         }
  67.     }
  68. }