<?php
declare(strict_types=1);
namespace ProductsBundleMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\Uid\UuidV6;
final class Version20240229124327 extends AbstractMigration
{
private $stockMovements = [];
public function preUp(Schema $schema): void
{
$this->stockMovements = $this->connection->executeQuery('SELECT * FROM products_bundle__brand')->fetchAllAssociative();
}
public function up(Schema $schema): void
{
$this->addSql('SET FOREIGN_KEY_CHECKS=0');
$this->addSql('DROP INDEX name ON products_bundle__brand');
$this->addSql('ALTER TABLE products_bundle__brand DROP name, DROP description, DROP summary');
$this->addSql(
'ALTER TABLE products_bundle__brand_context ADD name VARCHAR(64) NOT NULL,
ADD description LONGTEXT DEFAULT NULL, ADD summary LONGTEXT DEFAULT NULL'
);
$this->addSql('CREATE INDEX name ON products_bundle__brand_context (name)');
$this->addSql('ALTER TABLE products_bundle__brand_translations DROP FOREIGN KEY FK_2941B107232D562B');
$this->addSql(
'ALTER TABLE products_bundle__brand_translations ADD CONSTRAINT FK_2941B107232D562B
FOREIGN KEY (object_id) REFERENCES products_bundle__brand_context (id) ON DELETE CASCADE'
);
$this->addSql('SET FOREIGN_KEY_CHECKS=1');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$conn = $this->connection;
foreach ($this->stockMovements as $sm) {
$smId = UuidV6::fromBinary($sm['id'])->toRfc4122();
$conn->executeStatement(
"UPDATE `products_bundle__brand_context` SET `name` = '" . addslashes((string) $sm['name']) . "', `description` = '" .
addslashes((string) $sm['description']) . "', `summary` = '" . addslashes((string) $sm['summary']) .
"' WHERE brand_id = UNHEX(CONCAT('', REPLACE('" . $smId . "', '-', '')))"
);
$brandTranslations = $conn->executeQuery(
"SELECT id, object_id, locale, field FROM products_bundle__brand_translations
WHERE object_id = UNHEX(CONCAT('', REPLACE('" . $smId . "', '-', '')))"
)->fetchAllAssociative();
$brandContexts = $conn->executeQuery(
"SELECT id, `name`, `summary`, `description` FROM products_bundle__brand_context
WHERE brand_id = UNHEX(CONCAT('', REPLACE('" . $smId . "', '-', '')))"
)->fetchAllAssociative();
$first = true;
foreach ($brandContexts as $bc) {
$bc_uuid = UuidV6::fromBinary($bc['id'])->toRfc4122();
$bc_uuid = '0x' . str_replace('-', '', $bc_uuid);
$summary_fr = false;
$description_fr = false;
if ($first) {
foreach ($brandTranslations as $bt) {
if ($bt['locale'] == 'fr') {
if ($bt['field'] == 'summary') {
$summary_fr = true;
} elseif ($bt['field'] == 'description') {
$description_fr = true;
}
}
$bt_id = $bt['id'];
$conn->executeStatement(
"UPDATE products_bundle__brand_translations SET object_id = " .
$bc_uuid . " WHERE id = " . $bt_id
);
}
$first = false;
}
if (!$summary_fr) {
$value = $bc['summary'];
if (!$value) {
$value = 'NULL';
} else {
$value = addslashes($value);
$value = "'" . $value . "'";
}
$conn->executeStatement(
"INSERT INTO products_bundle__brand_translations (id, object_id, locale, field, content)
VALUES (NULL, " . $bc_uuid . ", 'fr', 'summary', " . $value . ")"
);
}
if (!$description_fr) {
$value = $bc['description'];
if (!$value) {
$value = 'NULL';
} else {
$value = addslashes($value);
$value = "'" . $value . "'";
}
$conn->executeStatement(
"INSERT INTO products_bundle__brand_translations (id, object_id, locale, field, content)
VALUES (NULL, " . $bc_uuid . ", 'fr', 'description', " . $value . ")"
);
}
}
}
echo "fin brands \n";
}
public function down(Schema $schema): void
{
$this->addSql('SET FOREIGN_KEY_CHECKS=0');
$this->addSql(
'ALTER TABLE products_bundle__brand ADD name VARCHAR(64) NOT NULL,
ADD description LONGTEXT DEFAULT NULL, ADD summary LONGTEXT DEFAULT NULL'
);
$this->addSql('CREATE INDEX name ON products_bundle__brand (name)');
$this->addSql('DROP INDEX name ON products_bundle__brand_context');
$this->addSql('ALTER TABLE products_bundle__brand_context DROP name, DROP description, DROP summary');
$this->addSql('ALTER TABLE products_bundle__brand_translations DROP FOREIGN KEY FK_2941B107232D562B');
$this->addSql(
'ALTER TABLE products_bundle__brand_translations ADD CONSTRAINT FK_2941B107232D562B
FOREIGN KEY (object_id) REFERENCES products_bundle__brand (id) ON DELETE CASCADE'
);
$this->addSql('SET FOREIGN_KEY_CHECKS=1');
}
}