Skip to content

Commit dca7a89

Browse files
committed
added datapatch to migrate configurations
1 parent 82d702d commit dca7a89

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Meta\BusinessExtension\Setup\Patch\Data;
5+
6+
use Magento\Framework\Setup\Patch\DataPatchInterface;
7+
use Magento\Framework\Setup\ModuleDataSetupInterface;
8+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
9+
use Meta\BusinessExtension\Logger\Logger;
10+
use Magento\Store\Model\StoreManagerInterface;
11+
12+
class UpdateConfigurations implements DataPatchInterface
13+
{
14+
15+
private const OLD_CONFIG_TABLE_NAME = 'facebook_business_extension_config';
16+
17+
/**
18+
* @var SystemConfig
19+
*/
20+
private $config;
21+
22+
/**
23+
* @var StoreManagerInterface
24+
*/
25+
private $storeManager;
26+
27+
/**
28+
* @var Logger
29+
*/
30+
private $logger;
31+
32+
/**
33+
* @var ModuleDataSetupInterface
34+
*/
35+
private $moduleDataSetup;
36+
37+
/**
38+
* Constructor
39+
*
40+
* @param ModuleDataSetupInterface $moduleDataSetup
41+
* @param SystemConfig $config
42+
* @param StoreManagerInterface $storeManager
43+
* @param Logger $logger
44+
*/
45+
public function __construct(
46+
ModuleDataSetupInterface $moduleDataSetup,
47+
SystemConfig $config,
48+
StoreManagerInterface $storeManager,
49+
Logger $logger
50+
) {
51+
$this->config = $config;
52+
$this->storeManager = $storeManager;
53+
$this->logger = $logger;
54+
$this->moduleDataSetup = $moduleDataSetup;
55+
}
56+
57+
/**
58+
* Get dependencies for the Patch
59+
*
60+
* @return array
61+
*/
62+
public static function getDependencies(): array
63+
{
64+
return [];
65+
}
66+
67+
/**
68+
* Get alias for the Patch
69+
*
70+
* @return array
71+
*/
72+
public function getAliases(): array
73+
{
74+
return [];
75+
}
76+
77+
/**
78+
* Apply the patch
79+
*
80+
* @return void
81+
*/
82+
public function apply(): void
83+
{
84+
$connection = $this->moduleDataSetup->getConnection();
85+
$connection->startSetup();
86+
87+
$this->migrateOldConfigs($connection);
88+
$this->deleteOldConfigs($connection);
89+
90+
$connection->endSetup();
91+
}
92+
93+
/**
94+
* Revert the changes from Patch
95+
*
96+
* @return void
97+
*/
98+
public function revert(): void
99+
{
100+
$connection = $this->moduleDataSetup->getConnection();
101+
$connection->startSetup();
102+
103+
$connection->dropTable(self::OLD_CONFIG_TABLE_NAME);
104+
$connection->delete('core_config_data', "path LIKE 'facebook/%'");
105+
106+
$connection->endSetup();
107+
}
108+
109+
/**
110+
* Migrate configuration from old table to core_config_data
111+
*
112+
* @param $connection
113+
* @return void
114+
*/
115+
private function migrateOldConfigs($connection): void
116+
{
117+
$facebookConfig = $connection->getTableName(self::OLD_CONFIG_TABLE_NAME);
118+
if ($facebookConfig) {
119+
$configKeys = [SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_ACCESS_TOKEN => 'fbaccess/token',
120+
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_INSTALLED => 'fbe/installed',
121+
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_EXTERNAL_BUSINESS_ID => 'fbe/external/id',
122+
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_CATALOG_ID => 'fbe/catalog/id',
123+
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_PIXEL_AAM_SETTINGS => 'fbpixel/aam_settings',
124+
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_PIXEL_ID => 'fbpixel/id',
125+
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_PROFILES => 'fbprofile/ids',
126+
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_API_VERSION => 'fb/api/version'
127+
];
128+
129+
$defaultStoreId = $this->storeManager->getDefaultStoreView()->getStoreGroupId();
130+
131+
foreach ($configKeys as $newKey => $oldKey) {
132+
try {
133+
$query = $connection->select()
134+
->from($facebookConfig, ['config_value'])
135+
->where('config_key = ?', $oldKey);
136+
$value = $connection->fetchOne($query);
137+
$this->config->saveConfig($newKey, $value, $defaultStoreId);
138+
} catch (\Exception $e) {
139+
$this->logger->critical($e);
140+
$this->logger->critical('Error migrating: '. $oldKey);
141+
}
142+
}
143+
}
144+
}
145+
146+
/**
147+
* Remove configurations from old table
148+
*
149+
* @param $connection
150+
* @return void
151+
*/
152+
private function deleteOldConfigs($connection): void
153+
{
154+
$facebookConfig = $connection->getTableName(self::OLD_CONFIG_TABLE_NAME);
155+
if($facebookConfig) {
156+
try {
157+
$connection->delete($facebookConfig, "config_key NOT LIKE 'permanent%'");
158+
} catch (\Exception $e) {
159+
$this->logger->critical($e);
160+
}
161+
}
162+
}
163+
}

0 commit comments

Comments
 (0)