<?php
declare(strict_types=1);
namespace SuppliersBundleMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\Uid\UuidV6;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241015114523 extends AbstractMigration
{
private array $productSuppliers = [];
public function preUp(Schema $schema): void
{
$this->productSuppliers = $this->connection->executeQuery('SELECT id, product_id, declination_id, created_by_id, updated_by_id, deleted_by_id, created_at, updated_at, deleted_at, is_default FROM suppliers_bundle__product_supplier')->fetchAllAssociative();
}
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE suppliers_bundle__product_supplier_context (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', product_supplier_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', context_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', created_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', updated_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', deleted_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, is_default TINYINT(1) NOT NULL, INDEX IDX_6143A20C86F2F393 (product_supplier_id), INDEX IDX_6143A20C6B00C1CF (context_id), INDEX IDX_6143A20CB03A8386 (created_by_id), INDEX IDX_6143A20C896DBBDE (updated_by_id), INDEX IDX_6143A20CC76F1F52 (deleted_by_id), INDEX is_default (is_default), INDEX deleted_at (deleted_at), INDEX created_at (created_at), INDEX updated_at (updated_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context ADD CONSTRAINT FK_6143A20C86F2F393 FOREIGN KEY (product_supplier_id) REFERENCES suppliers_bundle__product_supplier (id)');
$this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context ADD CONSTRAINT FK_6143A20C6B00C1CF FOREIGN KEY (context_id) REFERENCES context (id)');
$this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context ADD CONSTRAINT FK_6143A20CB03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context ADD CONSTRAINT FK_6143A20C896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context ADD CONSTRAINT FK_6143A20CC76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
$this->addSql('DROP INDEX is_default ON suppliers_bundle__product_supplier');
$this->addSql('ALTER TABLE suppliers_bundle__product_supplier DROP is_default');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$conn = $this->connection;
foreach ($this->productSuppliers as $ps) {
$psId = '0x' . str_replace('-', '', UuidV6::fromBinary($ps['id'])->toRfc4122());
$createdById = $ps['created_by_id'] ? '0x' . str_replace('-', '', UuidV6::fromBinary($ps['created_by_id'])->toRfc4122()) : 'NULL';
$createdAt = $ps['created_at'] ? '"' . $ps['created_at'] . '"' : 'NULL';
$updatedById = $ps['updated_by_id'] ? '0x' . str_replace('-', '', UuidV6::fromBinary($ps['updated_by_id'])->toRfc4122()) : 'NULL';
$updatedAt = $ps['updated_at'] ? '"' . $ps['updated_at'] . '"' : 'NULL';
$deletedById = $ps['deleted_by_id'] ? '0x' . str_replace('-', '', UuidV6::fromBinary($ps['deleted_by_id'])->toRfc4122()) : 'NULL';
$deletedAt = $ps['deleted_at'] ? '"' . $ps['deleted_at'] . '"' : 'NULL';
$isDefault = $ps['is_default'] ? '1' : '0';
$decliId = $ps['declination_id'] ? '0x' . str_replace('-', '', UuidV6::fromBinary($ps['declination_id'])->toRfc4122()) : null;
$productId = $ps['product_id'] ? '0x' . str_replace('-', '', UuidV6::fromBinary($ps['product_id'])->toRfc4122()) : null;
if ($decliId) {
$contexts = $conn->executeQuery("SELECT context_id FROM products_bundle__declination_context WHERE declination_id = CAST(" . $decliId . " AS BINARY) AND deleted_at IS NULL")->fetchAllAssociative();
} else {
$contexts = $conn->executeQuery("SELECT context_id FROM products_bundle__product_context WHERE product_id = CAST(" . $productId . " AS BINARY) AND deleted_at IS NULL")->fetchAllAssociative();
}
foreach ($contexts as $context) {
$id = '0x' . str_replace('-', '', (new UuidV6())->toRfc4122());
$contextId = '0x' . str_replace('-', '', UuidV6::fromBinary($context['context_id'])->toRfc4122());
$query = 'INSERT INTO suppliers_bundle__product_supplier_context (id, product_supplier_id, context_id, created_by_id, updated_by_id, deleted_by_id, created_at, updated_at, deleted_at, is_default) VALUES (' . $id . ', ' . $psId . ', ' . $contextId . ', ' . $createdById . ', ' . $updatedById . ', ' . $deletedById . ', ' . $createdAt . ', ' . $updatedAt . ', ' . $deletedAt . ', ' . $isDefault . ')';
$conn->executeStatement($query);
}
}
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context DROP FOREIGN KEY FK_6143A20C86F2F393');
$this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context DROP FOREIGN KEY FK_6143A20C6B00C1CF');
$this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context DROP FOREIGN KEY FK_6143A20CB03A8386');
$this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context DROP FOREIGN KEY FK_6143A20C896DBBDE');
$this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context DROP FOREIGN KEY FK_6143A20CC76F1F52');
$this->addSql('DROP TABLE suppliers_bundle__product_supplier_context');
$this->addSql('ALTER TABLE suppliers_bundle__product_supplier ADD is_default TINYINT(1) NOT NULL');
$this->addSql('CREATE INDEX is_default ON suppliers_bundle__product_supplier (is_default)');
}
}