app/DoctrineMigrations/Version20250917000000.php line 1

Open in your IDE?
  1. <?php
    
    declare(strict_types=1);
    
    namespace DoctrineMigrations;
    
    use Doctrine\DBAL\Schema\Schema;
    use Doctrine\Migrations\AbstractMigration;
    
    /**
     * 兄弟設定でのスクール表示制御のためのマイグレーション
     * A学校でB学校、C学校のみを兄弟設定リストに表示する機能を追加
     */
    final class Version20250917000000 extends AbstractMigration
    {
        public function getDescription(): string
        {
            return '兄弟設定でのスクール表示制御テーブル (dtb_school_brother_display) を追加';
        }
    
        public function up(Schema $schema): void
        {
            $schemaManager = $this->connection->createSchemaManager();
            
            // テーブルが存在しない場合のみ作成
            if (!$schemaManager->tablesExist(['dtb_school_brother_display'])) {
                // 兄弟設定表示制御テーブルを作成
                $this->addSql('CREATE TABLE dtb_school_brother_display (
                    id INT AUTO_INCREMENT NOT NULL,
                    base_school_id INT UNSIGNED NOT NULL,
                    target_school_id INT UNSIGNED NOT NULL,
                    PRIMARY KEY(id),
                    UNIQUE INDEX unique_school_pair (base_school_id, target_school_id),
                    INDEX IDX_school_brother_display_base_school (base_school_id),
                    INDEX IDX_school_brother_display_target_school (target_school_id)
                ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_general_ci` ENGINE = InnoDB');
    
                // 外部キー制約を追加
                $this->addSql('ALTER TABLE dtb_school_brother_display 
                    ADD CONSTRAINT FK_school_brother_display_base_school 
                    FOREIGN KEY (base_school_id) REFERENCES dtb_school (school_id) ON DELETE CASCADE');
                
                $this->addSql('ALTER TABLE dtb_school_brother_display 
                    ADD CONSTRAINT FK_school_brother_display_target_school 
                    FOREIGN KEY (target_school_id) REFERENCES dtb_school (school_id) ON DELETE CASCADE');
            }
        }
    
        public function down(Schema $schema): void
        {
            $schemaManager = $this->connection->createSchemaManager();
            
            // テーブルが存在する場合のみ削除
            if ($schemaManager->tablesExist(['dtb_school_brother_display'])) {
                // 外部キー制約を削除
                $this->addSql('ALTER TABLE dtb_school_brother_display DROP FOREIGN KEY FK_school_brother_display_base_school');
                $this->addSql('ALTER TABLE dtb_school_brother_display DROP FOREIGN KEY FK_school_brother_display_target_school');
                
                // テーブルを削除
                $this->addSql('DROP TABLE dtb_school_brother_display');
            }
        }
    }