app/DoctrineMigrations/Version20250922060000.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.  * 初期メンテナンス設定レコードを挿入 (id=1)
  8.  */
  9. final class Version20250922060000 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Insert initial maintenance settings record with id=1 if not exists';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         $schemaManager $this->connection->createSchemaManager();
  18.         
  19.         // テーブルが存在するかチェック
  20.         if (!$schemaManager->tablesExist(['dtb_maintenance_settings'])) {
  21.             return;
  22.         }
  23.         
  24.         // MySQL 向け: id=1 のレコードが存在しなければ挿入
  25.         $this->addSql("INSERT INTO dtb_maintenance_settings (status, target_type, title, content, start_time, end_time, create_date, update_date)
  26.             SELECT 0, 0, '初期メンテナンス', 'メンテナンス用の初期レコード', NULL, NULL, NOW(), NOW()
  27.             FROM DUAL
  28.             WHERE NOT EXISTS (SELECT 1 FROM dtb_maintenance_settings WHERE id = 1)");
  29.         // SQLite/Postgres などでは DUAL が不要だが、互換性がある場合は適宜修正されることを期待
  30.     }
  31.     public function down(Schema $schema): void
  32.     {
  33.         $schemaManager $this->connection->createSchemaManager();
  34.         
  35.         // テーブルが存在するかチェック
  36.         if (!$schemaManager->tablesExist(['dtb_maintenance_settings'])) {
  37.             return;
  38.         }
  39.         
  40.         // ダウングレード時は id=1 のレコードを削除
  41.         $this->addSql('DELETE FROM dtb_maintenance_settings WHERE id = 1');
  42.     }
  43. }