<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Make baugesuch_id nullable in baugesuchausnahmebewilligung table
* and drop baugesuchausnahmebewilligung_ohne_baugesuch table
*/
final class Version20251111000000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Make baugesuch_id nullable in baugesuchausnahmebewilligung and drop baugesuchausnahmebewilligung_ohne_baugesuch table';
}
public function up(Schema $schema): void
{
// Get the actual foreign key constraint name from information_schema
$connection = $this->connection;
$constraints = $connection->fetchAllAssociative(
"SELECT CONSTRAINT_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'baugesuchausnahmebewilligung'
AND COLUMN_NAME = 'baugesuch_id'
AND REFERENCED_TABLE_NAME IS NOT NULL"
);
// Drop foreign key constraint if it exists
if (!empty($constraints)) {
$constraintName = $constraints[0]['CONSTRAINT_NAME'];
$this->addSql("ALTER TABLE baugesuchausnahmebewilligung DROP FOREIGN KEY {$constraintName}");
}
// Make baugesuch_id nullable
$this->addSql('ALTER TABLE baugesuchausnahmebewilligung MODIFY baugesuch_id INT DEFAULT NULL');
// Re-add foreign key constraint with nullable support
if (!empty($constraints)) {
$constraintName = $constraints[0]['CONSTRAINT_NAME'];
$this->addSql("ALTER TABLE baugesuchausnahmebewilligung ADD CONSTRAINT {$constraintName} FOREIGN KEY (baugesuch_id) REFERENCES baugesuch (id)");
}
// Drop the redundant table if it exists
$this->addSql('DROP TABLE IF EXISTS baugesuchausnahmebewilligung_ohne_baugesuch');
}
public function down(Schema $schema): void
{
// Recreate baugesuchausnahmebewilligung_ohne_baugesuch table
$this->addSql('CREATE TABLE baugesuchausnahmebewilligung_ohne_baugesuch (
id INT AUTO_INCREMENT NOT NULL,
amtsblatt_id INT NOT NULL,
gemeinde_id INT NOT NULL,
status INT NOT NULL,
data LONGTEXT NOT NULL,
PRIMARY KEY(id),
INDEX IDX_amtsblatt_id (amtsblatt_id),
INDEX IDX_gemeinde_id (gemeinde_id),
CONSTRAINT FK_baugesuchausnahmebewilligung_ohne_baugesuch_amtsblatt_id FOREIGN KEY (amtsblatt_id) REFERENCES amtsblatt (id),
CONSTRAINT FK_baugesuchausnahmebewilligung_ohne_baugesuch_gemeinde_id FOREIGN KEY (gemeinde_id) REFERENCES gemeinde (id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
// Drop foreign key constraint
$this->addSql('ALTER TABLE baugesuchausnahmebewilligung DROP FOREIGN KEY FK_baugesuchausnahmebewilligung_baugesuch_id');
// Make baugesuch_id NOT NULL again
$this->addSql('ALTER TABLE baugesuchausnahmebewilligung MODIFY baugesuch_id INT NOT NULL');
// Re-add foreign key constraint with NOT NULL
$this->addSql('ALTER TABLE baugesuchausnahmebewilligung ADD CONSTRAINT FK_baugesuchausnahmebewilligung_baugesuch_id FOREIGN KEY (baugesuch_id) REFERENCES baugesuch (id)');
}
}