Skip to content

Commit 3ed5711

Browse files
atuld123Atul Dusane
authored andcommitted
created data patch for updating source model value from Facebook to … (#274)
* created data patch for updating source model value from Facebook to Meta * removed echo statement --------- Co-authored-by: Atul Dusane <[email protected]>
1 parent 3dee042 commit 3ed5711

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace Meta\Catalog\Setup\Patch\Data;
4+
5+
use Magento\Framework\Setup\Patch\DataPatchInterface;
6+
use Magento\Framework\Setup\ModuleDataSetupInterface;
7+
8+
class UpdateMetaCatalogSourceAttribute implements DataPatchInterface
9+
{
10+
/**
11+
* @var ModuleDataSetupInterface
12+
*/
13+
private $moduleDataSetup;
14+
15+
/**
16+
* @param ModuleDataSetupInterface $moduleDataSetup
17+
*/
18+
public function __construct(
19+
ModuleDataSetupInterface $moduleDataSetup
20+
) {
21+
$this->moduleDataSetup = $moduleDataSetup;
22+
}
23+
24+
/**
25+
* Get dependencies for the data patch
26+
*
27+
* @return array
28+
*/
29+
public static function getDependencies(): array
30+
{
31+
return [];
32+
}
33+
34+
/**
35+
* Get alias for the data patch
36+
*
37+
* @return array
38+
*/
39+
public function getAliases(): array
40+
{
41+
return [];
42+
}
43+
44+
/**
45+
* Update the attribute source model value from Facebook to Meta
46+
*
47+
* @return void
48+
*/
49+
public function apply(): void
50+
{
51+
$this->moduleDataSetup->getConnection()->startSetup();
52+
53+
$this->updateAttribute();
54+
55+
$this->moduleDataSetup->getConnection()->endSetup();
56+
}
57+
58+
/**
59+
* Update the attribute source model value from Facebook to Meta
60+
*/
61+
private function updateAttribute()
62+
{
63+
$connection = $this->moduleDataSetup->getConnection();
64+
$tableName = $this->moduleDataSetup->getTable('eav_attribute');
65+
$oldSourceModel = 'Facebook\BusinessExtension\Model\Config\Source\Product\GoogleProductCategory';
66+
$newSourceModel = \Meta\Catalog\Model\Config\Source\Product\GoogleProductCategory::class;
67+
68+
$connection->update(
69+
$tableName,
70+
['source_model' => $newSourceModel],
71+
$connection->quoteInto('source_model = ?', $oldSourceModel)
72+
);
73+
}
74+
75+
/**
76+
* Revert the attribute source model value from Facebook to Meta
77+
*
78+
* @return void
79+
*/
80+
public function revert(): void
81+
{
82+
$this->moduleDataSetup->getConnection()->startSetup();
83+
84+
$this->revertAttributeUpdate();
85+
86+
$this->moduleDataSetup->getConnection()->endSetup();
87+
}
88+
89+
/**
90+
* Revert the attribute source model value from Facebook to Meta
91+
*/
92+
private function revertAttributeUpdate()
93+
{
94+
$connection = $this->moduleDataSetup->getConnection();
95+
$tableName = $this->moduleDataSetup->getTable('eav_attribute');
96+
$oldSourceModel = 'Facebook\BusinessExtension\Model\Config\Source\Product\GoogleProductCategory';
97+
$newSourceModel = \Meta\Catalog\Model\Config\Source\Product\GoogleProductCategory::class;
98+
$connection->update(
99+
$tableName,
100+
['source_model' => $oldSourceModel],
101+
$connection->quoteInto('source_model = ?', $newSourceModel)
102+
);
103+
}
104+
}

0 commit comments

Comments
 (0)