app/DoctrineMigrations/Version20250925000000.php line 1

Open in your IDE?
  1. <?php
    
    declare(strict_types=1);
    
    namespace DoctrineMigrations;
    
    use Doctrine\DBAL\Schema\Schema;
    use Doctrine\Migrations\AbstractMigration;
    
    /**
     * Add sex_show_noanswer flag to dtb_school so per-school setting can be persisted
     */
    final class Version20250925000000 extends AbstractMigration
    {
        public function getDescription(): string
        {
            return 'Add sex_show_noanswer boolean column to dtb_school (default false)';
        }
    
        public function up(Schema $schema): void
        {
            $schemaManager = $this->connection->createSchemaManager();
    
            if (!$schemaManager->tablesExist(['dtb_school'])) {
                return;
            }
    
            $columns = $schemaManager->listTableColumns('dtb_school');
            $hasColumn = false;
            foreach ($columns as $column) {
                if ($column->getName() === 'sex_show_noanswer') {
                    $hasColumn = true;
                    break;
                }
            }
    
            if (!$hasColumn) {
                // boolean type; some DBs use TINYINT, use SMALLINT to be portable
                $this->addSql('ALTER TABLE dtb_school ADD sex_show_noanswer TINYINT(1) DEFAULT 0');
            }
        }
    
        public function down(Schema $schema): void
        {
            $schemaManager = $this->connection->createSchemaManager();
    
            if (!$schemaManager->tablesExist(['dtb_school'])) {
                return;
            }
    
            $columns = $schemaManager->listTableColumns('dtb_school');
            $hasColumn = false;
            foreach ($columns as $column) {
                if ($column->getName() === 'sex_show_noanswer') {
                    $hasColumn = true;
                    break;
                }
            }
    
            if ($hasColumn) {
                $this->addSql('ALTER TABLE dtb_school DROP sex_show_noanswer');
            }
        }
    }