migrations/Version20220314090107.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. final class Version20220314090107 extends AbstractMigration
  7. {
  8.     public function getDescription(): string
  9.     {
  10.         return 'Выносит связь User и Team в отдельную сущность TeamMember(team_members)';
  11.     }
  12.     public function up(Schema $schema): void
  13.     {
  14.         $this->addSql('CREATE TABLE team_members (id UUID NOT NULL, user_id UUID, team_id UUID, is_lead BOOLEAN DEFAULT false NOT NULL, PRIMARY KEY(id))');
  15.         $this->addSql('CREATE INDEX idx_user ON team_members (user_id, is_lead)');
  16.         $this->addSql('CREATE INDEX idx_team ON team_members (team_id)');
  17.         $this->addSql('COMMENT ON COLUMN team_members.id IS \'(DC2Type:uuid)\'');
  18.         $this->addSql('COMMENT ON COLUMN team_members.user_id IS \'(DC2Type:uuid)\'');
  19.         $this->addSql('COMMENT ON COLUMN team_members.team_id IS \'(DC2Type:uuid)\'');
  20.         $this->addSql('ALTER TABLE team_members ADD CONSTRAINT fk_hub_user_id FOREIGN KEY (user_id) REFERENCES hub_user (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
  21.         $this->addSql('ALTER TABLE team_members ADD CONSTRAINT fk_team_id FOREIGN KEY (team_id) REFERENCES team (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
  22.         $this->addSql('INSERT INTO team_members (id, user_id, team_id, is_lead)
  23.             SELECT
  24.                 md5(random()::text || clock_timestamp()::text)::uuid AS id,    
  25.                 u.id AS user_id,
  26.                 t.id AS team_id,
  27.                 CASE WHEN u.id = t.lead_id THEN true ELSE false END AS is_lead
  28.             FROM hub_user AS u 
  29.             JOIN team AS t 
  30.                 ON (u.member_of_team_id = t.id OR u.id = t.lead_id)
  31.         ');
  32.         $this->addSql('ALTER TABLE hub_user DROP CONSTRAINT fk_8bd464f53f5737f6');
  33.         $this->addSql('DROP INDEX idx_8bd464f53f5737f6');
  34.         $this->addSql('ALTER TABLE hub_user DROP member_of_team_id');
  35.         $this->addSql('ALTER TABLE team DROP CONSTRAINT fk_c4e0a61f55458d');
  36.         $this->addSql('DROP INDEX uniq_c4e0a61f55458d');
  37.         $this->addSql('ALTER TABLE team DROP lead_id');
  38.     }
  39.     public function down(Schema $schema): void
  40.     {
  41.         $this->addSql('ALTER TABLE hub_user ADD member_of_team_id UUID DEFAULT NULL');
  42.         $this->addSql('COMMENT ON COLUMN hub_user.member_of_team_id IS \'(DC2Type:uuid)\'');
  43.         $this->addSql('ALTER TABLE hub_user ADD CONSTRAINT fk_8bd464f53f5737f6 FOREIGN KEY (member_of_team_id) REFERENCES team (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
  44.         $this->addSql('CREATE INDEX idx_8bd464f53f5737f6 ON hub_user (member_of_team_id)');
  45.         $this->addSql('ALTER TABLE team ADD lead_id UUID DEFAULT NULL');
  46.         $this->addSql('COMMENT ON COLUMN team.lead_id IS \'(DC2Type:uuid)\'');
  47.         $this->addSql('ALTER TABLE team ADD CONSTRAINT fk_c4e0a61f55458d FOREIGN KEY (lead_id) REFERENCES hub_user (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
  48.         $this->addSql('CREATE UNIQUE INDEX uniq_c4e0a61f55458d ON team (lead_id)');
  49.         $this->addSql('UPDATE team t SET lead_id = h.user_id FROM team_members h WHERE h.team_id = t.id AND h.is_lead IS true');
  50.         $this->addSql('UPDATE hub_user hu SET member_of_team_id = h.team_id FROM team_members h WHERE h.user_id = hu.id');
  51.         $this->addSql('DROP TABLE team_members');
  52.     }
  53. }