app/DoctrineMigrations/Version20251225120000.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.  * dtb_order_register_itemテーブルに入庫数カラムを追加
  8.  */
  9. final class Version20251225120000 extends AbstractMigration
  10. {
  11.     public function up(Schema $schema): void
  12.     {
  13.         // dtb_order_register_itemテーブルの存在確認
  14.         if (!$schema->hasTable('dtb_order_register_item')) {
  15.             $this->write('dtb_order_register_itemテーブルが存在しません。スキップします。');
  16.             return;
  17.         }
  18.         $table $schema->getTable('dtb_order_register_item');
  19.         
  20.         // stock_in_quantityカラムが既に存在する場合はスキップ
  21.         if ($table->hasColumn('stock_in_quantity')) {
  22.             $this->write('stock_in_quantityカラムは既に存在します。スキップします。');
  23.             return;
  24.         }
  25.         // dtb_order_register_itemテーブルに入庫数カラムを追加
  26.         $this->addSql("
  27.             ALTER TABLE dtb_order_register_item
  28.             ADD COLUMN stock_in_quantity INT NOT NULL DEFAULT 0 COMMENT '入庫数' AFTER quantity
  29.         ");
  30.         
  31.         $this->write('dtb_order_register_itemテーブルに入庫数カラムを追加しました');
  32.     }
  33.     public function down(Schema $schema): void
  34.     {
  35.         // dtb_order_register_itemテーブルの存在確認
  36.         if (!$schema->hasTable('dtb_order_register_item')) {
  37.             $this->write('dtb_order_register_itemテーブルが存在しません。スキップします。');
  38.             return;
  39.         }
  40.         $table $schema->getTable('dtb_order_register_item');
  41.         
  42.         // stock_in_quantityカラムが存在しない場合はスキップ
  43.         if (!$table->hasColumn('stock_in_quantity')) {
  44.             $this->write('stock_in_quantityカラムが存在しません。スキップします。');
  45.             return;
  46.         }
  47.         // ロールバック: 入庫数カラムを削除
  48.         $this->addSql("
  49.             ALTER TABLE dtb_order_register_item
  50.             DROP COLUMN stock_in_quantity
  51.         ");
  52.         
  53.         $this->write('dtb_order_register_itemテーブルから入庫数カラムを削除しました');
  54.     }
  55. }