app/DoctrineMigrations/Version20250922060000.php line 1

Open in your IDE?
  1. <?php
    
    declare(strict_types=1);
    
    namespace DoctrineMigrations;
    
    use Doctrine\DBAL\Schema\Schema;
    use Doctrine\Migrations\AbstractMigration;
    
    /**
     * 初期メンテナンス設定レコードを挿入 (id=1)
     */
    final class Version20250922060000 extends AbstractMigration
    {
        public function getDescription(): string
        {
            return 'Insert initial maintenance settings record with id=1 if not exists';
        }
    
        public function up(Schema $schema): void
        {
            $schemaManager = $this->connection->createSchemaManager();
            
            // テーブルが存在するかチェック
            if (!$schemaManager->tablesExist(['dtb_maintenance_settings'])) {
                return;
            }
            
            // MySQL 向け: id=1 のレコードが存在しなければ挿入
            $this->addSql("INSERT INTO dtb_maintenance_settings (status, target_type, title, content, start_time, end_time, create_date, update_date)
                SELECT 0, 0, '初期メンテナンス', 'メンテナンス用の初期レコード', NULL, NULL, NOW(), NOW()
                FROM DUAL
                WHERE NOT EXISTS (SELECT 1 FROM dtb_maintenance_settings WHERE id = 1)");
    
            // SQLite/Postgres などでは DUAL が不要だが、互換性がある場合は適宜修正されることを期待
        }
    
        public function down(Schema $schema): void
        {
            $schemaManager = $this->connection->createSchemaManager();
            
            // テーブルが存在するかチェック
            if (!$schemaManager->tablesExist(['dtb_maintenance_settings'])) {
                return;
            }
            
            // ダウングレード時は id=1 のレコードを削除
            $this->addSql('DELETE FROM dtb_maintenance_settings WHERE id = 1');
        }
    }