<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220217110616 extends AbstractMigration
{
public function getDescription(): string
{
return 'add table unit';
}
public function up(Schema $schema): void
{
$units = [
['id' => 'abb34377-d88e-4a81-a315-c239a24e97d4', 'name' => 'ООО "Креатив"', 'slug' => 'crtweb'],
['id' => '7b237b9d-d241-4c3e-93bb-b8b07241c757', 'name' => 'ООО "Курсорс"', 'slug' => 'cursource'],
];
$this->addSql('CREATE TABLE unit (id UUID NOT NULL, slug VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
$this->addSql('COMMENT ON COLUMN unit.id IS \'(DC2Type:uuid)\'');
$this->addSql('COMMENT ON COLUMN unit.slug IS \'Slug for Unit\'');
$this->addSql('COMMENT ON COLUMN unit.name IS \'Name of Unit\'');
$this->addSql('ALTER TABLE hub_user ADD unit_id UUID DEFAULT NULL');
$this->addSql('COMMENT ON COLUMN hub_user.unit_id IS \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE hub_user ADD CONSTRAINT FK_UNIT_ID FOREIGN KEY (unit_id) REFERENCES unit (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX IDX_UNIT_ID ON hub_user (unit_id)');
foreach ($units as $unit) {
$this->addSql('INSERT INTO unit ("id", "name", "slug") VALUES (:id, :name, :slug) ON CONFLICT DO NOTHING', $unit);
}
$this->addSql('UPDATE hub_user SET unit_id = (SELECT id FROM unit WHERE slug = :slug) WHERE unit_id IS NULL', $units[0]);
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE hub_user DROP CONSTRAINT FK_UNIT_ID');
$this->addSql('DROP TABLE unit');
$this->addSql('DROP INDEX IDX_UNIT_ID');
$this->addSql('ALTER TABLE hub_user DROP unit_id');
}
}