Skip to content

Commit 806ad0c

Browse files
PB-357: POC for Page builder upgrade script
1 parent f41f238 commit 806ad0c

File tree

5 files changed

+237
-0
lines changed

5 files changed

+237
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\PageBuilder\Model;
7+
8+
/**
9+
* Class EntityPool
10+
*
11+
* Pool of entities
12+
*/
13+
class UpgradableEntitiesPool
14+
{
15+
/**
16+
* @var array
17+
*/
18+
private $entities;
19+
20+
/**
21+
* @param array $entities
22+
*/
23+
public function __construct(array $entities = [])
24+
{
25+
$this->entities = $entities;
26+
}
27+
28+
/**
29+
* Retrieve entities
30+
*
31+
* @return array
32+
*/
33+
public function getEntities()
34+
{
35+
return $this->entities;
36+
}
37+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\PageBuilder\Setup\Converters;
7+
8+
use Magento\Framework\DB\DataConverter\DataConverterInterface;
9+
10+
class MoveAttribute implements DataConverterInterface
11+
{
12+
/**
13+
* @inheritDoc
14+
*/
15+
public function convert($value) {
16+
//Perform content conversion
17+
return $value;
18+
}
19+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\PageBuilder\Setup\Patch\Data;
7+
8+
use Magento\Framework\Setup\Patch\DataPatchInterface;
9+
use Magento\Framework\DB\FieldDataConversionException;
10+
use Magento\PageBuilder\Setup\UpgradeContentHelper;
11+
use Magento\PageBuilder\Setup\Converters\MoveAttribute;
12+
13+
/**
14+
* Patch is mechanism, that allows to do atomic upgrade data changes
15+
*/
16+
class UpdateContent implements DataPatchInterface
17+
{
18+
/**
19+
* @var UpgradeContentHelper
20+
*/
21+
private $helper;
22+
23+
/**
24+
* @param UpgradeContentHelper $helper
25+
*/
26+
public function __construct(
27+
UpgradeContentHelper $helper
28+
) {
29+
$this->helper = $helper;
30+
}
31+
32+
/**
33+
* Do Upgrade
34+
*
35+
* @return void
36+
*/
37+
/**
38+
* @return DataPatchInterface|void
39+
* @throws FieldDataConversionException
40+
*/
41+
public function apply()
42+
{
43+
$this->helper->upgrade([
44+
MoveAttribute::class
45+
]);
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function getAliases()
52+
{
53+
return [];
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public static function getDependencies()
60+
{
61+
return [];
62+
}
63+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\PageBuilder\Setup;
7+
8+
use Magento\Framework\DB\Select\QueryModifierFactory;
9+
use Magento\Framework\DB\FieldToConvert;
10+
use Magento\Framework\Setup\ModuleDataSetupInterface;
11+
use Magento\Framework\DB\AggregatedFieldDataConverter;
12+
use Magento\Framework\DB\FieldDataConversionException;
13+
use Magento\PageBuilder\Model\UpgradableEntitiesPool;
14+
15+
class UpgradeContentHelper
16+
{
17+
public const PAGE_BUILDER_CONTENT_PATTERN = '%data-content-type="%';
18+
/**
19+
* @var ModuleDataSetupInterface $moduleDataSetup
20+
*/
21+
private $moduleDataSetup;
22+
23+
/**
24+
* @var QueryModifierFactory
25+
*/
26+
private $queryModifierFactory;
27+
28+
/**
29+
* @var UpgradableEntitiesPool
30+
*/
31+
private $entitiesPool;
32+
33+
/**
34+
* @var AggregatedFieldDataConverter
35+
*/
36+
private $aggregatedFieldDataConverter;
37+
38+
/**
39+
* UpdateContent constructor.
40+
* @param ModuleDataSetupInterface $moduleDataSetup
41+
* @param QueryModifierFactory $queryModifierFactory
42+
* @param UpgradableEntitiesPool $entitiesPool
43+
* @param AggregatedFieldDataConverter $aggregatedFieldDataConverter
44+
*/
45+
public function __construct(
46+
ModuleDataSetupInterface $moduleDataSetup,
47+
QueryModifierFactory $queryModifierFactory,
48+
UpgradableEntitiesPool $entitiesPool,
49+
AggregatedFieldDataConverter $aggregatedFieldDataConverter
50+
) {
51+
$this->moduleDataSetup = $moduleDataSetup;
52+
$this->queryModifierFactory = $queryModifierFactory;
53+
$this->entitiesPool = $entitiesPool;
54+
$this->aggregatedFieldDataConverter = $aggregatedFieldDataConverter;
55+
}
56+
57+
/**
58+
* @param array $converters
59+
*/
60+
/**
61+
* @param array $converters
62+
* @throws FieldDataConversionException
63+
*/
64+
public function upgrade(array $converters): void
65+
{
66+
if (count($converters)) {
67+
$fields = [];
68+
69+
foreach ($this->entitiesPool->getEntities() as $tableName => $tableInfo) {
70+
foreach ($tableInfo['fields'] as $fieldName => $upgradeField) {
71+
if (!$upgradeField) {
72+
continue;
73+
}
74+
75+
$queryModifier = $this->queryModifierFactory->create(
76+
'like',
77+
[
78+
'values' => [
79+
$fieldName => self::PAGE_BUILDER_CONTENT_PATTERN
80+
]
81+
]
82+
);
83+
84+
foreach ($converters as $converter) {
85+
$fields[] = new FieldToConvert(
86+
$converter,
87+
$this->moduleDataSetup->getTable($tableName),
88+
$tableInfo['identifier'],
89+
$fieldName,
90+
$queryModifier
91+
);
92+
}
93+
94+
}
95+
}
96+
97+
$this->aggregatedFieldDataConverter->convert($fields, $this->moduleDataSetup->getConnection());
98+
}
99+
}
100+
}

app/code/Magento/PageBuilder/etc/di.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,22 @@
254254
<type name="Magento\Framework\Filter\Template">
255255
<plugin name="convertBackgroundImages" type="Magento\PageBuilder\Plugin\Filter\TemplatePlugin"/>
256256
</type>
257+
<type name="Magento\PageBuilder\Model\UpgradableEntitiesPool">
258+
<arguments>
259+
<argument name="entities" xsi:type="array">
260+
<item name="cms_block" xsi:type="array">
261+
<item name="identifier" xsi:type="string">block_id</item>
262+
<item name="fields" xsi:type="array">
263+
<item name="content" xsi:type="boolean">true</item>
264+
</item>
265+
</item>
266+
<item name="cms_page" xsi:type="array">
267+
<item name="identifier" xsi:type="string">page_id</item>
268+
<item name="fields" xsi:type="array">
269+
<item name="content" xsi:type="boolean">true</item>
270+
</item>
271+
</item>
272+
</argument>
273+
</arguments>
274+
</type>
257275
</config>

0 commit comments

Comments
 (0)