migrations/Version20230518141800.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.  * Auto-generated Migration: Please modify to your needs!
  8.  */
  9. final class Version20230518141800 extends AbstractMigration
  10. {
  11.     private const GEOLOCATION_TABLE 'geolocation';
  12.     private const FK_NAME 'FK_9DC0E5B461220EA6';
  13.     public function getDescription(): string
  14.     {
  15.         return 'Remove bidirectional association';
  16.     }
  17.     public function up(Schema $schema): void
  18.     {
  19.         if ($this->foreignKeyExists(self::GEOLOCATION_TABLEself::FK_NAME)) {
  20.             $this->addSql("ALTER TABLE `geolocation` DROP FOREIGN KEY `FK_9DC0E5B461220EA6`");
  21.         }
  22.         $this->addSql('ALTER TABLE `geolocation` DROP COLUMN `creator_id`');
  23.     }
  24.     public function down(Schema $schema): void
  25.     {
  26.         $this->addSql('
  27.             ALTER TABLE `geolocation`
  28.             ADD COLUMN `creator_id` bigint UNSIGNED NULL AFTER `id`,
  29.             ADD UNIQUE INDEX `UNIQ_9DC0E5B461220EA6`(`creator_id`),
  30.             ADD CONSTRAINT `FK_9DC0E5B461220EA6` FOREIGN KEY (`creator_id`) REFERENCES `creator` (`id`) ON DELETE CASCADE
  31.         ');
  32.     }
  33.     private function foreignKeyExists(string $tableNamestring $constraintName): bool
  34.     {
  35.         $sql = <<<'SQL'
  36.             SELECT COUNT(*)
  37.             FROM information_schema.TABLE_CONSTRAINTS
  38.             WHERE CONSTRAINT_SCHEMA = DATABASE()
  39.               AND TABLE_NAME = :tableName
  40.               AND CONSTRAINT_NAME = :constraintName
  41.               AND CONSTRAINT_TYPE = 'FOREIGN KEY'
  42.         SQL;
  43.         return (int) $this->connection->fetchOne($sql, [
  44.             'tableName' => $tableName,
  45.             'constraintName' => $constraintName,
  46.         ]) > 0;
  47.     }
  48. }