<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Set NOT NULL constraints on created and client_id columns (now that data is assigned)
*/
final class Version20251022104000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Set NOT NULL constraints on created and client_id columns';
}
public function up(Schema $schema): void
{
// First, ensure all created dates have values
$tablesWithCreated = [
'customer_order', 'person', 'customer_order_item', 'invoice', 'wait_item',
'protocol_entry', 'course_occurrence', 'customer_history_entry', 'customer_document',
'speaker', 'venue', 'venue_room', 'venue_document', 'venue_image', 'course',
'course_subscription', 'category', 'course_image', 'course_text', 'course_series',
'course_type', 'invoice_payment', 'course_field', 'course_subscription_booking',
'customer_order_item_person', 'invoice_item', 'speaker_image', 'invoice_reminder',
'ckimage', 'course_data', 'cart_item', 'speaker_text', 'cart', 'presence',
'client_config', 'email_history_entry'
];
foreach ($tablesWithCreated as $table) {
// Set created to NOW() where NULL
$this->addSql("UPDATE $table SET created = NOW() WHERE created IS NULL");
// Set NOT NULL constraint
if ($table === 'invoice_reminder') {
// Special case: invoice_reminder also has remind_date
$this->addSql("ALTER TABLE $table CHANGE remind_date remind_date DATETIME NOT NULL, CHANGE created created DATETIME NOT NULL");
} else {
$this->addSql("ALTER TABLE $table CHANGE created created DATETIME NOT NULL");
}
}
// Set client_id to NOT NULL for specific tables
$tablesWithClientId = [
'cart' => 'Cart should always have a client',
'presence' => 'Presence should always have a client',
'email_history_entry' => 'Email history should always have a client',
'course_data' => 'Course data should always have a client'
];
foreach ($tablesWithClientId as $table => $comment) {
// First verify no NULL values exist
$result = $this->connection->executeQuery("SELECT COUNT(*) as cnt FROM $table WHERE client_id IS NULL")->fetchAssociative();
if ($result['cnt'] > 0) {
// If NULL values exist, assign to client 1 (should not happen after the script ran)
$this->addSql("UPDATE $table SET client_id = 1 WHERE client_id IS NULL");
}
// Set NOT NULL constraint
$this->addSql("ALTER TABLE $table CHANGE client_id client_id INT NOT NULL");
}
}
public function down(Schema $schema): void
{
// Rollback: Set back to nullable
$tablesWithCreated = [
'customer_order', 'person', 'customer_order_item', 'invoice', 'wait_item',
'protocol_entry', 'course_occurrence', 'customer_history_entry', 'customer_document',
'speaker', 'venue', 'venue_room', 'venue_document', 'venue_image', 'course',
'course_subscription', 'category', 'course_image', 'course_text', 'course_series',
'course_type', 'invoice_payment', 'course_field', 'course_subscription_booking',
'customer_order_item_person', 'invoice_item', 'speaker_image', 'invoice_reminder',
'ckimage', 'course_data', 'cart_item', 'speaker_text', 'cart', 'presence',
'client_config', 'email_history_entry'
];
foreach ($tablesWithCreated as $table) {
$this->addSql("ALTER TABLE $table CHANGE created created DATETIME DEFAULT NULL");
}
$tablesWithClientId = ['cart', 'presence', 'email_history_entry', 'course_data'];
foreach ($tablesWithClientId as $table) {
$this->addSql("ALTER TABLE $table CHANGE client_id client_id INT DEFAULT NULL");
}
}
}