<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20220314090107 extends AbstractMigration
{
public function getDescription(): string
{
return 'Выносит связь User и Team в отдельную сущность TeamMember(team_members)';
}
public function up(Schema $schema): void
{
$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))');
$this->addSql('CREATE INDEX idx_user ON team_members (user_id, is_lead)');
$this->addSql('CREATE INDEX idx_team ON team_members (team_id)');
$this->addSql('COMMENT ON COLUMN team_members.id IS \'(DC2Type:uuid)\'');
$this->addSql('COMMENT ON COLUMN team_members.user_id IS \'(DC2Type:uuid)\'');
$this->addSql('COMMENT ON COLUMN team_members.team_id IS \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE team_members ADD CONSTRAINT fk_hub_user_id FOREIGN KEY (user_id) REFERENCES hub_user (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE team_members ADD CONSTRAINT fk_team_id FOREIGN KEY (team_id) REFERENCES team (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('INSERT INTO team_members (id, user_id, team_id, is_lead)
SELECT
md5(random()::text || clock_timestamp()::text)::uuid AS id,
u.id AS user_id,
t.id AS team_id,
CASE WHEN u.id = t.lead_id THEN true ELSE false END AS is_lead
FROM hub_user AS u
JOIN team AS t
ON (u.member_of_team_id = t.id OR u.id = t.lead_id)
');
$this->addSql('ALTER TABLE hub_user DROP CONSTRAINT fk_8bd464f53f5737f6');
$this->addSql('DROP INDEX idx_8bd464f53f5737f6');
$this->addSql('ALTER TABLE hub_user DROP member_of_team_id');
$this->addSql('ALTER TABLE team DROP CONSTRAINT fk_c4e0a61f55458d');
$this->addSql('DROP INDEX uniq_c4e0a61f55458d');
$this->addSql('ALTER TABLE team DROP lead_id');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE hub_user ADD member_of_team_id UUID DEFAULT NULL');
$this->addSql('COMMENT ON COLUMN hub_user.member_of_team_id IS \'(DC2Type:uuid)\'');
$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');
$this->addSql('CREATE INDEX idx_8bd464f53f5737f6 ON hub_user (member_of_team_id)');
$this->addSql('ALTER TABLE team ADD lead_id UUID DEFAULT NULL');
$this->addSql('COMMENT ON COLUMN team.lead_id IS \'(DC2Type:uuid)\'');
$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');
$this->addSql('CREATE UNIQUE INDEX uniq_c4e0a61f55458d ON team (lead_id)');
$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');
$this->addSql('UPDATE hub_user hu SET member_of_team_id = h.team_id FROM team_members h WHERE h.user_id = hu.id');
$this->addSql('DROP TABLE team_members');
}
}