Skip to content

Commit

Permalink
Merge pull request #134 from akeneo/PIM-7114
Browse files Browse the repository at this point in the history
PIM-7114: Migration of products on IVB family
  • Loading branch information
LaurentPetard authored Jan 22, 2018
2 parents 2c7198b + 630d23d commit 99ad740
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,8 +25,8 @@
*/
class InnerVariationProductMigrator
{
/** @var InnerVariationTypeRepository */
private $innerVariationTypeRepository;
/** @var FamilyRepository */
private $familyRepository;

/** @var LoggerInterface */
private $logger;
Expand All @@ -45,15 +46,15 @@ class InnerVariationProductMigrator
private $familyVariantRepository;

public function __construct(
InnerVariationTypeRepository $innerVariationTypeRepository,
FamilyRepository $familyRepository,
LoggerInterface $logger,
ProductRepository $productRepository,
ProductModelBuilder $builder,
ProductModelRepository $productModelRepository,
ProductVariantTransformer $productVariantTransformer,
FamilyVariantRepository $familyVariantRepository
) {
$this->innerVariationTypeRepository = $innerVariationTypeRepository;
$this->familyRepository = $familyRepository;
$this->logger = $logger;
$this->productRepository = $productRepository;
$this->productModelBuilder = $builder;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."<all_channels>"."<all_locales>"\') = 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.
*/
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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."<all_channels>"."<all_locales>"\') = product_model.identifier
);', $parentFamilyId, $innerVariationFamilyId)
'SELECT id, identifier FROM pim_catalog_product WHERE family_id = %d',
$family->getId())
), $pim)->getOutput();

foreach ($sqlResults as $result) {
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Cli/LocalMySqlQueryExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,15 +29,17 @@ function let(
FamilyVariantImporter $familyVariantImporter,
ChainedConsole $console,
LoggerInterface $logger,
FamilyVariantRepository $familyVariantRepository
FamilyVariantRepository $familyVariantRepository,
FamilyRepository $familyRepository
)
{
$this->beConstructedWith(
$innerVariationTypeRepository,
$familyVariantImporter,
$console,
$logger,
$familyVariantRepository
$familyVariantRepository,
$familyRepository
);
}

Expand All @@ -47,6 +50,7 @@ function it_is_initializable()

function it_successfully_migrates_families(
$innerVariationTypeRepository,
$familyRepository,
InnerVariationType $innerVariationType,
DestinationPim $pim,
FamilyVariantRepository $familyVariantRepository,
Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +27,7 @@
class InnerVariationProductMigratorSpec extends ObjectBehavior
{
function let(
InnerVariationTypeRepository $innerVariationTypeRepository,
FamilyRepository $familyRepository,
LoggerInterface $logger,
ProductRepository $productRepository,
ProductModelBuilder $productModelBuilder,
Expand All @@ -37,7 +37,7 @@ function let(
)
{
$this->beConstructedWith(
$innerVariationTypeRepository,
$familyRepository,
$logger,
$productRepository,
$productModelBuilder,
Expand All @@ -53,7 +53,7 @@ function it_is_initializable()
}

function it_successfully_migrates_products(
$innerVariationTypeRepository,
$familyRepository,
$productModelRepository,
$familyVariantRepository,
$productRepository,
Expand All @@ -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']);

Expand Down

0 comments on commit 99ad740

Please sign in to comment.