src/Migrations/Version20210415150331.php line 1

  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use App\Model\Common\HaveOwnerInterface;
  5. use Doctrine\DBAL\Schema\Schema;
  6. use Doctrine\Migrations\AbstractMigration;
  7. use Ramsey\Uuid\Uuid;
  8. /**
  9.  * Auto-generated Migration: Please modify to your needs!
  10.  */
  11. final class Version20210415150331 extends AbstractMigration
  12. {
  13.     public function getDescription(): string
  14.     {
  15.         return '';
  16.     }
  17.     public function up(Schema $schema): void
  18.     {
  19.         // this up() migration is auto-generated, please modify it to your needs
  20.         $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');
  21.         $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');
  22.         $this->addSql('ALTER TABLE olympiad_user ADD CONSTRAINT FK_3DEEA8C1A76ED395 FOREIGN KEY (user_id) REFERENCES users (id)');
  23.         $this->addSql('ALTER TABLE contacts ADD owner_uuid VARCHAR(36) DEFAULT NULL');
  24.         $this->addSql('ALTER TABLE files ADD owner_uuid VARCHAR(36) DEFAULT NULL');
  25.         $this->addSql('ALTER TABLE forum_thread ADD owner_uuid VARCHAR(36) DEFAULT NULL');
  26.         $this->addSql('ALTER TABLE links ADD owner_uuid VARCHAR(36) DEFAULT NULL');
  27.         $this->addSql('ALTER TABLE material ADD owner_uuid VARCHAR(36) DEFAULT NULL');
  28.         $this->addSql('ALTER TABLE news ADD uuid VARCHAR(36) DEFAULT NULL, ADD owner_uuid VARCHAR(36) DEFAULT NULL');
  29.         $this->addSql('ALTER TABLE olympiad ADD uuid VARCHAR(36) DEFAULT NULL');
  30.         $this->addSql('ALTER TABLE organisation ADD uuid VARCHAR(36) DEFAULT NULL');
  31.         $this->addSql('ALTER TABLE olympiad_stage ADD uuid VARCHAR(36) DEFAULT NULL');
  32.         $this->addSql('ALTER TABLE reviews ADD owner_uuid VARCHAR(36) DEFAULT NULL');
  33.     }
  34.     public function down(Schema $schema): void
  35.     {
  36.         // this down() migration is auto-generated, please modify it to your needs
  37.         $this->addSql('DROP TABLE olympiad_user');
  38.         $this->addSql('DROP TABLE uuid_owners');
  39.         $this->addSql('ALTER TABLE contacts DROP owner_uuid');
  40.         $this->addSql('ALTER TABLE files DROP owner_uuid');
  41.         $this->addSql('ALTER TABLE forum_thread DROP owner_uuid');
  42.         $this->addSql('ALTER TABLE links DROP owner_uuid');
  43.         $this->addSql('ALTER TABLE material DROP owner_uuid');
  44.         $this->addSql('ALTER TABLE news DROP owner_uuid, DROP uuid');
  45.         $this->addSql('ALTER TABLE olympiad DROP uuid');
  46.         $this->addSql('ALTER TABLE organisation DROP uuid');
  47.         $this->addSql('ALTER TABLE olympiad_stage DROP uuid');
  48.         $this->addSql('ALTER TABLE reviews DROP owner_uuid');
  49.     }
  50.     public function postUp(Schema $schema): void
  51.     {
  52.         parent::postUp($schema);
  53.         $updateList = [];
  54.         $result $this->connection->executeQuery('select id from olympiad');
  55.         foreach ($result->fetchAllAssociative() as $o) {
  56.             $uuid Uuid::uuid4()->toString();
  57.             $this->connection->insert('uuid_owners', [
  58.                 'uuid' => $uuid,
  59.                 'type' => HaveOwnerInterface::OWNER_OLYMPIAD,
  60.             ]);
  61.             $this->connection->update(
  62.                 'olympiad',
  63.                 ['uuid' => $uuid,],
  64.                 ['id' => $o['id'],]
  65.             );
  66.             $updateList[$uuid] = ['type' => HaveOwnerInterface::OWNER_OLYMPIAD'id' => $o['id']];
  67.         }
  68.         $result $this->connection->executeQuery('select id from olympiad_stage');
  69.         foreach ($result->fetchAllAssociative() as $o) {
  70.             $uuid Uuid::uuid4()->toString();
  71.             $this->connection->insert('uuid_owners', [
  72.                 'uuid' => $uuid,
  73.                 'type' => HaveOwnerInterface::OWNER_OLYMPIAD_STAGE,
  74.             ]);
  75.             $this->connection->update(
  76.                 'olympiad_stage',
  77.                 ['uuid' => $uuid,],
  78.                 ['id' => $o['id'],]
  79.             );
  80.             $updateList[$uuid] = ['type' => HaveOwnerInterface::OWNER_OLYMPIAD_STAGE'id' => $o['id']];
  81.         }
  82.         $result $this->connection->executeQuery('select id from organisation');
  83.         foreach ($result->fetchAllAssociative() as $o) {
  84.             $uuid Uuid::uuid4()->toString();
  85.             $this->connection->insert('uuid_owners', [
  86.                 'uuid' => $uuid,
  87.                 'type' => HaveOwnerInterface::OWNER_ORGANISATION,
  88.             ]);
  89.             $this->connection->update(
  90.                 'organisation',
  91.                 ['uuid' => $uuid,],
  92.                 ['id' => $o['id'],]
  93.             );
  94.             $updateList[$uuid] = ['type' => HaveOwnerInterface::OWNER_ORGANISATION'id' => $o['id']];
  95.         }
  96.         $result $this->connection->executeQuery('select id from news');
  97.         foreach ($result->fetchAllAssociative() as $o) {
  98.             $uuid Uuid::uuid4()->toString();
  99.             $this->connection->insert('uuid_owners', [
  100.                 'uuid' => $uuid,
  101.                 'type' => HaveOwnerInterface::OWNER_NEWS,
  102.             ]);
  103.             $this->connection->update(
  104.                 'news',
  105.                 ['uuid' => $uuid,],
  106.                 ['id' => $o['id'],]
  107.             );
  108.             $updateList[$uuid] = ['type' => HaveOwnerInterface::OWNER_NEWS'id' => $o['id']];
  109.         }
  110.         foreach ($updateList as $uuid => $condition) {
  111.             foreach (['news''contacts''files''forum_thread''links''material''reviews'] as $t) {
  112.                 $this->connection->update(
  113.                     $t,
  114.                     ['owner_uuid' => $uuid,],
  115.                     [
  116.                         'owner_type' => $condition['type'],
  117.                         'owner_id' => $condition['id'],
  118.                     ]
  119.                 );
  120.             }
  121.         }
  122.     }
  123. }