<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20250925093000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Ensure mtb_sex contains entries for "その他" and "回答しない" (ids 3 and 4)';
}
public function up(Schema $schema): void
{
$sm = $this->connection->createSchemaManager();
if (!$sm->tablesExist(['mtb_sex'])) {
return;
}
// check existing ids
$existing = [];
$rows = $this->connection->fetchAllAssociative('SELECT id FROM mtb_sex');
foreach ($rows as $r) {
$existing[(int)$r['id']] = true;
}
// insert id=3 (その他) if missing
if (!isset($existing[3])) {
$this->addSql("INSERT INTO mtb_sex (id, name, sort_no, discriminator_type) VALUES (3, 'その他', 2, 'sex')");
}
// insert id=4 (回答しない) if missing
if (!isset($existing[4])) {
$this->addSql("INSERT INTO mtb_sex (id, name, sort_no, discriminator_type) VALUES (4, '回答しない', 3, 'sex')");
}
}
public function down(Schema $schema): void
{
$sm = $this->connection->createSchemaManager();
if (!$sm->tablesExist(['mtb_sex'])) {
return;
}
// remove entries we may have added
$this->addSql("DELETE FROM mtb_sex WHERE id IN (3,4)");
}
}