<?php
declare(strict_types=1);
namespace StocksBundleMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\Uid\UuidV6;
final class Version20231205125131 extends AbstractMigration
{
private $stockMovements = [];
public function preUp(Schema $schema): void
{
$this->stockMovements = $this->connection->executeQuery('SELECT * FROM stocks_bundle__stock_movement')->fetchAllAssociative();
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE stocks_bundle__stock_movement ADD entity_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD entity_name VARCHAR(255) DEFAULT NULL');
$this->addSql('CREATE INDEX entity_id ON stocks_bundle__stock_movement (entity_id)');
$this->addSql('CREATE INDEX entity_name ON stocks_bundle__stock_movement (entity_name)');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$conn = $this->connection;
foreach ($this->stockMovements as $sm) {
$smId = UuidV6::fromBinary($sm['id'])->toRfc4122();
if (isset($sm['product_return_line_id']) && $sm['product_return_line_id']) {
$uuid = '0x' . str_replace('-', '', UuidV6::fromBinary($sm['product_return_line_id'])->toRfc4122());
$conn->executeStatement("UPDATE stocks_bundle__stock_movement SET entity_name = 'Bluue\\\\ProductReturnsBundle\\\\Entity\\\\ProductReturnLine', entity_id = " . $uuid . " WHERE id = UNHEX(CONCAT('', REPLACE('" . $smId . "', '-', '')))");
}
if (isset($sm['delivery_note_line_id']) && $sm['delivery_note_line_id']) {
$uuid = '0x' . str_replace('-', '', UuidV6::fromBinary($sm['delivery_note_line_id'])->toRfc4122());
$conn->executeStatement("UPDATE stocks_bundle__stock_movement SET entity_name = 'Bluue\\\\SalesBundle\\\\Entity\\\\DeliveryNoteLine', entity_id = " . $uuid . " WHERE id = UNHEX(CONCAT('', REPLACE('" . $smId . "', '-', '')))");
}
if (isset($sm['delivery_note_id']) && $sm['delivery_note_id'] && !$sm['delivery_note_line_id']) {
$uuid = '0x' . str_replace('-', '', UuidV6::fromBinary($sm['delivery_note_id'])->toRfc4122());
$conn->executeStatement("UPDATE stocks_bundle__stock_movement SET entity_name = 'Bluue\\\\SalesBundle\\\\Entity\\\\DeliveryNote', entity_id = " . $uuid . " WHERE id = UNHEX(CONCAT('', REPLACE('" . $smId . "', '-', '')))");
}
}
}
public function down(Schema $schema): void
{
$this->addSql('DROP INDEX entity_id ON stocks_bundle__stock_movement');
$this->addSql('DROP INDEX entity_name ON stocks_bundle__stock_movement');
$this->addSql('ALTER TABLE stocks_bundle__stock_movement ADD delivery_note_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD delivery_note_line_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD product_return_line_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', DROP entity_id, DROP entity_name');
$this->addSql('ALTER TABLE stocks_bundle__stock_movement ADD CONSTRAINT FK_11E6623A535E48DC FOREIGN KEY (delivery_note_line_id) REFERENCES sales_bundle__delivery_note_line (id)');
$this->addSql('ALTER TABLE stocks_bundle__stock_movement ADD CONSTRAINT FK_11E6623A2CF3B78B FOREIGN KEY (delivery_note_id) REFERENCES sales_bundle__delivery_note (id)');
$this->addSql('ALTER TABLE stocks_bundle__stock_movement ADD CONSTRAINT FK_11E6623A83A19B1 FOREIGN KEY (product_return_line_id) REFERENCES product_returns_bundle__product_return_line (id)');
$this->addSql('CREATE INDEX IDX_11E6623A2CF3B78B ON stocks_bundle__stock_movement (delivery_note_id)');
$this->addSql('CREATE INDEX IDX_11E6623A83A19B1 ON stocks_bundle__stock_movement (product_return_line_id)');
$this->addSql('CREATE INDEX IDX_11E6623A535E48DC ON stocks_bundle__stock_movement (delivery_note_line_id)');
}
}