<?php
declare(strict_types=1);
namespace ProductsBundleMigrations;
use DateTime;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Uid\UuidV6;
use Doctrine\Migrations\AbstractMigration;
final class Version20230206152033 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE products_bundle__pack_product (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', parent_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', child_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)\', position INT NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, INDEX IDX_D52AD2E0727ACA70 (parent_id), INDEX IDX_D52AD2E0DD62C21B (child_id), INDEX IDX_D52AD2E0B03A8386 (created_by_id), INDEX IDX_D52AD2E0896DBBDE (updated_by_id), INDEX IDX_D52AD2E0C76F1F52 (deleted_by_id), INDEX position (position), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE products_bundle__pack_product ADD CONSTRAINT FK_D52AD2E0727ACA70 FOREIGN KEY (parent_id) REFERENCES products_bundle__product (id)');
$this->addSql('ALTER TABLE products_bundle__pack_product ADD CONSTRAINT FK_D52AD2E0DD62C21B FOREIGN KEY (child_id) REFERENCES products_bundle__product (id)');
$this->addSql('ALTER TABLE products_bundle__pack_product ADD CONSTRAINT FK_D52AD2E0B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE products_bundle__pack_product ADD CONSTRAINT FK_D52AD2E0896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE products_bundle__pack_product ADD CONSTRAINT FK_D52AD2E0C76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE products_bundle__pack_product DROP FOREIGN KEY FK_D52AD2E0727ACA70');
$this->addSql('ALTER TABLE products_bundle__pack_product DROP FOREIGN KEY FK_D52AD2E0DD62C21B');
$this->addSql('ALTER TABLE products_bundle__pack_product DROP FOREIGN KEY FK_D52AD2E0B03A8386');
$this->addSql('ALTER TABLE products_bundle__pack_product DROP FOREIGN KEY FK_D52AD2E0896DBBDE');
$this->addSql('ALTER TABLE products_bundle__pack_product DROP FOREIGN KEY FK_D52AD2E0C76F1F52');
$this->addSql('DROP TABLE products_bundle__pack_product');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$languages = $this->connection->executeQuery('SELECT * FROM language WHERE deleted_at IS NULL')
->fetchAllAssociative();
$rootLocale = 'en';
$defaultLocale = $this->connection
->executeQuery('SELECT locale FROM language WHERE deleted_at IS NULL AND is_default = 1')
->fetchOne() ?: 'en';
$productType = ['fr' => 'Pack', 'en' => 'Pack', 'key' => 3];
$productTypeId = (new UuidV6())->toBinary();
$this->connection->insert('products_bundle__product_type', [
'id' => $productTypeId,
'name' => !empty($productType[$defaultLocale]) ?
$productType[$defaultLocale] : $productType[$rootLocale],
'number' => $productType['key'],
'is_active' => 1,
'created_at' => (new DateTime())->format('Y-m-d H:i:s'),
'updated_at' => (new DateTime())->format('Y-m-d H:i:s')
]);
foreach ($languages as $language) {
$localeTo = $rootLocale;
if (!empty($productType[$language['locale']])) {
$localeTo = $language['locale'];
} elseif (!empty($productType[$defaultLocale])) {
$localeTo = $defaultLocale;
}
$this->connection->insert('products_bundle__product_type_translations', [
'object_id' => $productTypeId,
'locale' => $language['locale'],
'field' => 'name',
'content' => $productType[$localeTo]
]);
}
}
}