<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Ensure all tables extending GenericEntity have created and modified columns.
*/
final class Version20251022101100 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add missing created and modified columns to all GenericEntity tables';
}
private function columnExists(string $table, string $column): bool
{
$result = $this->connection->fetchOne(
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = ?
AND COLUMN_NAME = ?",
[$table, $column]
);
return $result > 0;
}
public function up(Schema $schema): void
{
// Tables that extend GenericEntity and might be missing created/modified
$tables = [
'client_config',
'client_config_variable',
'course',
'course_data',
'course_field',
'course_image',
'course_occurrence',
'course_series',
'course_subscription',
'course_subscription_booking',
'course_text',
'course_type',
'customer_document',
'customer_history_entry',
'customer_order',
'customer_order_item',
'customer_order_item_person',
'invoice',
'invoice_item',
'invoice_payment',
'invoice_reminder',
'person',
'presence',
'speaker',
'speaker_image',
'speaker_text',
'venue',
'venue_document',
'venue_image',
'venue_room',
'wait_item',
'cart',
'cart_item',
'category',
'ckimage',
'email_history_entry',
'textblocks',
'protocol_entry',
'presence_reason',
'search_index_entry',
];
foreach ($tables as $table) {
// Add created column if missing
if (!$this->columnExists($table, 'created')) {
$this->addSql("ALTER TABLE $table ADD created DATETIME DEFAULT NULL AFTER client_id");
}
// Add modified column if missing
if (!$this->columnExists($table, 'modified')) {
$this->addSql("ALTER TABLE $table ADD modified DATETIME DEFAULT NULL AFTER created");
}
}
// Ensure columns are nullable
foreach ($tables as $table) {
if ($this->columnExists($table, 'created')) {
$this->addSql("ALTER TABLE $table MODIFY created DATETIME DEFAULT NULL");
}
if ($this->columnExists($table, 'modified')) {
$this->addSql("ALTER TABLE $table MODIFY modified DATETIME DEFAULT NULL");
}
}
}
public function down(Schema $schema): void
{
// Don't remove created/modified columns in rollback
// They might be needed by the application
}
}