app/DoctrineMigrations/Version20251110000000.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.  * フリー項目1,2に制限文字数カラムを追加
  8.  */
  9. final class Version20251110000000 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'dtb_schoolテーブルにfreeitem1_max_lengthとfreeitem2_max_lengthカラムを追加';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         $schemaManager $this->connection->createSchemaManager();
  18.         
  19.         // テーブルが存在するかチェック
  20.         if (!$schemaManager->tablesExist(['dtb_school'])) {
  21.             return;
  22.         }
  23.         
  24.         $columns $schemaManager->listTableColumns('dtb_school');
  25.         $columnNames array_map(function($column) {
  26.             return $column->getName();
  27.         }, $columns);
  28.         
  29.         // freeitem1_max_lengthカラムが存在しない場合のみ追加
  30.         if (!in_array('freeitem1_max_length'$columnNames)) {
  31.             // freeitem1_max_lengthカラムを追加
  32.             $this->addSql('ALTER TABLE dtb_school ADD freeitem1_max_length INT UNSIGNED DEFAULT NULL COMMENT \'フリー項目1の制限文字数\'');
  33.         }
  34.         
  35.         // freeitem2_max_lengthカラムが存在しない場合のみ追加
  36.         if (!in_array('freeitem2_max_length'$columnNames)) {
  37.             // freeitem2_max_lengthカラムを追加
  38.             $this->addSql('ALTER TABLE dtb_school ADD freeitem2_max_length INT UNSIGNED DEFAULT NULL COMMENT \'フリー項目2の制限文字数\'');
  39.         }
  40.     }
  41.     public function down(Schema $schema): void
  42.     {
  43.         $schemaManager $this->connection->createSchemaManager();
  44.         
  45.         // テーブルが存在するかチェック
  46.         if (!$schemaManager->tablesExist(['dtb_school'])) {
  47.             return;
  48.         }
  49.         
  50.         $columns $schemaManager->listTableColumns('dtb_school');
  51.         $columnNames array_map(function($column) {
  52.             return $column->getName();
  53.         }, $columns);
  54.         
  55.         // freeitem1_max_lengthカラムが存在する場合のみ削除
  56.         if (in_array('freeitem1_max_length'$columnNames)) {
  57.             // ロールバック時にカラムを削除
  58.             $this->addSql('ALTER TABLE dtb_school DROP COLUMN freeitem1_max_length');
  59.         }
  60.         
  61.         // freeitem2_max_lengthカラムが存在する場合のみ削除
  62.         if (in_array('freeitem2_max_length'$columnNames)) {
  63.             $this->addSql('ALTER TABLE dtb_school DROP COLUMN freeitem2_max_length');
  64.         }
  65.     }
  66. }