Skip to content

Commit

Permalink
Merge pull request #866 from magento-tsg/MAGETWO-62025-develop
Browse files Browse the repository at this point in the history
[TSG] MAGETWO-62025: Upgrading from 2.0.11 with Sample Data fails
  • Loading branch information
Alexander Akimov authored Mar 2, 2017
2 parents a005167 + 5c8ea22 commit c993777
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions app/code/Magento/Catalog/Setup/UpgradeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Model\Entity\AttributeCache;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
Expand Down Expand Up @@ -34,16 +35,26 @@ class UpgradeData implements UpgradeDataInterface
*/
private $eavSetupFactory;

/**
* @var AttributeCache
*/
private $attributeCache;

/**
* Init
*
* @param CategorySetupFactory $categorySetupFactory
* @param EavSetupFactory $eavSetupFactory
* @param AttributeCache $attributeCache
*/
public function __construct(CategorySetupFactory $categorySetupFactory, EavSetupFactory $eavSetupFactory)
{
public function __construct(
CategorySetupFactory $categorySetupFactory,
EavSetupFactory $eavSetupFactory,
AttributeCache $attributeCache
) {
$this->categorySetupFactory = $categorySetupFactory;
$this->eavSetupFactory = $eavSetupFactory;
$this->attributeCache = $attributeCache;
}

/**
Expand Down Expand Up @@ -135,19 +146,24 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
}

if (version_compare($context->getVersion(), '2.0.4') < 0) {
$mediaBackendType = 'static';
$mediaBackendModel = null;
/** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
$categorySetup->updateAttribute(
'catalog_product',
'media_gallery',
'backend_type',
'static'
$mediaBackendType
);
$categorySetup->updateAttribute(
'catalog_product',
'media_gallery',
'backend_model'
'backend_model',
$mediaBackendModel
);

$this->changeMediaGalleryAttributeInCache($mediaBackendType, $mediaBackendModel);
}

if (version_compare($context->getVersion(), '2.0.5', '<')) {
Expand Down Expand Up @@ -340,10 +356,10 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
]
);
}

if (version_compare($context->getVersion(), '2.0.7') < 0) {
/** @var EavSetup $eavSetup */
$eavSetup= $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$eavSetup->updateAttribute(
ProductAttributeInterface::ENTITY_TYPE_CODE,
Expand Down Expand Up @@ -381,4 +397,25 @@ private function changePriceAttributeDefaultScope($categorySetup)

}
}

/**
* @param string $mediaBackendType
* @param string $mediaBackendModel
* @return void
*/
private function changeMediaGalleryAttributeInCache($mediaBackendType, $mediaBackendModel)
{
// need to do, because media_gallery has backend model in cache.
$catalogProductAttributes = $this->attributeCache->getAttributes('catalog_product', '0-0');

if (is_array($catalogProductAttributes)) {
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $catalogProductAttribute */
foreach ($catalogProductAttributes as $catalogProductAttribute) {
if ($catalogProductAttribute->getAttributeCode() == 'media_gallery') {
$catalogProductAttribute->setBackendModel($mediaBackendModel);
$catalogProductAttribute->setBackendType($mediaBackendType);
}
}
}
}
}

0 comments on commit c993777

Please sign in to comment.