app/DoctrineMigrations/Version20251009000000.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.  * Auto-generated Migration: Please modify to your needs!
  8.  * 商品テーブルにメーカー取り寄せフラグを追加するためのマイグレーション
  9.  * dtb_productテーブルにmaker_order_flgフィールドを追加
  10.  */
  11. final class Version20251009000000 extends AbstractMigration
  12. {
  13.     public function getDescription(): string
  14.     {
  15.         return 'dtb_productテーブルにmaker_order_flg(boolean)を追加 - 規格なし商品用メーカー取り寄せフラグ';
  16.     }
  17.     public function up(Schema $schema): void
  18.     {
  19.         // this up() migration is auto-generated, please modify it to your needs
  20.         
  21.         $schemaManager $this->connection->getSchemaManager();
  22.         
  23.         // テーブルが存在するかチェック
  24.         if (!$schemaManager->tablesExist(['dtb_product'])) {
  25.             return;
  26.         }
  27.         
  28.         // カラムが存在するかチェック
  29.         $columns $schemaManager->listTableColumns('dtb_product');
  30.         
  31.         // PostgreSQLの場合、カラム名は小文字になる場合があるので、
  32.         // 大文字小文字を区別しないでチェック
  33.         $columnExists false;
  34.         foreach ($columns as $columnName => $column) {
  35.             if (strtolower($columnName) === 'maker_order_flg') {
  36.                 $columnExists true;
  37.                 break;
  38.             }
  39.         }
  40.         
  41.         // カラムが存在しない場合のみ追加
  42.         if (!$columnExists) {
  43.             $this->addSql('ALTER TABLE dtb_product ADD maker_order_flg BOOLEAN DEFAULT FALSE NOT NULL');
  44.         }
  45.     }
  46.     public function down(Schema $schema): void
  47.     {
  48.         // this down() migration is auto-generated, please modify it to your needs
  49.         
  50.         $schemaManager $this->connection->getSchemaManager();
  51.         
  52.         // テーブルが存在し、カラムが存在する場合のみ削除
  53.         if ($schemaManager->tablesExist(['dtb_product'])) {
  54.             $columns $schemaManager->listTableColumns('dtb_product');
  55.             
  56.             $columnExists false;
  57.             foreach ($columns as $columnName => $column) {
  58.                 if (strtolower($columnName) === 'maker_order_flg') {
  59.                     $columnExists true;
  60.                     break;
  61.                 }
  62.             }
  63.             
  64.             if ($columnExists) {
  65.                 $this->addSql('ALTER TABLE dtb_product DROP COLUMN maker_order_flg');
  66.             }
  67.         }
  68.     }
  69. }