app/DoctrineMigrations/Version20250925093000.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. final class Version20250925093000 extends AbstractMigration
  7. {
  8.     public function getDescription(): string
  9.     {
  10.         return 'Ensure mtb_sex contains entries for "その他" and "回答しない" (ids 3 and 4)';
  11.     }
  12.     public function up(Schema $schema): void
  13.     {
  14.         $sm $this->connection->createSchemaManager();
  15.         if (!$sm->tablesExist(['mtb_sex'])) {
  16.             return;
  17.         }
  18.         // check existing ids
  19.         $existing = [];
  20.         $rows $this->connection->fetchAllAssociative('SELECT id FROM mtb_sex');
  21.         foreach ($rows as $r) {
  22.             $existing[(int)$r['id']] = true;
  23.         }
  24.         // insert id=3 (その他) if missing
  25.         if (!isset($existing[3])) {
  26.             $this->addSql("INSERT INTO mtb_sex (id, name, sort_no, discriminator_type) VALUES (3, 'その他', 2, 'sex')");
  27.         }
  28.         // insert id=4 (回答しない) if missing
  29.         if (!isset($existing[4])) {
  30.             $this->addSql("INSERT INTO mtb_sex (id, name, sort_no, discriminator_type) VALUES (4, '回答しない', 3, 'sex')");
  31.         }
  32.     }
  33.     public function down(Schema $schema): void
  34.     {
  35.         $sm $this->connection->createSchemaManager();
  36.         if (!$sm->tablesExist(['mtb_sex'])) {
  37.             return;
  38.         }
  39.         // remove entries we may have added
  40.         $this->addSql("DELETE FROM mtb_sex WHERE id IN (3,4)");
  41.     }
  42. }