<?php
declare(strict_types=1);
namespace CategoriesBundleMigrations;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Uid\UuidV6;
use Doctrine\Migrations\AbstractMigration;
final class Version20231101130715 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('SET FOREIGN_KEY_CHECKS=0');
$this->addSql('ALTER TABLE categories_bundle__category_product DROP FOREIGN KEY FK_4F39BF044584665A');
$this->addSql('DROP INDEX IDX_4F39BF044584665A ON categories_bundle__category_product');
$this->addSql('ALTER TABLE categories_bundle__category_product CHANGE product_id product_context_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE categories_bundle__category_product ADD CONSTRAINT FK_4F39BF04F8679EE4 FOREIGN KEY (product_context_id) REFERENCES products_bundle__product_context (id)');
$this->addSql('CREATE INDEX IDX_4F39BF04F8679EE4 ON categories_bundle__category_product (product_context_id)');
$this->addSql('SET FOREIGN_KEY_CHECKS=1');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$conn = $this->connection;
$products = $conn->executeQuery('SELECT id FROM products_bundle__product')->fetchAllAssociative();
foreach ($products as $p) {
$p_uuid = UuidV6::fromBinary($p['id'])->toRfc4122();
$product_context = $conn->executeQuery("SELECT id FROM products_bundle__product_context WHERE product_id = UNHEX(CONCAT('', REPLACE('" . $p_uuid . "', '-', '')))")->fetchOne();
if ($product_context) {
$pc_uuid = UuidV6::fromBinary($product_context)->toRfc4122();
$pc_uuid = '0x' . str_replace('-', '', $pc_uuid);
$conn->executeStatement("UPDATE categories_bundle__category_product SET product_context_id = " . $pc_uuid . " WHERE product_context_id = UNHEX(CONCAT('', REPLACE('" . $p_uuid . "', '-', '')))");
}
}
}
public function down(Schema $schema): void
{
}
}