src/Migrations/Version20210415150331.php line 1
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use App\Model\Common\HaveOwnerInterface;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Ramsey\Uuid\Uuid;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210415150331 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE olympiad_user (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, owner VARCHAR(36) NOT NULL, type VARCHAR(255) NOT NULL, INDEX IDX_3DEEA8C1A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE uuid_owners (uuid VARCHAR(36) NOT NULL, type VARCHAR(255) NOT NULL, PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE olympiad_user ADD CONSTRAINT FK_3DEEA8C1A76ED395 FOREIGN KEY (user_id) REFERENCES users (id)');
$this->addSql('ALTER TABLE contacts ADD owner_uuid VARCHAR(36) DEFAULT NULL');
$this->addSql('ALTER TABLE files ADD owner_uuid VARCHAR(36) DEFAULT NULL');
$this->addSql('ALTER TABLE forum_thread ADD owner_uuid VARCHAR(36) DEFAULT NULL');
$this->addSql('ALTER TABLE links ADD owner_uuid VARCHAR(36) DEFAULT NULL');
$this->addSql('ALTER TABLE material ADD owner_uuid VARCHAR(36) DEFAULT NULL');
$this->addSql('ALTER TABLE news ADD uuid VARCHAR(36) DEFAULT NULL, ADD owner_uuid VARCHAR(36) DEFAULT NULL');
$this->addSql('ALTER TABLE olympiad ADD uuid VARCHAR(36) DEFAULT NULL');
$this->addSql('ALTER TABLE organisation ADD uuid VARCHAR(36) DEFAULT NULL');
$this->addSql('ALTER TABLE olympiad_stage ADD uuid VARCHAR(36) DEFAULT NULL');
$this->addSql('ALTER TABLE reviews ADD owner_uuid VARCHAR(36) DEFAULT NULL');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE olympiad_user');
$this->addSql('DROP TABLE uuid_owners');
$this->addSql('ALTER TABLE contacts DROP owner_uuid');
$this->addSql('ALTER TABLE files DROP owner_uuid');
$this->addSql('ALTER TABLE forum_thread DROP owner_uuid');
$this->addSql('ALTER TABLE links DROP owner_uuid');
$this->addSql('ALTER TABLE material DROP owner_uuid');
$this->addSql('ALTER TABLE news DROP owner_uuid, DROP uuid');
$this->addSql('ALTER TABLE olympiad DROP uuid');
$this->addSql('ALTER TABLE organisation DROP uuid');
$this->addSql('ALTER TABLE olympiad_stage DROP uuid');
$this->addSql('ALTER TABLE reviews DROP owner_uuid');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$updateList = [];
$result = $this->connection->executeQuery('select id from olympiad');
foreach ($result->fetchAllAssociative() as $o) {
$uuid = Uuid::uuid4()->toString();
$this->connection->insert('uuid_owners', [
'uuid' => $uuid,
'type' => HaveOwnerInterface::OWNER_OLYMPIAD,
]);
$this->connection->update(
'olympiad',
['uuid' => $uuid,],
['id' => $o['id'],]
);
$updateList[$uuid] = ['type' => HaveOwnerInterface::OWNER_OLYMPIAD, 'id' => $o['id']];
}
$result = $this->connection->executeQuery('select id from olympiad_stage');
foreach ($result->fetchAllAssociative() as $o) {
$uuid = Uuid::uuid4()->toString();
$this->connection->insert('uuid_owners', [
'uuid' => $uuid,
'type' => HaveOwnerInterface::OWNER_OLYMPIAD_STAGE,
]);
$this->connection->update(
'olympiad_stage',
['uuid' => $uuid,],
['id' => $o['id'],]
);
$updateList[$uuid] = ['type' => HaveOwnerInterface::OWNER_OLYMPIAD_STAGE, 'id' => $o['id']];
}
$result = $this->connection->executeQuery('select id from organisation');
foreach ($result->fetchAllAssociative() as $o) {
$uuid = Uuid::uuid4()->toString();
$this->connection->insert('uuid_owners', [
'uuid' => $uuid,
'type' => HaveOwnerInterface::OWNER_ORGANISATION,
]);
$this->connection->update(
'organisation',
['uuid' => $uuid,],
['id' => $o['id'],]
);
$updateList[$uuid] = ['type' => HaveOwnerInterface::OWNER_ORGANISATION, 'id' => $o['id']];
}
$result = $this->connection->executeQuery('select id from news');
foreach ($result->fetchAllAssociative() as $o) {
$uuid = Uuid::uuid4()->toString();
$this->connection->insert('uuid_owners', [
'uuid' => $uuid,
'type' => HaveOwnerInterface::OWNER_NEWS,
]);
$this->connection->update(
'news',
['uuid' => $uuid,],
['id' => $o['id'],]
);
$updateList[$uuid] = ['type' => HaveOwnerInterface::OWNER_NEWS, 'id' => $o['id']];
}
foreach ($updateList as $uuid => $condition) {
foreach (['news', 'contacts', 'files', 'forum_thread', 'links', 'material', 'reviews'] as $t) {
$this->connection->update(
$t,
['owner_uuid' => $uuid,],
[
'owner_type' => $condition['type'],
'owner_id' => $condition['id'],
]
);
}
}
}
}