app/DoctrineMigrations/Version20260202000000.php line 1

Open in your IDE?
  1. <?php
    
    declare(strict_types=1);
    
    namespace DoctrineMigrations;
    
    use Doctrine\DBAL\Schema\Schema;
    use Doctrine\Migrations\AbstractMigration;
    
    /**
     * 返品・取り消し履歴テーブルを作成
     */
    final class Version20260202000000 extends AbstractMigration
    {
        public function getDescription(): string
        {
            return '返品・取り消し履歴テーブル(dtb_order_item_return_history)を作成';
        }
    
        public function up(Schema $schema): void
        {
            // テーブルの存在確認
            $tableExists = $schema->hasTable('dtb_order_item_return_history');
            
            if (!$tableExists) {
                // dtb_order_item_return_history テーブルを作成
                $this->addSql("
                    CREATE TABLE dtb_order_item_return_history (
                        id INT UNSIGNED AUTO_INCREMENT NOT NULL,
                        order_item_id INT UNSIGNED DEFAULT NULL,
                        order_id INT UNSIGNED DEFAULT NULL,
                        return_date DATETIME DEFAULT NULL COMMENT '(DC2Type:datetimetz)',
                        product_name VARCHAR(255) DEFAULT NULL,
                        return_amount DECIMAL(12, 2) DEFAULT NULL,
                        return_quantity INT DEFAULT NULL,
                        return_method SMALLINT DEFAULT NULL COMMENT '1:現金手渡し, 2:授業料引き落とし口座に振り込み',
                        remaining_quantity INT DEFAULT NULL,
                        remaining_total DECIMAL(12, 2) DEFAULT NULL,
                        create_date DATETIME NOT NULL COMMENT '(DC2Type:datetimetz)',
                        update_date DATETIME NOT NULL COMMENT '(DC2Type:datetimetz)',
                        INDEX IDX_ORDER_ITEM_RETURN_HISTORY_ORDER_ITEM_ID (order_item_id),
                        INDEX IDX_ORDER_ITEM_RETURN_HISTORY_ORDER_ID (order_id),
                        PRIMARY KEY(id)
                    ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ENGINE = InnoDB
                ");
            }
    
            // 外部キー制約の存在確認と追加
            if ($tableExists) {
                $table = $schema->getTable('dtb_order_item_return_history');
                
                // order_item_id の外部キー制約
                if (!$table->hasForeignKey('FK_ORDER_ITEM_RETURN_HISTORY_ORDER_ITEM')) {
                    $this->addSql("
                        ALTER TABLE dtb_order_item_return_history 
                        ADD CONSTRAINT FK_ORDER_ITEM_RETURN_HISTORY_ORDER_ITEM 
                        FOREIGN KEY (order_item_id) REFERENCES dtb_order_item (id) ON DELETE SET NULL
                    ");
                }
                
                // order_id の外部キー制約
                if (!$table->hasForeignKey('FK_ORDER_ITEM_RETURN_HISTORY_ORDER')) {
                    $this->addSql("
                        ALTER TABLE dtb_order_item_return_history 
                        ADD CONSTRAINT FK_ORDER_ITEM_RETURN_HISTORY_ORDER 
                        FOREIGN KEY (order_id) REFERENCES dtb_order (id) ON DELETE SET NULL
                    ");
                }
            } else {
                // テーブルを新規作成した場合は外部キー制約も追加
                $this->addSql("
                    ALTER TABLE dtb_order_item_return_history 
                    ADD CONSTRAINT FK_ORDER_ITEM_RETURN_HISTORY_ORDER_ITEM 
                    FOREIGN KEY (order_item_id) REFERENCES dtb_order_item (id) ON DELETE SET NULL
                ");
    
                $this->addSql("
                    ALTER TABLE dtb_order_item_return_history 
                    ADD CONSTRAINT FK_ORDER_ITEM_RETURN_HISTORY_ORDER 
                    FOREIGN KEY (order_id) REFERENCES dtb_order (id) ON DELETE SET NULL
                ");
            }
        }
    
        public function down(Schema $schema): void
        {
            // テーブルの存在確認
            if (!$schema->hasTable('dtb_order_item_return_history')) {
                return;
            }
    
            $table = $schema->getTable('dtb_order_item_return_history');
    
            // 外部キー制約の存在確認と削除
            if ($table->hasForeignKey('FK_ORDER_ITEM_RETURN_HISTORY_ORDER_ITEM')) {
                $this->addSql("ALTER TABLE dtb_order_item_return_history DROP FOREIGN KEY FK_ORDER_ITEM_RETURN_HISTORY_ORDER_ITEM");
            }
    
            if ($table->hasForeignKey('FK_ORDER_ITEM_RETURN_HISTORY_ORDER')) {
                $this->addSql("ALTER TABLE dtb_order_item_return_history DROP FOREIGN KEY FK_ORDER_ITEM_RETURN_HISTORY_ORDER");
            }
    
            // テーブルを削除
            $this->addSql("DROP TABLE dtb_order_item_return_history");
        }
    }