app/DoctrineMigrations/Version20251121120000.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 Version20251121120000 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return '商品テーブル(dtb_product)に購入点数制限(purchase_limit)カラムを追加';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // MySQL
  18.         if ($this->connection->getDatabasePlatform()->getName() === 'mysql') {
  19.             // テーブルが存在するか確認
  20.             $tableExists $this->connection->fetchOne("
  21.                 SELECT COUNT(*) 
  22.                 FROM information_schema.TABLES 
  23.                 WHERE TABLE_SCHEMA = DATABASE() 
  24.                 AND TABLE_NAME = 'dtb_product'
  25.             ");
  26.             
  27.             if ($tableExists) {
  28.                 // カラムが存在しない場合のみ追加
  29.                 $columnExists $this->connection->fetchOne("
  30.                     SELECT COUNT(*) 
  31.                     FROM information_schema.COLUMNS 
  32.                     WHERE TABLE_SCHEMA = DATABASE() 
  33.                     AND TABLE_NAME = 'dtb_product' 
  34.                     AND COLUMN_NAME = 'purchase_limit'
  35.                 ");
  36.                 
  37.                 if (!$columnExists) {
  38.                     $this->addSql('ALTER TABLE dtb_product ADD purchase_limit INT UNSIGNED DEFAULT NULL COMMENT \'購入点数制限(NULL or 0 = 制限なし)\'');
  39.                 }
  40.             }
  41.         }
  42.         // PostgreSQL
  43.         else if ($this->connection->getDatabasePlatform()->getName() === 'postgresql') {
  44.             // テーブルが存在するか確認
  45.             $tableExists $this->connection->fetchOne("
  46.                 SELECT COUNT(*) 
  47.                 FROM information_schema.tables 
  48.                 WHERE table_schema = 'public' 
  49.                 AND table_name = 'dtb_product'
  50.             ");
  51.             
  52.             if ($tableExists) {
  53.                 // カラムが存在しない場合のみ追加
  54.                 $columnExists $this->connection->fetchOne("
  55.                     SELECT COUNT(*) 
  56.                     FROM information_schema.columns 
  57.                     WHERE table_schema = 'public' 
  58.                     AND table_name = 'dtb_product' 
  59.                     AND column_name = 'purchase_limit'
  60.                 ");
  61.                 
  62.                 if (!$columnExists) {
  63.                     $this->addSql('ALTER TABLE dtb_product ADD purchase_limit INT DEFAULT NULL');
  64.                     $this->addSql('COMMENT ON COLUMN dtb_product.purchase_limit IS \'購入点数制限(NULL or 0 = 制限なし)\'');
  65.                 }
  66.             }
  67.         }
  68.     }
  69.     public function down(Schema $schema): void
  70.     {
  71.         // MySQL
  72.         if ($this->connection->getDatabasePlatform()->getName() === 'mysql') {
  73.             $columnExists $this->connection->fetchOne("
  74.                 SELECT COUNT(*) 
  75.                 FROM information_schema.COLUMNS 
  76.                 WHERE TABLE_SCHEMA = DATABASE() 
  77.                 AND TABLE_NAME = 'dtb_product' 
  78.                 AND COLUMN_NAME = 'purchase_limit'
  79.             ");
  80.             
  81.             if ($columnExists) {
  82.                 $this->addSql('ALTER TABLE dtb_product DROP COLUMN purchase_limit');
  83.             }
  84.         }
  85.         // PostgreSQL
  86.         else if ($this->connection->getDatabasePlatform()->getName() === 'postgresql') {
  87.             $columnExists $this->connection->fetchOne("
  88.                 SELECT COUNT(*) 
  89.                 FROM information_schema.columns 
  90.                 WHERE table_schema = 'public' 
  91.                 AND table_name = 'dtb_product' 
  92.                 AND column_name = 'purchase_limit'
  93.             ");
  94.             
  95.             if ($columnExists) {
  96.                 $this->addSql('ALTER TABLE dtb_product DROP COLUMN purchase_limit');
  97.             }
  98.         }
  99.     }
  100. }