migrations/Version20251022104000.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.  * Set NOT NULL constraints on created and client_id columns (now that data is assigned)
  8.  */
  9. final class Version20251022104000 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Set NOT NULL constraints on created and client_id columns';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // First, ensure all created dates have values
  18.         $tablesWithCreated = [
  19.             'customer_order''person''customer_order_item''invoice''wait_item',
  20.             'protocol_entry''course_occurrence''customer_history_entry''customer_document',
  21.             'speaker''venue''venue_room''venue_document''venue_image''course',
  22.             'course_subscription''category''course_image''course_text''course_series',
  23.             'course_type''invoice_payment''course_field''course_subscription_booking',
  24.             'customer_order_item_person''invoice_item''speaker_image''invoice_reminder',
  25.             'ckimage''course_data''cart_item''speaker_text''cart''presence',
  26.             'client_config''email_history_entry'
  27.         ];
  28.         foreach ($tablesWithCreated as $table) {
  29.             // Set created to NOW() where NULL
  30.             $this->addSql("UPDATE $table SET created = NOW() WHERE created IS NULL");
  31.             
  32.             // Set NOT NULL constraint
  33.             if ($table === 'invoice_reminder') {
  34.                 // Special case: invoice_reminder also has remind_date
  35.                 $this->addSql("ALTER TABLE $table CHANGE remind_date remind_date DATETIME NOT NULL, CHANGE created created DATETIME NOT NULL");
  36.             } else {
  37.                 $this->addSql("ALTER TABLE $table CHANGE created created DATETIME NOT NULL");
  38.             }
  39.         }
  40.         // Set client_id to NOT NULL for specific tables
  41.         $tablesWithClientId = [
  42.             'cart' => 'Cart should always have a client',
  43.             'presence' => 'Presence should always have a client',
  44.             'email_history_entry' => 'Email history should always have a client',
  45.             'course_data' => 'Course data should always have a client'
  46.         ];
  47.         foreach ($tablesWithClientId as $table => $comment) {
  48.             // First verify no NULL values exist
  49.             $result $this->connection->executeQuery("SELECT COUNT(*) as cnt FROM $table WHERE client_id IS NULL")->fetchAssociative();
  50.             
  51.             if ($result['cnt'] > 0) {
  52.                 // If NULL values exist, assign to client 1 (should not happen after the script ran)
  53.                 $this->addSql("UPDATE $table SET client_id = 1 WHERE client_id IS NULL");
  54.             }
  55.             
  56.             // Set NOT NULL constraint
  57.             $this->addSql("ALTER TABLE $table CHANGE client_id client_id INT NOT NULL");
  58.         }
  59.     }
  60.     public function down(Schema $schema): void
  61.     {
  62.         // Rollback: Set back to nullable
  63.         $tablesWithCreated = [
  64.             'customer_order''person''customer_order_item''invoice''wait_item',
  65.             'protocol_entry''course_occurrence''customer_history_entry''customer_document',
  66.             'speaker''venue''venue_room''venue_document''venue_image''course',
  67.             'course_subscription''category''course_image''course_text''course_series',
  68.             'course_type''invoice_payment''course_field''course_subscription_booking',
  69.             'customer_order_item_person''invoice_item''speaker_image''invoice_reminder',
  70.             'ckimage''course_data''cart_item''speaker_text''cart''presence',
  71.             'client_config''email_history_entry'
  72.         ];
  73.         foreach ($tablesWithCreated as $table) {
  74.             $this->addSql("ALTER TABLE $table CHANGE created created DATETIME DEFAULT NULL");
  75.         }
  76.         $tablesWithClientId = ['cart''presence''email_history_entry''course_data'];
  77.         foreach ($tablesWithClientId as $table) {
  78.             $this->addSql("ALTER TABLE $table CHANGE client_id client_id INT DEFAULT NULL");
  79.         }
  80.     }
  81. }