app/DoctrineMigrations/Version20251020100000.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.  * dtb_schoolテーブルにreport_font_sizeフィールドを追加
  9.  */
  10. final class Version20251020100000 extends AbstractMigration
  11. {
  12.     public function getDescription(): string
  13.     {
  14.         return 'dtb_schoolテーブルにreport_font_size(smallint)を追加 - 購入明細書(納品書)のフォントサイズ設定機能用';
  15.     }
  16.     public function up(Schema $schema): void
  17.     {
  18.         // report_font_sizeカラムが存在するかチェック
  19.         $schemaManager $this->connection->createSchemaManager();
  20.         
  21.         // テーブルが存在するかチェック
  22.         if (!$schemaManager->tablesExist(['dtb_school'])) {
  23.             // テーブルが存在しない場合はスキップ
  24.             return;
  25.         }
  26.         
  27.         $columns $schemaManager->listTableColumns('dtb_school');
  28.         
  29.         $hasReportFontSize false;
  30.         
  31.         foreach ($columns as $column) {
  32.             if ($column->getName() === 'report_font_size') {
  33.                 $hasReportFontSize true;
  34.                 break;
  35.             }
  36.         }
  37.         
  38.         // report_font_sizeカラムが存在しない場合のみ追加
  39.         // デフォルト値は2(中サイズ)
  40.         if (!$hasReportFontSize) {
  41.             $this->addSql('ALTER TABLE dtb_school ADD report_font_size SMALLINT UNSIGNED NULL DEFAULT 2');
  42.         }
  43.     }
  44.     public function down(Schema $schema): void
  45.     {
  46.         // rollback処理 - カラムが存在する場合のみ削除
  47.         $schemaManager $this->connection->createSchemaManager();
  48.         
  49.         // テーブルが存在するかチェック
  50.         if (!$schemaManager->tablesExist(['dtb_school'])) {
  51.             return;
  52.         }
  53.         
  54.         $columns $schemaManager->listTableColumns('dtb_school');
  55.         
  56.         $hasReportFontSize false;
  57.         
  58.         foreach ($columns as $column) {
  59.             if ($column->getName() === 'report_font_size') {
  60.                 $hasReportFontSize true;
  61.                 break;
  62.             }
  63.         }
  64.         
  65.         if ($hasReportFontSize) {
  66.             $this->addSql('ALTER TABLE dtb_school DROP report_font_size');
  67.         }
  68.     }
  69. }