<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Add created and modified columns to course_occurrence_time
*/
final class Version20251022110000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add created and modified columns to course_occurrence_time';
}
public function up(Schema $schema): void
{
// Check if columns exist before adding
$this->connection->executeStatement("
SET @col_created = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'course_occurrence_time'
AND COLUMN_NAME = 'created');
SET @sql_created = IF(@col_created = 0,
'ALTER TABLE course_occurrence_time ADD created DATETIME DEFAULT NULL AFTER client_id',
'SELECT 1');
PREPARE stmt FROM @sql_created;
EXECUTE stmt;
");
$this->connection->executeStatement("
SET @col_modified = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'course_occurrence_time'
AND COLUMN_NAME = 'modified');
SET @sql_modified = IF(@col_modified = 0,
'ALTER TABLE course_occurrence_time ADD modified DATETIME DEFAULT NULL AFTER created',
'SELECT 1');
PREPARE stmt FROM @sql_modified;
EXECUTE stmt;
");
// Set created date for existing records
$this->addSql("UPDATE course_occurrence_time SET created = NOW() WHERE created IS NULL");
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE course_occurrence_time DROP COLUMN IF EXISTS modified');
$this->addSql('ALTER TABLE course_occurrence_time DROP COLUMN IF EXISTS created');
}
}