app/DoctrineMigrations/Version20251009000000.php line 1

Open in your IDE?
  1. <?php
    
    declare(strict_types=1);
    
    namespace DoctrineMigrations;
    
    use Doctrine\DBAL\Schema\Schema;
    use Doctrine\Migrations\AbstractMigration;
    
    /**
     * Auto-generated Migration: Please modify to your needs!
     * 商品テーブルにメーカー取り寄せフラグを追加するためのマイグレーション
     * dtb_productテーブルにmaker_order_flgフィールドを追加
     */
    final class Version20251009000000 extends AbstractMigration
    {
        public function getDescription(): string
        {
            return 'dtb_productテーブルにmaker_order_flg(boolean)を追加 - 規格なし商品用メーカー取り寄せフラグ';
        }
    
        public function up(Schema $schema): void
        {
            // this up() migration is auto-generated, please modify it to your needs
            
            $schemaManager = $this->connection->getSchemaManager();
            
            // テーブルが存在するかチェック
            if (!$schemaManager->tablesExist(['dtb_product'])) {
                return;
            }
            
            // カラムが存在するかチェック
            $columns = $schemaManager->listTableColumns('dtb_product');
            
            // PostgreSQLの場合、カラム名は小文字になる場合があるので、
            // 大文字小文字を区別しないでチェック
            $columnExists = false;
            foreach ($columns as $columnName => $column) {
                if (strtolower($columnName) === 'maker_order_flg') {
                    $columnExists = true;
                    break;
                }
            }
            
            // カラムが存在しない場合のみ追加
            if (!$columnExists) {
                $this->addSql('ALTER TABLE dtb_product ADD maker_order_flg BOOLEAN DEFAULT FALSE NOT NULL');
            }
        }
    
        public function down(Schema $schema): void
        {
            // this down() migration is auto-generated, please modify it to your needs
            
            $schemaManager = $this->connection->getSchemaManager();
            
            // テーブルが存在し、カラムが存在する場合のみ削除
            if ($schemaManager->tablesExist(['dtb_product'])) {
                $columns = $schemaManager->listTableColumns('dtb_product');
                
                $columnExists = false;
                foreach ($columns as $columnName => $column) {
                    if (strtolower($columnName) === 'maker_order_flg') {
                        $columnExists = true;
                        break;
                    }
                }
                
                if ($columnExists) {
                    $this->addSql('ALTER TABLE dtb_product DROP COLUMN maker_order_flg');
                }
            }
        }
    }