From 57b576c11f222800a1beacc628417d7c52bfcfa9 Mon Sep 17 00:00:00 2001 From: Laurent Petard Date: Mon, 15 Jan 2018 15:00:39 +0100 Subject: [PATCH 1/2] Fix DSN construction for MySQL connection --- src/Infrastructure/Cli/LocalMySqlQueryExecutor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Infrastructure/Cli/LocalMySqlQueryExecutor.php b/src/Infrastructure/Cli/LocalMySqlQueryExecutor.php index 3d8b1ec..b512bf9 100644 --- a/src/Infrastructure/Cli/LocalMySqlQueryExecutor.php +++ b/src/Infrastructure/Cli/LocalMySqlQueryExecutor.php @@ -38,7 +38,7 @@ public function query(string $sql, Pim $pim): array public function getConnection(Pim $pim): \PDO { $dsn = sprintf( - 'mysql: host=%s;dbname=%s;port=%s', + 'mysql:host=%s;dbname=%s;port=%s', $pim->getMysqlHost(), $pim->getDatabaseName(), strval($pim->getMysqlPort()) From 630d23daf6ea329f645939b3be1aebd9a9b0997a Mon Sep 17 00:00:00 2001 From: Laurent Petard Date: Tue, 16 Jan 2018 14:46:22 +0100 Subject: [PATCH 2/2] PIM-7114: All the products of an inner variation type family must be migrated as product models --- .../FamilyRepository.php | 23 ++++++++++- .../InnerVariationFamilyMigrator.php | 10 ++++- .../InnerVariationProductMigrator.php | 16 ++++---- .../InnerVariationTypeRepository.php | 38 ------------------- .../ProductRepository.php | 11 ++---- .../InnerVariationFamilyMigratorSpec.php | 10 +++-- .../InnerVariationProductMigratorSpec.php | 12 +++--- 7 files changed, 55 insertions(+), 65 deletions(-) diff --git a/src/Domain/MigrationStep/s150_ProductVariationMigration/FamilyRepository.php b/src/Domain/MigrationStep/s150_ProductVariationMigration/FamilyRepository.php index 40c61e0..f6781c9 100644 --- a/src/Domain/MigrationStep/s150_ProductVariationMigration/FamilyRepository.php +++ b/src/Domain/MigrationStep/s150_ProductVariationMigration/FamilyRepository.php @@ -8,7 +8,7 @@ use Akeneo\PimMigration\Domain\Command\ChainedConsole; use Akeneo\PimMigration\Domain\Command\MySqlQueryCommand; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\Family; -use Akeneo\PimMigration\Domain\Pim\DestinationPim; +use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\InnerVariationType; use Akeneo\PimMigration\Domain\Pim\Pim; /** @@ -68,4 +68,25 @@ public function findById(int $familyId, Pim $pim): Family return $family; } + + public function findAllByInnerVariationType(InnerVariationType $innerVariationType, Pim $pim) + { + $sqlResult = $this->console->execute( + new MySqlQueryCommand(sprintf( + 'SELECT DISTINCT f.code, f.id + FROM pim_inner_variation_inner_variation_type ivt + INNER JOIN pim_inner_variation_inner_variation_type_family ivtf ON ivtf.inner_variation_type_id = ivt.id + INNER JOIN pim_catalog_family f ON f.id = ivtf.family_id + WHERE ivt.id = %d', + $innerVariationType->getId() + )), $pim + )->getOutput(); + + foreach ($sqlResult as $sqlResultLine) { + $familyData = $this->console->execute(new GetFamilyCommand($sqlResultLine['code']), $pim)->getOutput(); + $family = new Family((int) $sqlResultLine['id'], $sqlResultLine['code'], $familyData); + + yield $family; + } + } } diff --git a/src/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationFamilyMigrator.php b/src/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationFamilyMigrator.php index 01744c2..578c0eb 100644 --- a/src/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationFamilyMigrator.php +++ b/src/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationFamilyMigrator.php @@ -9,6 +9,7 @@ use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\Family; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\FamilyVariant; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\InnerVariationType; +use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\FamilyRepository; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\FamilyVariantImporter; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\FamilyVariantRepository; use Akeneo\PimMigration\Domain\Pim\Pim; @@ -39,24 +40,29 @@ class InnerVariationFamilyMigrator /** @var FamilyVariantRepository */ private $familyVariantRepository; + /** @var FamilyRepository */ + private $familyRepository; + public function __construct( InnerVariationTypeRepository $innerVariationTypeRepository, FamilyVariantImporter $familyVariantImporter, ChainedConsole $console, LoggerInterface $logger, - FamilyVariantRepository $familyVariantRepository + FamilyVariantRepository $familyVariantRepository, + FamilyRepository $familyRepository ) { $this->innerVariationTypeRepository = $innerVariationTypeRepository; $this->familyVariantImporter = $familyVariantImporter; $this->logger = $logger; $this->console = $console; $this->familyVariantRepository = $familyVariantRepository; + $this->familyRepository = $familyRepository; } public function migrate(InnerVariationType $innerVariationType, Pim $pim): void { $innerVariationFamily = $innerVariationType->getVariationFamily(); - $parentFamilies = $this->innerVariationTypeRepository->getParentFamiliesHavingVariantProducts($innerVariationType, $pim); + $parentFamilies = $this->familyRepository->findAllByInnerVariationType($innerVariationType, $pim); foreach ($parentFamilies as $parentFamily) { $this->migrateFamilyAttributes($parentFamily, $innerVariationFamily, $pim); diff --git a/src/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationProductMigrator.php b/src/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationProductMigrator.php index 5d294c0..4dff17c 100644 --- a/src/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationProductMigrator.php +++ b/src/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationProductMigrator.php @@ -7,6 +7,7 @@ use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\Family; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\FamilyVariant; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\InnerVariationType; +use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\FamilyRepository; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\FamilyVariantRepository; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\ProductModelRepository; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\ProductRepository; @@ -24,8 +25,8 @@ */ class InnerVariationProductMigrator { - /** @var InnerVariationTypeRepository */ - private $innerVariationTypeRepository; + /** @var FamilyRepository */ + private $familyRepository; /** @var LoggerInterface */ private $logger; @@ -45,7 +46,7 @@ class InnerVariationProductMigrator private $familyVariantRepository; public function __construct( - InnerVariationTypeRepository $innerVariationTypeRepository, + FamilyRepository $familyRepository, LoggerInterface $logger, ProductRepository $productRepository, ProductModelBuilder $builder, @@ -53,7 +54,7 @@ public function __construct( ProductVariantTransformer $productVariantTransformer, FamilyVariantRepository $familyVariantRepository ) { - $this->innerVariationTypeRepository = $innerVariationTypeRepository; + $this->familyRepository = $familyRepository; $this->logger = $logger; $this->productRepository = $productRepository; $this->productModelBuilder = $builder; @@ -68,7 +69,7 @@ public function __construct( public function migrate(InnerVariationType $innerVariationType, Pim $pim): void { $innerVariationFamily = $innerVariationType->getVariationFamily(); - $parentFamilies = $this->innerVariationTypeRepository->getParentFamiliesHavingVariantProducts($innerVariationType, $pim); + $parentFamilies = $this->familyRepository->findAllByInnerVariationType($innerVariationType, $pim); foreach ($parentFamilies as $parentFamily) { $familyVariant = $this->familyVariantRepository->findOneByCode($parentFamily->getCode(), $pim); @@ -88,7 +89,7 @@ private function migrateFamilyVariantProducts( Pim $pim ): void { - $productsModels = $this->createProductModels($parentFamily, $innerVariationFamily, $familyVariant, $pim); + $productsModels = $this->createProductModels($parentFamily, $familyVariant, $pim); foreach ($productsModels as $productModel) { $this->productModelRepository->updateRawValuesAndCreatedForProduct($productModel, $pim); @@ -102,12 +103,11 @@ private function migrateFamilyVariantProducts( */ private function createProductModels( Family $parentFamily, - Family $innerVariationFamily, FamilyVariant $familyVariant, Pim $pim ): array { - $products = $this->productRepository->findAllHavingVariantsForIvb($parentFamily->getId(), $innerVariationFamily->getId(), $pim); + $products = $this->productRepository->findAllByFamily($parentFamily, $pim); $productsModels = []; foreach ($products as $product) { diff --git a/src/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationTypeRepository.php b/src/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationTypeRepository.php index fdcfcb8..ec1e7e1 100644 --- a/src/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationTypeRepository.php +++ b/src/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationTypeRepository.php @@ -94,33 +94,6 @@ public function delete(InnerVariationType $innerVariationType, DestinationPim $p } } - /** - * Retrieves parent families having variant products related to an InnerVariationType. - */ - public function getParentFamiliesHavingVariantProducts(InnerVariationType $innerVariationType, Pim $pim): \Traversable - { - $parentFamiliesData = $this->console->execute( - new MySqlQueryCommand(sprintf( - 'SELECT DISTINCT f.code, f.id - FROM pim_inner_variation_inner_variation_type ivt - INNER JOIN pim_inner_variation_inner_variation_type_family ivtf ON ivtf.inner_variation_type_id = ivt.id - INNER JOIN pim_catalog_family f ON f.id = ivtf.family_id - INNER JOIN pim_catalog_product product_model ON product_model.family_id = f.id - WHERE ivt.id = %d - AND EXISTS( - SELECT * FROM pim_catalog_product AS product_variant - WHERE product_variant.family_id = ivt.variation_family_id - AND JSON_EXTRACT(product_variant.raw_values, \'$.variation_parent_product."".""\') = product_model.identifier - )', - $innerVariationType->getId() - )), $pim - )->getOutput(); - - foreach ($parentFamiliesData as $parentFamilyData) { - yield $this->buildFamily((int) $parentFamilyData['id'], $parentFamilyData['code'], $pim); - } - } - /** * Retrieves the label of an InnerVariationType for a given locale. */ @@ -178,15 +151,4 @@ private function getAxes(int $innerVariationTypeId, Pim $pim): array $pim )->getOutput(); } - - /** - * Retrieves all the data of a family. - */ - private function buildFamily(int $familyId, string $familyCode, Pim $pim): Family - { - $apiCommand = new GetFamilyCommand($familyCode); - $familyStandardData = $this->console->execute($apiCommand, $pim)->getOutput(); - - return new Family($familyId, $familyCode, $familyStandardData); - } } diff --git a/src/Domain/MigrationStep/s150_ProductVariationMigration/ProductRepository.php b/src/Domain/MigrationStep/s150_ProductVariationMigration/ProductRepository.php index b2b1af2..3e8a406 100644 --- a/src/Domain/MigrationStep/s150_ProductVariationMigration/ProductRepository.php +++ b/src/Domain/MigrationStep/s150_ProductVariationMigration/ProductRepository.php @@ -8,6 +8,7 @@ use Akeneo\PimMigration\Domain\Command\Api\GetProductCommand; use Akeneo\PimMigration\Domain\Command\ChainedConsole; use Akeneo\PimMigration\Domain\Command\MySqlQueryCommand; +use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\Family; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\Product; use Akeneo\PimMigration\Domain\Pim\Pim; @@ -41,15 +42,11 @@ public function getCategoryCodes(int $productId, Pim $pim): array return $categories; } - public function findAllHavingVariantsForIvb(int $parentFamilyId, int $innerVariationFamilyId, Pim $pim): \Traversable + public function findAllByFamily(Family $family, Pim $pim): \Traversable { $sqlResults = $this->console->execute(new MySqlQueryCommand(sprintf( - 'SELECT id, identifier FROM pim_catalog_product AS product_model - WHERE family_id = %d AND EXISTS( - SELECT * FROM pim_catalog_product AS product_variant - WHERE product_variant.family_id = %d - AND JSON_EXTRACT(product_variant.raw_values, \'$.variation_parent_product."".""\') = product_model.identifier - );', $parentFamilyId, $innerVariationFamilyId) + 'SELECT id, identifier FROM pim_catalog_product WHERE family_id = %d', + $family->getId()) ), $pim)->getOutput(); foreach ($sqlResults as $result) { diff --git a/tests/spec/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationFamilyMigratorSpec.php b/tests/spec/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationFamilyMigratorSpec.php index 88f15b9..02879bb 100644 --- a/tests/spec/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationFamilyMigratorSpec.php +++ b/tests/spec/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationFamilyMigratorSpec.php @@ -9,6 +9,7 @@ use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\Family; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\FamilyVariant; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\InnerVariationType; +use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\FamilyRepository; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\FamilyVariantImporter; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\FamilyVariantRepository; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\InnerVariation\InnerVariationFamilyMigrator; @@ -28,7 +29,8 @@ function let( FamilyVariantImporter $familyVariantImporter, ChainedConsole $console, LoggerInterface $logger, - FamilyVariantRepository $familyVariantRepository + FamilyVariantRepository $familyVariantRepository, + FamilyRepository $familyRepository ) { $this->beConstructedWith( @@ -36,7 +38,8 @@ function let( $familyVariantImporter, $console, $logger, - $familyVariantRepository + $familyVariantRepository, + $familyRepository ); } @@ -47,6 +50,7 @@ function it_is_initializable() function it_successfully_migrates_families( $innerVariationTypeRepository, + $familyRepository, InnerVariationType $innerVariationType, DestinationPim $pim, FamilyVariantRepository $familyVariantRepository, @@ -93,7 +97,7 @@ function it_successfully_migrates_families( ] ]); - $innerVariationTypeRepository->getParentFamiliesHavingVariantProducts($innerVariationType, $pim)->willReturn(new \ArrayObject([$firstParentFamily, $secondParentFamily])); + $familyRepository->findAllByInnerVariationType($innerVariationType, $pim)->willReturn(new \ArrayObject([$firstParentFamily, $secondParentFamily])); $console->execute(new UpdateFamilyCommand([ 'code' => 'first_parent_family', diff --git a/tests/spec/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationProductMigratorSpec.php b/tests/spec/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationProductMigratorSpec.php index 3f04a3a..616b575 100644 --- a/tests/spec/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationProductMigratorSpec.php +++ b/tests/spec/Domain/MigrationStep/s150_ProductVariationMigration/InnerVariation/InnerVariationProductMigratorSpec.php @@ -9,9 +9,9 @@ use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\InnerVariationType; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\Product; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\Entity\ProductModel; +use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\FamilyRepository; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\FamilyVariantRepository; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\InnerVariation\InnerVariationProductMigrator; -use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\InnerVariation\InnerVariationTypeRepository; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\InnerVariation\ProductModelBuilder; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\InnerVariation\ProductVariantTransformer; use Akeneo\PimMigration\Domain\MigrationStep\s150_ProductVariationMigration\ProductModelRepository; @@ -27,7 +27,7 @@ class InnerVariationProductMigratorSpec extends ObjectBehavior { function let( - InnerVariationTypeRepository $innerVariationTypeRepository, + FamilyRepository $familyRepository, LoggerInterface $logger, ProductRepository $productRepository, ProductModelBuilder $productModelBuilder, @@ -37,7 +37,7 @@ function let( ) { $this->beConstructedWith( - $innerVariationTypeRepository, + $familyRepository, $logger, $productRepository, $productModelBuilder, @@ -53,7 +53,7 @@ function it_is_initializable() } function it_successfully_migrates_products( - $innerVariationTypeRepository, + $familyRepository, $productModelRepository, $familyVariantRepository, $productRepository, @@ -67,14 +67,14 @@ function it_successfully_migrates_products( $parentFamily = new Family(10, 'first_parent_family', []); $innerVariationType->getVariationFamily()->willReturn($innerVariationFamily); - $innerVariationTypeRepository->getParentFamiliesHavingVariantProducts($innerVariationType, $pim)->willReturn(new \ArrayObject([$parentFamily])); + $familyRepository->findAllByInnerVariationType($innerVariationType, $pim)->willReturn(new \ArrayObject([$parentFamily])); $familyVariant = new FamilyVariant(20, 'first_family_variant'); $familyVariantRepository->findOneByCode('first_parent_family', $pim)->willReturn($familyVariant); $product1 = new Product(110, 'product_model_1', null, null, null); - $productRepository->findAllHavingVariantsForIvb(10, 1, $pim)->willReturn(new \ArrayObject([$product1])); + $productRepository->findAllByFamily($parentFamily, $pim)->willReturn(new \ArrayObject([$product1])); $productRepository->getCategoryCodes(110, $pim)->willReturn(['cat_1', 'cat_2']);