app/DoctrineMigrations/Version20251126100001.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 Version20251126100001 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'dtb_schoolテーブルに商品詳細ページ用の注意書きフィールドを追加';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // MySQLの場合のみ実行
  18.         if ($this->connection->getDatabasePlatform()->getName() !== 'mysql') {
  19.             return;
  20.         }
  21.         // display_product_notice カラムが存在しない場合のみ追加
  22.         $displayProductNoticeExists $this->connection->fetchOne("
  23.             SELECT COUNT(*) 
  24.             FROM information_schema.COLUMNS 
  25.             WHERE TABLE_SCHEMA = DATABASE() 
  26.             AND TABLE_NAME = 'dtb_school' 
  27.             AND COLUMN_NAME = 'display_product_notice'
  28.         ");
  29.         if (!$displayProductNoticeExists) {
  30.             $this->addSql("
  31.                 ALTER TABLE dtb_school 
  32.                 ADD display_product_notice BOOLEAN DEFAULT 0 NOT NULL 
  33.                 COMMENT '商品詳細ページに注意書きを表示するか'
  34.             ");
  35.         }
  36.         // product_notice_text カラムが存在しない場合のみ追加
  37.         $productNoticeTextExists $this->connection->fetchOne("
  38.             SELECT COUNT(*) 
  39.             FROM information_schema.COLUMNS 
  40.             WHERE TABLE_SCHEMA = DATABASE() 
  41.             AND TABLE_NAME = 'dtb_school' 
  42.             AND COLUMN_NAME = 'product_notice_text'
  43.         ");
  44.         if (!$productNoticeTextExists) {
  45.             $this->addSql("
  46.                 ALTER TABLE dtb_school 
  47.                 ADD product_notice_text TEXT DEFAULT NULL 
  48.                 COMMENT '商品詳細ページに表示する注意書きテキスト'
  49.             ");
  50.         }
  51.     }
  52.     public function down(Schema $schema): void
  53.     {
  54.         // MySQLの場合のみ実行
  55.         if ($this->connection->getDatabasePlatform()->getName() !== 'mysql') {
  56.             return;
  57.         }
  58.         // カラムを削除
  59.         $this->addSql('ALTER TABLE dtb_school DROP COLUMN IF EXISTS display_product_notice');
  60.         $this->addSql('ALTER TABLE dtb_school DROP COLUMN IF EXISTS product_notice_text');
  61.     }
  62. }