app/DoctrineMigrations/Version20250917000000.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.  * A学校でB学校、C学校のみを兄弟設定リストに表示する機能を追加
  9.  */
  10. final class Version20250917000000 extends AbstractMigration
  11. {
  12.     public function getDescription(): string
  13.     {
  14.         return '兄弟設定でのスクール表示制御テーブル (dtb_school_brother_display) を追加';
  15.     }
  16.     public function up(Schema $schema): void
  17.     {
  18.         $schemaManager $this->connection->createSchemaManager();
  19.         
  20.         // テーブルが存在しない場合のみ作成
  21.         if (!$schemaManager->tablesExist(['dtb_school_brother_display'])) {
  22.             // 兄弟設定表示制御テーブルを作成
  23.             $this->addSql('CREATE TABLE dtb_school_brother_display (
  24.                 id INT AUTO_INCREMENT NOT NULL,
  25.                 base_school_id INT UNSIGNED NOT NULL,
  26.                 target_school_id INT UNSIGNED NOT NULL,
  27.                 PRIMARY KEY(id),
  28.                 UNIQUE INDEX unique_school_pair (base_school_id, target_school_id),
  29.                 INDEX IDX_school_brother_display_base_school (base_school_id),
  30.                 INDEX IDX_school_brother_display_target_school (target_school_id)
  31.             ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_general_ci` ENGINE = InnoDB');
  32.             // 外部キー制約を追加
  33.             $this->addSql('ALTER TABLE dtb_school_brother_display 
  34.                 ADD CONSTRAINT FK_school_brother_display_base_school 
  35.                 FOREIGN KEY (base_school_id) REFERENCES dtb_school (school_id) ON DELETE CASCADE');
  36.             
  37.             $this->addSql('ALTER TABLE dtb_school_brother_display 
  38.                 ADD CONSTRAINT FK_school_brother_display_target_school 
  39.                 FOREIGN KEY (target_school_id) REFERENCES dtb_school (school_id) ON DELETE CASCADE');
  40.         }
  41.     }
  42.     public function down(Schema $schema): void
  43.     {
  44.         $schemaManager $this->connection->createSchemaManager();
  45.         
  46.         // テーブルが存在する場合のみ削除
  47.         if ($schemaManager->tablesExist(['dtb_school_brother_display'])) {
  48.             // 外部キー制約を削除
  49.             $this->addSql('ALTER TABLE dtb_school_brother_display DROP FOREIGN KEY FK_school_brother_display_base_school');
  50.             $this->addSql('ALTER TABLE dtb_school_brother_display DROP FOREIGN KEY FK_school_brother_display_target_school');
  51.             
  52.             // テーブルを削除
  53.             $this->addSql('DROP TABLE dtb_school_brother_display');
  54.         }
  55.     }
  56. }