migrations/Version20251022101100.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.  * Ensure all tables extending GenericEntity have created and modified columns.
  8.  */
  9. final class Version20251022101100 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Add missing created and modified columns to all GenericEntity tables';
  14.     }
  15.     private function columnExists(string $tablestring $column): bool
  16.     {
  17.         $result $this->connection->fetchOne(
  18.             "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
  19.              WHERE TABLE_SCHEMA = DATABASE() 
  20.              AND TABLE_NAME = ? 
  21.              AND COLUMN_NAME = ?",
  22.             [$table$column]
  23.         );
  24.         
  25.         return $result 0;
  26.     }
  27.     public function up(Schema $schema): void
  28.     {
  29.         // Tables that extend GenericEntity and might be missing created/modified
  30.         $tables = [
  31.             'client_config',
  32.             'client_config_variable',
  33.             'course',
  34.             'course_data',
  35.             'course_field',
  36.             'course_image',
  37.             'course_occurrence',
  38.             'course_series',
  39.             'course_subscription',
  40.             'course_subscription_booking',
  41.             'course_text',
  42.             'course_type',
  43.             'customer_document',
  44.             'customer_history_entry',
  45.             'customer_order',
  46.             'customer_order_item',
  47.             'customer_order_item_person',
  48.             'invoice',
  49.             'invoice_item',
  50.             'invoice_payment',
  51.             'invoice_reminder',
  52.             'person',
  53.             'presence',
  54.             'speaker',
  55.             'speaker_image',
  56.             'speaker_text',
  57.             'venue',
  58.             'venue_document',
  59.             'venue_image',
  60.             'venue_room',
  61.             'wait_item',
  62.             'cart',
  63.             'cart_item',
  64.             'category',
  65.             'ckimage',
  66.             'email_history_entry',
  67.             'textblocks',
  68.             'protocol_entry',
  69.             'presence_reason',
  70.             'search_index_entry',
  71.         ];
  72.         foreach ($tables as $table) {
  73.             // Add created column if missing
  74.             if (!$this->columnExists($table'created')) {
  75.                 $this->addSql("ALTER TABLE $table ADD created DATETIME DEFAULT NULL AFTER client_id");
  76.             }
  77.             // Add modified column if missing
  78.             if (!$this->columnExists($table'modified')) {
  79.                 $this->addSql("ALTER TABLE $table ADD modified DATETIME DEFAULT NULL AFTER created");
  80.             }
  81.         }
  82.         // Ensure columns are nullable
  83.         foreach ($tables as $table) {
  84.             if ($this->columnExists($table'created')) {
  85.                 $this->addSql("ALTER TABLE $table MODIFY created DATETIME DEFAULT NULL");
  86.             }
  87.             if ($this->columnExists($table'modified')) {
  88.                 $this->addSql("ALTER TABLE $table MODIFY modified DATETIME DEFAULT NULL");
  89.             }
  90.         }
  91.     }
  92.     public function down(Schema $schema): void
  93.     {
  94.         // Don't remove created/modified columns in rollback
  95.         // They might be needed by the application
  96.     }
  97. }