<?php
declare(strict_types=1);
namespace SalesBundleMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\Uid\UuidV6;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240301131526 extends AbstractMigration
{
private array $orders_quotations = [];
public function preUp(Schema $schema): void
{
$this->orders_quotations = $this->connection->executeQuery('SELECT id, quotation_id FROM sales_bundle__order WHERE quotation_id IS NOT NULL')->fetchAllAssociative();
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE sales_bundle__order DROP FOREIGN KEY FK_FBA82B62B4EA4E60');
$this->addSql('DROP INDEX UNIQ_FBA82B62B4EA4E60 ON sales_bundle__order');
$this->addSql('ALTER TABLE sales_bundle__order DROP quotation_id');
$this->addSql('ALTER TABLE sales_bundle__quotation ADD order_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE sales_bundle__quotation ADD CONSTRAINT FK_A0FC50DF8D9F6D38 FOREIGN KEY (order_id) REFERENCES sales_bundle__order (id)');
$this->addSql('CREATE INDEX IDX_A0FC50DF8D9F6D38 ON sales_bundle__quotation (order_id)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE sales_bundle__order ADD quotation_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE sales_bundle__order ADD CONSTRAINT FK_FBA82B62B4EA4E60 FOREIGN KEY (quotation_id) REFERENCES sales_bundle__quotation (id)');
$this->addSql('CREATE UNIQUE INDEX UNIQ_FBA82B62B4EA4E60 ON sales_bundle__order (quotation_id)');
$this->addSql('ALTER TABLE sales_bundle__quotation DROP FOREIGN KEY FK_A0FC50DF8D9F6D38');
$this->addSql('DROP INDEX IDX_A0FC50DF8D9F6D38 ON sales_bundle__quotation');
$this->addSql('ALTER TABLE sales_bundle__quotation DROP order_id');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$conn = $this->connection;
foreach ($this->orders_quotations as $oq) {
$order_uuid = '0x' . str_replace('-', '', UuidV6::fromBinary($oq['id'])->toRfc4122());
$quotation_uuid = UuidV6::fromBinary($oq['quotation_id'])->toRfc4122();
$conn->executeStatement("UPDATE sales_bundle__quotation SET order_id = " . $order_uuid . " WHERE id = UNHEX(CONCAT('', REPLACE('" . $quotation_uuid . "', '-', '')))");
}
}
}