vendor/bluue/categories-bundle/migrations/Version20231101130715.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CategoriesBundleMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Symfony\Component\Uid\UuidV6;
  6. use Doctrine\Migrations\AbstractMigration;
  7. final class Version20231101130715 extends AbstractMigration
  8. {
  9.     public function up(Schema $schema): void
  10.     {
  11.         $this->addSql('SET FOREIGN_KEY_CHECKS=0');
  12.         $this->addSql('ALTER TABLE categories_bundle__category_product DROP FOREIGN KEY FK_4F39BF044584665A');
  13.         $this->addSql('DROP INDEX IDX_4F39BF044584665A ON categories_bundle__category_product');
  14.         $this->addSql('ALTER TABLE categories_bundle__category_product CHANGE product_id product_context_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\'');
  15.         $this->addSql('ALTER TABLE categories_bundle__category_product ADD CONSTRAINT FK_4F39BF04F8679EE4 FOREIGN KEY (product_context_id) REFERENCES products_bundle__product_context (id)');
  16.         $this->addSql('CREATE INDEX IDX_4F39BF04F8679EE4 ON categories_bundle__category_product (product_context_id)');
  17.         $this->addSql('SET FOREIGN_KEY_CHECKS=1');
  18.     }
  19.     public function postUp(Schema $schema): void
  20.     {
  21.         parent::postUp($schema);
  22.         $conn $this->connection;
  23.         $products $conn->executeQuery('SELECT id FROM products_bundle__product')->fetchAllAssociative();
  24.         foreach ($products as $p) {
  25.             $p_uuid UuidV6::fromBinary($p['id'])->toRfc4122();
  26.             $product_context $conn->executeQuery("SELECT id FROM products_bundle__product_context WHERE product_id = UNHEX(CONCAT('', REPLACE('" $p_uuid "', '-', '')))")->fetchOne();
  27.             if ($product_context) {
  28.                 $pc_uuid UuidV6::fromBinary($product_context)->toRfc4122();
  29.                 $pc_uuid '0x' str_replace('-'''$pc_uuid);
  30.                 $conn->executeStatement("UPDATE categories_bundle__category_product SET product_context_id = " $pc_uuid " WHERE product_context_id = UNHEX(CONCAT('', REPLACE('" $p_uuid "', '-', '')))");
  31.             }
  32.         }
  33.     }
  34.     public function down(Schema $schema): void
  35.     {
  36.     }
  37. }