app/DoctrineMigrations/Version20250918140000.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テーブルにship_free_flgとship_free_textフィールドを追加
  9.  */
  10. final class Version20250918140000 extends AbstractMigration
  11. {
  12.     public function getDescription(): string
  13.     {
  14.         return 'dtb_productテーブルにship_free_flg(smallint)とship_free_text(varchar(512))を追加 - 商品ごとの送料無料設定用';
  15.     }
  16.     public function up(Schema $schema): void
  17.     {
  18.         // カラムが存在するかチェック
  19.         $schemaManager $this->connection->createSchemaManager();
  20.         
  21.         // テーブルが存在するかチェック
  22.         if (!$schemaManager->tablesExist(['dtb_product'])) {
  23.             // テーブルが存在しない場合はスキップ
  24.             return;
  25.         }
  26.         
  27.         $columns $schemaManager->listTableColumns('dtb_product');
  28.         
  29.         $hasShipFreeFlg false;
  30.         $hasShipFreeText false;
  31.         
  32.         foreach ($columns as $column) {
  33.             if ($column->getName() === 'ship_free_flg') {
  34.                 $hasShipFreeFlg true;
  35.             }
  36.             if ($column->getName() === 'ship_free_text') {
  37.                 $hasShipFreeText true;
  38.             }
  39.         }
  40.         
  41.         // ship_free_flgカラムが存在しない場合のみ追加
  42.         if (!$hasShipFreeFlg) {
  43.             $this->addSql('ALTER TABLE dtb_product ADD ship_free_flg SMALLINT DEFAULT 0 NOT NULL');
  44.         }
  45.         
  46.         // ship_free_textカラムが存在しない場合のみ追加
  47.         if (!$hasShipFreeText) {
  48.             $this->addSql('ALTER TABLE dtb_product ADD ship_free_text VARCHAR(512) DEFAULT NULL');
  49.         }
  50.     }
  51.     public function down(Schema $schema): void
  52.     {
  53.         $schemaManager $this->connection->createSchemaManager();
  54.         
  55.         if (!$schemaManager->tablesExist(['dtb_product'])) {
  56.             return;
  57.         }
  58.         
  59.         $columns $schemaManager->listTableColumns('dtb_product');
  60.         $columnNames array_map(function($column) {
  61.             return $column->getName();
  62.         }, $columns);
  63.         
  64.         // ship_free_flgカラムを削除
  65.         if (in_array('ship_free_flg'$columnNames)) {
  66.             $this->addSql('ALTER TABLE dtb_product DROP COLUMN ship_free_flg');
  67.         }
  68.         
  69.         // ship_free_textカラムを削除
  70.         if (in_array('ship_free_text'$columnNames)) {
  71.             $this->addSql('ALTER TABLE dtb_product DROP COLUMN ship_free_text');
  72.         }
  73.     }
  74. }