app/DoctrineMigrations/Version20260202000000.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.  */
  9. final class Version20260202000000 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return '返品・取り消し履歴テーブル(dtb_order_item_return_history)を作成';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // テーブルの存在確認
  18.         $tableExists $schema->hasTable('dtb_order_item_return_history');
  19.         
  20.         if (!$tableExists) {
  21.             // dtb_order_item_return_history テーブルを作成
  22.             $this->addSql("
  23.                 CREATE TABLE dtb_order_item_return_history (
  24.                     id INT UNSIGNED AUTO_INCREMENT NOT NULL,
  25.                     order_item_id INT UNSIGNED DEFAULT NULL,
  26.                     order_id INT UNSIGNED DEFAULT NULL,
  27.                     return_date DATETIME DEFAULT NULL COMMENT '(DC2Type:datetimetz)',
  28.                     product_name VARCHAR(255) DEFAULT NULL,
  29.                     return_amount DECIMAL(12, 2) DEFAULT NULL,
  30.                     return_quantity INT DEFAULT NULL,
  31.                     return_method SMALLINT DEFAULT NULL COMMENT '1:現金手渡し, 2:授業料引き落とし口座に振り込み',
  32.                     remaining_quantity INT DEFAULT NULL,
  33.                     remaining_total DECIMAL(12, 2) DEFAULT NULL,
  34.                     create_date DATETIME NOT NULL COMMENT '(DC2Type:datetimetz)',
  35.                     update_date DATETIME NOT NULL COMMENT '(DC2Type:datetimetz)',
  36.                     INDEX IDX_ORDER_ITEM_RETURN_HISTORY_ORDER_ITEM_ID (order_item_id),
  37.                     INDEX IDX_ORDER_ITEM_RETURN_HISTORY_ORDER_ID (order_id),
  38.                     PRIMARY KEY(id)
  39.                 ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ENGINE = InnoDB
  40.             ");
  41.         }
  42.         // 外部キー制約の存在確認と追加
  43.         if ($tableExists) {
  44.             $table $schema->getTable('dtb_order_item_return_history');
  45.             
  46.             // order_item_id の外部キー制約
  47.             if (!$table->hasForeignKey('FK_ORDER_ITEM_RETURN_HISTORY_ORDER_ITEM')) {
  48.                 $this->addSql("
  49.                     ALTER TABLE dtb_order_item_return_history 
  50.                     ADD CONSTRAINT FK_ORDER_ITEM_RETURN_HISTORY_ORDER_ITEM 
  51.                     FOREIGN KEY (order_item_id) REFERENCES dtb_order_item (id) ON DELETE SET NULL
  52.                 ");
  53.             }
  54.             
  55.             // order_id の外部キー制約
  56.             if (!$table->hasForeignKey('FK_ORDER_ITEM_RETURN_HISTORY_ORDER')) {
  57.                 $this->addSql("
  58.                     ALTER TABLE dtb_order_item_return_history 
  59.                     ADD CONSTRAINT FK_ORDER_ITEM_RETURN_HISTORY_ORDER 
  60.                     FOREIGN KEY (order_id) REFERENCES dtb_order (id) ON DELETE SET NULL
  61.                 ");
  62.             }
  63.         } else {
  64.             // テーブルを新規作成した場合は外部キー制約も追加
  65.             $this->addSql("
  66.                 ALTER TABLE dtb_order_item_return_history 
  67.                 ADD CONSTRAINT FK_ORDER_ITEM_RETURN_HISTORY_ORDER_ITEM 
  68.                 FOREIGN KEY (order_item_id) REFERENCES dtb_order_item (id) ON DELETE SET NULL
  69.             ");
  70.             $this->addSql("
  71.                 ALTER TABLE dtb_order_item_return_history 
  72.                 ADD CONSTRAINT FK_ORDER_ITEM_RETURN_HISTORY_ORDER 
  73.                 FOREIGN KEY (order_id) REFERENCES dtb_order (id) ON DELETE SET NULL
  74.             ");
  75.         }
  76.     }
  77.     public function down(Schema $schema): void
  78.     {
  79.         // テーブルの存在確認
  80.         if (!$schema->hasTable('dtb_order_item_return_history')) {
  81.             return;
  82.         }
  83.         $table $schema->getTable('dtb_order_item_return_history');
  84.         // 外部キー制約の存在確認と削除
  85.         if ($table->hasForeignKey('FK_ORDER_ITEM_RETURN_HISTORY_ORDER_ITEM')) {
  86.             $this->addSql("ALTER TABLE dtb_order_item_return_history DROP FOREIGN KEY FK_ORDER_ITEM_RETURN_HISTORY_ORDER_ITEM");
  87.         }
  88.         if ($table->hasForeignKey('FK_ORDER_ITEM_RETURN_HISTORY_ORDER')) {
  89.             $this->addSql("ALTER TABLE dtb_order_item_return_history DROP FOREIGN KEY FK_ORDER_ITEM_RETURN_HISTORY_ORDER");
  90.         }
  91.         // テーブルを削除
  92.         $this->addSql("DROP TABLE dtb_order_item_return_history");
  93.     }
  94. }