migrations/Version20251022110000.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.  * Add created and modified columns to course_occurrence_time
  8.  */
  9. final class Version20251022110000 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Add created and modified columns to course_occurrence_time';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // Check if columns exist before adding
  18.         $this->connection->executeStatement("
  19.             SET @col_created = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
  20.                 WHERE TABLE_SCHEMA = DATABASE() 
  21.                 AND TABLE_NAME = 'course_occurrence_time' 
  22.                 AND COLUMN_NAME = 'created');
  23.             SET @sql_created = IF(@col_created = 0, 
  24.                 'ALTER TABLE course_occurrence_time ADD created DATETIME DEFAULT NULL AFTER client_id', 
  25.                 'SELECT 1');
  26.             PREPARE stmt FROM @sql_created; 
  27.             EXECUTE stmt;
  28.         ");
  29.         
  30.         $this->connection->executeStatement("
  31.             SET @col_modified = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
  32.                 WHERE TABLE_SCHEMA = DATABASE() 
  33.                 AND TABLE_NAME = 'course_occurrence_time' 
  34.                 AND COLUMN_NAME = 'modified');
  35.             SET @sql_modified = IF(@col_modified = 0, 
  36.                 'ALTER TABLE course_occurrence_time ADD modified DATETIME DEFAULT NULL AFTER created', 
  37.                 'SELECT 1');
  38.             PREPARE stmt FROM @sql_modified; 
  39.             EXECUTE stmt;
  40.         ");
  41.         
  42.         // Set created date for existing records
  43.         $this->addSql("UPDATE course_occurrence_time SET created = NOW() WHERE created IS NULL");
  44.     }
  45.     public function down(Schema $schema): void
  46.     {
  47.         $this->addSql('ALTER TABLE course_occurrence_time DROP COLUMN IF EXISTS modified');
  48.         $this->addSql('ALTER TABLE course_occurrence_time DROP COLUMN IF EXISTS created');
  49.     }
  50. }