migrations/Version20251111000000.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. * Make baugesuch_id nullable in baugesuchausnahmebewilligung table
  8. * and drop baugesuchausnahmebewilligung_ohne_baugesuch table
  9. */
  10. final class Version20251111000000 extends AbstractMigration
  11. {
  12. public function getDescription(): string
  13. {
  14. return 'Make baugesuch_id nullable in baugesuchausnahmebewilligung and drop baugesuchausnahmebewilligung_ohne_baugesuch table';
  15. }
  16. public function up(Schema $schema): void
  17. {
  18. // Get the actual foreign key constraint name from information_schema
  19. $connection = $this->connection;
  20. $constraints = $connection->fetchAllAssociative(
  21. "SELECT CONSTRAINT_NAME
  22. FROM information_schema.KEY_COLUMN_USAGE
  23. WHERE TABLE_SCHEMA = DATABASE()
  24. AND TABLE_NAME = 'baugesuchausnahmebewilligung'
  25. AND COLUMN_NAME = 'baugesuch_id'
  26. AND REFERENCED_TABLE_NAME IS NOT NULL"
  27. );
  28. // Drop foreign key constraint if it exists
  29. if (!empty($constraints)) {
  30. $constraintName = $constraints[0]['CONSTRAINT_NAME'];
  31. $this->addSql("ALTER TABLE baugesuchausnahmebewilligung DROP FOREIGN KEY {$constraintName}");
  32. }
  33. // Make baugesuch_id nullable
  34. $this->addSql('ALTER TABLE baugesuchausnahmebewilligung MODIFY baugesuch_id INT DEFAULT NULL');
  35. // Re-add foreign key constraint with nullable support
  36. if (!empty($constraints)) {
  37. $constraintName = $constraints[0]['CONSTRAINT_NAME'];
  38. $this->addSql("ALTER TABLE baugesuchausnahmebewilligung ADD CONSTRAINT {$constraintName} FOREIGN KEY (baugesuch_id) REFERENCES baugesuch (id)");
  39. }
  40. // Drop the redundant table if it exists
  41. $this->addSql('DROP TABLE IF EXISTS baugesuchausnahmebewilligung_ohne_baugesuch');
  42. }
  43. public function down(Schema $schema): void
  44. {
  45. // Recreate baugesuchausnahmebewilligung_ohne_baugesuch table
  46. $this->addSql('CREATE TABLE baugesuchausnahmebewilligung_ohne_baugesuch (
  47. id INT AUTO_INCREMENT NOT NULL,
  48. amtsblatt_id INT NOT NULL,
  49. gemeinde_id INT NOT NULL,
  50. status INT NOT NULL,
  51. data LONGTEXT NOT NULL,
  52. PRIMARY KEY(id),
  53. INDEX IDX_amtsblatt_id (amtsblatt_id),
  54. INDEX IDX_gemeinde_id (gemeinde_id),
  55. CONSTRAINT FK_baugesuchausnahmebewilligung_ohne_baugesuch_amtsblatt_id FOREIGN KEY (amtsblatt_id) REFERENCES amtsblatt (id),
  56. CONSTRAINT FK_baugesuchausnahmebewilligung_ohne_baugesuch_gemeinde_id FOREIGN KEY (gemeinde_id) REFERENCES gemeinde (id)
  57. ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  58. // Drop foreign key constraint
  59. $this->addSql('ALTER TABLE baugesuchausnahmebewilligung DROP FOREIGN KEY FK_baugesuchausnahmebewilligung_baugesuch_id');
  60. // Make baugesuch_id NOT NULL again
  61. $this->addSql('ALTER TABLE baugesuchausnahmebewilligung MODIFY baugesuch_id INT NOT NULL');
  62. // Re-add foreign key constraint with NOT NULL
  63. $this->addSql('ALTER TABLE baugesuchausnahmebewilligung ADD CONSTRAINT FK_baugesuchausnahmebewilligung_baugesuch_id FOREIGN KEY (baugesuch_id) REFERENCES baugesuch (id)');
  64. }
  65. }