<?php
declare(strict_types=1);
namespace ProductsBundleMigrations;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Uid\UuidV6;
use Doctrine\Migrations\AbstractMigration;
final class Version20221227131457 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('SET FOREIGN_KEY_CHECKS=0');
$this->addSql('ALTER TABLE products_bundle__declination_context ADD reference VARCHAR(128) DEFAULT NULL, ADD wholesale_price NUMERIC(20, 6) DEFAULT NULL, ADD sell_price NUMERIC(20, 6) DEFAULT NULL, ADD is_active TINYINT(1) DEFAULT 1 NOT NULL');
$this->addSql('CREATE INDEX reference ON products_bundle__declination_context (reference)');
$this->addSql('CREATE INDEX wholesale_price ON products_bundle__declination_context (wholesale_price)');
$this->addSql('CREATE INDEX sell_price ON products_bundle__declination_context (sell_price)');
$this->addSql('CREATE INDEX is_active ON products_bundle__declination_context (is_active)');
$this->addSql('ALTER TABLE products_bundle__product_context ADD eco_part_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD tax_rule_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD currency_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD name VARCHAR(255) DEFAULT \'\' NOT NULL, ADD description LONGTEXT DEFAULT NULL, ADD summary LONGTEXT DEFAULT NULL, ADD reference VARCHAR(255) DEFAULT NULL, ADD eco_part_amount NUMERIC(20, 6) DEFAULT NULL, ADD changeable_tax_rule TINYINT(1) DEFAULT 0 NOT NULL, ADD wholesale_price NUMERIC(20, 6) DEFAULT NULL, ADD sell_price NUMERIC(20, 6) DEFAULT NULL, ADD is_active TINYINT(1) DEFAULT 1 NOT NULL, ADD source VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE products_bundle__product_context ADD CONSTRAINT FK_F37E0B8CAE870F68 FOREIGN KEY (eco_part_id) REFERENCES products_bundle__eco_part (id)');
$this->addSql('ALTER TABLE products_bundle__product_context ADD CONSTRAINT FK_F37E0B8C3506A35B FOREIGN KEY (tax_rule_id) REFERENCES tax_rule (id)');
$this->addSql('ALTER TABLE products_bundle__product_context ADD CONSTRAINT FK_F37E0B8C38248176 FOREIGN KEY (currency_id) REFERENCES currency (id)');
$this->addSql('CREATE INDEX IDX_F37E0B8CAE870F68 ON products_bundle__product_context (eco_part_id)');
$this->addSql('CREATE INDEX IDX_F37E0B8C3506A35B ON products_bundle__product_context (tax_rule_id)');
$this->addSql('CREATE INDEX IDX_F37E0B8C38248176 ON products_bundle__product_context (currency_id)');
$this->addSql('CREATE INDEX reference ON products_bundle__product_context (reference)');
$this->addSql('CREATE INDEX name ON products_bundle__product_context (name)');
$this->addSql('CREATE INDEX wholesale_price ON products_bundle__product_context (wholesale_price)');
$this->addSql('CREATE INDEX sell_price ON products_bundle__product_context (sell_price)');
$this->addSql('CREATE INDEX is_active ON products_bundle__product_context (is_active)');
$this->addSql('ALTER TABLE products_bundle__declination_context ADD created_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD updated_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD deleted_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD created_at DATETIME DEFAULT NULL, ADD updated_at DATETIME DEFAULT NULL, ADD deleted_at DATETIME DEFAULT NULL');
$this->addSql('ALTER TABLE products_bundle__declination_context ADD CONSTRAINT FK_B1F3A380B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE products_bundle__declination_context ADD CONSTRAINT FK_B1F3A380896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE products_bundle__declination_context ADD CONSTRAINT FK_B1F3A380C76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
$this->addSql('CREATE INDEX IDX_B1F3A380B03A8386 ON products_bundle__declination_context (created_by_id)');
$this->addSql('CREATE INDEX IDX_B1F3A380896DBBDE ON products_bundle__declination_context (updated_by_id)');
$this->addSql('CREATE INDEX IDX_B1F3A380C76F1F52 ON products_bundle__declination_context (deleted_by_id)');
$this->addSql('ALTER TABLE products_bundle__product_context ADD created_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD updated_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD deleted_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD created_at DATETIME DEFAULT NULL, ADD updated_at DATETIME DEFAULT NULL, ADD deleted_at DATETIME DEFAULT NULL');
$this->addSql('ALTER TABLE products_bundle__product_context ADD CONSTRAINT FK_F37E0B8CB03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE products_bundle__product_context ADD CONSTRAINT FK_F37E0B8C896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE products_bundle__product_context ADD CONSTRAINT FK_F37E0B8CC76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
$this->addSql('CREATE INDEX IDX_F37E0B8CB03A8386 ON products_bundle__product_context (created_by_id)');
$this->addSql('CREATE INDEX IDX_F37E0B8C896DBBDE ON products_bundle__product_context (updated_by_id)');
$this->addSql('CREATE INDEX IDX_F37E0B8CC76F1F52 ON products_bundle__product_context (deleted_by_id)');
$this->addSql('UPDATE products_bundle__declination_context SET created_at = CURRENT_TIMESTAMP WHERE created_at IS NULL');
$this->addSql('UPDATE products_bundle__declination_context SET updated_at = CURRENT_TIMESTAMP WHERE updated_at IS NULL');
$this->addSql('UPDATE products_bundle__product_context SET created_at = CURRENT_TIMESTAMP WHERE created_at IS NULL');
$this->addSql('UPDATE products_bundle__product_context SET updated_at = CURRENT_TIMESTAMP WHERE updated_at IS NULL');
$this->addSql('ALTER TABLE products_bundle__declination_context CHANGE created_at created_at DATETIME NOT NULL, CHANGE updated_at updated_at DATETIME NOT NULL');
$this->addSql('ALTER TABLE products_bundle__product_context CHANGE created_at created_at DATETIME NOT NULL, CHANGE updated_at updated_at DATETIME NOT NULL');
$this->addSql('ALTER TABLE products_bundle__product_translations DROP FOREIGN KEY FK_9017C31B232D562B');
$this->addSql('ALTER TABLE products_bundle__product_translations ADD CONSTRAINT FK_9017C31B232D562B FOREIGN KEY (object_id) REFERENCES products_bundle__product_context (id) ON DELETE CASCADE');
$this->addSql('SET FOREIGN_KEY_CHECKS=1');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$conn = $this->connection;
$contexts = $conn->executeQuery("SELECT id FROM context")->fetchAllAssociative();
echo "debut declinations \n";
$declinations = $conn->executeQuery('SELECT id, reference, wholesale_price, sell_price, is_active FROM products_bundle__declination WHERE deleted_at IS NULL')->fetchAllAssociative();
foreach ($declinations as $decli) {
$keys = 'created_at, updated_at, ';
$values = '\'2023-01-01 00:00:00\', \'2023-01-01 00:00:00\', ';
foreach ($decli as $key => $el) {
$keys .= ($key == 'id' ? 'declination_id' : $key) . ', ';
if ($key == 'id') {
$value = UuidV6::fromBinary($el)->toRfc4122();
$value = '0x' . str_replace('-', '', $value);
} elseif (!$el) {
$value = 'NULL';
} else {
$value = "'" . $el . "'";
}
$values .= $value . ', ';
}
foreach ($contexts as $c) {
$id = (new UuidV6())->toRfc4122();
$cId = UuidV6::fromBinary($c['id'])->toRfc4122();
$query = 'INSERT INTO products_bundle__declination_context (' . $keys . 'context_id, id) VALUES (' . $values . '0x' . str_replace('-', '', $cId) . ', 0x' . str_replace('-', '', $id) . ')';
$conn->executeStatement($query);
}
}
echo "fin declinations \n";
// partie products
echo "debut products \n";
$products = $conn->executeQuery('SELECT id, name, summary, description, source, reference, eco_part_id, eco_part_amount, tax_rule_id, changeable_tax_rule, currency_id, wholesale_price, sell_price, is_active FROM products_bundle__product WHERE deleted_at IS NULL')->fetchAllAssociative();
foreach ($products as $p) {
$p_uuid = UuidV6::fromBinary($p['id'])->toRfc4122();
$keys = 'created_at, updated_at, ';
$values = '\'2023-01-01 00:00:00\', \'2023-01-01 00:00:00\', ';
foreach ($p as $key => $el) {
$keys .= ($key == 'id' ? 'product_id' : $key) . ', ';
if ($key == 'id') {
$value = UuidV6::fromBinary($el)->toRfc4122();
$value = '0x' . str_replace('-', '', $value);
} elseif (!$el) {
$value = 'NULL';
} elseif (str_contains($key, '_id')) {
$value = UuidV6::fromBinary($el)->toRfc4122();
$value = '0x' . str_replace('-', '', $value);
} else {
$el = addslashes($el);
$value = "'" . $el . "'";
}
$values .= $value . ', ';
}
foreach ($contexts as $c) {
$id = (new UuidV6())->toRfc4122();
$cId = UuidV6::fromBinary($c['id'])->toRfc4122();
$query = 'INSERT INTO products_bundle__product_context (' . $keys . 'context_id, id) VALUES (' . $values . '0x' . str_replace('-', '', $cId) . ', 0x' . str_replace('-', '', $id) . ')';
$conn->executeStatement($query);
}
// traitement traductions
$productTranslations = $conn->executeQuery("SELECT id, object_id, locale, field FROM products_bundle__product_translations WHERE object_id = UNHEX(CONCAT('', REPLACE('" . $p_uuid . "', '-', '')))")->fetchAllAssociative();
$products_contexts = $conn->executeQuery("SELECT id, name, summary, description, source FROM products_bundle__product_context WHERE product_id = UNHEX(CONCAT('', REPLACE('" . $p_uuid . "', '-', '')))")->fetchAllAssociative();
$first = true;
foreach ($products_contexts as $pc) {
$pc_uuid = UuidV6::fromBinary($pc['id'])->toRfc4122();
$pc_uuid = '0x' . str_replace('-', '', $pc_uuid);
$name_fr = false;
$summary_fr = false;
$description_fr = false;
$source_fr = false;
if ($first) {
foreach ($productTranslations as $pt) {
if ($pt['locale'] == 'fr') {
if ($pt['field'] == 'name') {
$name_fr = true;
} elseif ($pt['field'] == 'summary') {
$summary_fr = true;
} elseif ($pt['field'] == 'description') {
$description_fr = true;
} elseif ($pt['field'] == 'source') {
$source_fr = true;
}
}
$pt_id = $pt['id'];
$conn->executeStatement("UPDATE products_bundle__product_translations SET object_id = " . $pc_uuid . " WHERE id = " . $pt_id);
}
$first = false;
}
if (!$name_fr) {
$value = $pc['name'];
if (!$value) {
$value = 'NULL';
} else {
$value = addslashes($value);
$value = "'" . $value . "'";
}
$conn->executeStatement("INSERT INTO products_bundle__product_translations (id, object_id, locale, field, content) VALUES (NULL, " . $pc_uuid . ", 'fr', 'name', " . $value . ")");
}
if (!$summary_fr) {
$value = $pc['summary'];
if (!$value) {
$value = 'NULL';
} else {
$value = addslashes($value);
$value = "'" . $value . "'";
}
$conn->executeStatement("INSERT INTO products_bundle__product_translations (id, object_id, locale, field, content) VALUES (NULL, " . $pc_uuid . ", 'fr', 'summary', " . $value . ")");
}
if (!$description_fr) {
$value = $pc['description'];
if (!$value) {
$value = 'NULL';
} else {
$value = addslashes($value);
$value = "'" . $value . "'";
}
$conn->executeStatement("INSERT INTO products_bundle__product_translations (id, object_id, locale, field, content) VALUES (NULL, " . $pc_uuid . ", 'fr', 'description', " . $value . ")");
}
if (!$source_fr) {
$value = $pc['source'];
if (!$value) {
$value = 'NULL';
} else {
$value = addslashes($value);
$value = "'" . $value . "'";
}
$conn->executeStatement("INSERT INTO products_bundle__product_translations (id, object_id, locale, field, content) VALUES (NULL, " . $pc_uuid . ", 'fr', 'source', " . $value . ")");
}
}
}
echo "fin products \n";
}
public function down(Schema $schema): void
{
}
}