<?php
declare(strict_types=1);
namespace ShipmentsBundleMigrations;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Uid\UuidV6;
use Doctrine\Migrations\AbstractMigration;
final class Version20230725091406 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE shipments_bundle__burst ADD user_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD custom_text LONGTEXT DEFAULT NULL');
$this->addSql('ALTER TABLE shipments_bundle__burst ADD CONSTRAINT FK_561AB417A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)');
$this->addSql('CREATE INDEX IDX_561AB417A76ED395 ON shipments_bundle__burst (user_id)');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE shipments_bundle__burst DROP FOREIGN KEY FK_561AB417A76ED395');
$this->addSql('DROP INDEX IDX_561AB417A76ED395 ON shipments_bundle__burst');
$this->addSql('ALTER TABLE shipments_bundle__burst DROP user_id, DROP custom_text');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$bursts = $this->connection->executeQuery('SELECT * FROM shipments_bundle__burst')->fetchAllAssociative();
foreach ($bursts as $burst) {
$uuid = UuidV6::fromBinary($burst['id'])->toRfc4122();
$uuid = '0x' . str_replace('-', '', $uuid);
$createdByUuid = UuidV6::fromBinary($burst['created_by_id'])->toRfc4122();
$createdByUuid = '0x' . str_replace('-', '', $createdByUuid);
$this->connection->executeStatement("UPDATE shipments_bundle__burst SET user_id = " . $createdByUuid . " WHERE id = CAST(" . $uuid . " AS BINARY)");
}
}
}