migrations/Version20220217110616.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 Version20220217110616 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'add table unit';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         $units = [
  18.             ['id' => 'abb34377-d88e-4a81-a315-c239a24e97d4''name' => 'ООО "Креатив"''slug' => 'crtweb'],
  19.             ['id' => '7b237b9d-d241-4c3e-93bb-b8b07241c757''name' => 'ООО "Курсорс"''slug' => 'cursource'],
  20.         ];
  21.         $this->addSql('CREATE TABLE unit (id UUID NOT NULL, slug VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
  22.         $this->addSql('COMMENT ON COLUMN unit.id IS \'(DC2Type:uuid)\'');
  23.         $this->addSql('COMMENT ON COLUMN unit.slug IS \'Slug for Unit\'');
  24.         $this->addSql('COMMENT ON COLUMN unit.name IS \'Name of Unit\'');
  25.         $this->addSql('ALTER TABLE hub_user ADD unit_id UUID DEFAULT NULL');
  26.         $this->addSql('COMMENT ON COLUMN hub_user.unit_id IS \'(DC2Type:uuid)\'');
  27.         $this->addSql('ALTER TABLE hub_user ADD CONSTRAINT FK_UNIT_ID FOREIGN KEY (unit_id) REFERENCES unit (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
  28.         $this->addSql('CREATE INDEX IDX_UNIT_ID ON hub_user (unit_id)');
  29.         foreach ($units as $unit) {
  30.             $this->addSql('INSERT INTO unit ("id", "name", "slug") VALUES (:id, :name, :slug) ON CONFLICT DO NOTHING'$unit);
  31.         }
  32.         $this->addSql('UPDATE hub_user SET unit_id = (SELECT id FROM unit WHERE slug = :slug) WHERE unit_id IS NULL'$units[0]);
  33.     }
  34.     public function down(Schema $schema): void
  35.     {
  36.         $this->addSql('ALTER TABLE hub_user DROP CONSTRAINT FK_UNIT_ID');
  37.         $this->addSql('DROP TABLE unit');
  38.         $this->addSql('DROP INDEX IDX_UNIT_ID');
  39.         $this->addSql('ALTER TABLE hub_user DROP unit_id');
  40.     }
  41. }