Skip to content

Commit 2bcefa4

Browse files
authored
Access table names via the resource and not the connection (#715)
* Add ResourceConnection to fix database prefix issue * Remove unused code * Update locations where ResourceModel should be used * Configure dependencies * Add explicit dependencies * Testing some reverts * Add ResourceConnection back in * Edit composer.json * Change to use alternate function * Delete helper function * Testing change * Finalize changes
1 parent f2c2a3d commit 2bcefa4

File tree

7 files changed

+73
-49
lines changed

7 files changed

+73
-49
lines changed

app/code/Meta/BusinessExtension/Model/ResourceModel/FacebookInstalledFeature.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public function _construct()
4545
*/
4646
public function doesFeatureTypeExist($featureType, $storeId)
4747
{
48-
$connection = $this->getConnection();
48+
$connection = $this->_resources->getConnection();
4949

50-
$facebookInstalledFeaturesTable = $connection->getTableName(self::TABLE_NAME);
50+
$facebookInstalledFeaturesTable = $this->_resources->getTableName(self::TABLE_NAME);
5151
$select = $connection->select()
5252
->from($facebookInstalledFeaturesTable)
5353
->where('feature_type = ?', $featureType)
@@ -63,9 +63,9 @@ public function doesFeatureTypeExist($featureType, $storeId)
6363
*/
6464
public function deleteAll($storeId)
6565
{
66-
$connection = $this->getConnection();
66+
$connection = $this->_resources->getConnection();
6767

68-
$facebookInstalledFeaturesTable = $connection->getTableName(self::TABLE_NAME);
68+
$facebookInstalledFeaturesTable = $this->_resources->getTableName(self::TABLE_NAME);
6969
return $connection->delete($facebookInstalledFeaturesTable, ['store_id = ?' => $storeId]);
7070
}
7171

@@ -89,9 +89,9 @@ function ($value) use ($storeId) {
8989
array_values($features)
9090
);
9191

92-
$connection = $this->getConnection();
92+
$connection = $this->_resources->getConnection();
9393

94-
$facebookInstalledFeaturesTable = $connection->getTableName(self::TABLE_NAME);
94+
$facebookInstalledFeaturesTable = $this->_resources->getTableName(self::TABLE_NAME);
9595
$connection->insertOnDuplicate($facebookInstalledFeaturesTable, $finalFeatures);
9696
}
9797

app/code/Meta/BusinessExtension/Model/ResourceModel/MetaIssueNotification.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public function _construct()
4646
*/
4747
public function deleteByNotificationId(string $notificationId): int
4848
{
49-
$connection = $this->getConnection();
49+
$connection = $this->_resources->getConnection();
5050

51-
$metaIssueNotificationTable = $connection->getTableName(self::TABLE_NAME);
51+
$metaIssueNotificationTable = $this->_resources->getTableName(self::TABLE_NAME);
5252
return $connection->delete($metaIssueNotificationTable, ['notification_id = ?' => $notificationId]);
5353
}
5454

@@ -59,14 +59,14 @@ public function deleteByNotificationId(string $notificationId): int
5959
*/
6060
public function saveVersionNotification(MetaIssueNotificationInterface $notification)
6161
{
62-
$connection = $this->getConnection();
62+
$connection = $this->_resources->getConnection();
6363
$version_notification = [
6464
'notification_id' => MetaIssueNotification::VERSION_NOTIFICATION_ID,
6565
'message' => $notification->getMessage(),
6666
'severity' => $notification->getSeverity(),
6767
];
6868

69-
$metaIssueNotificationTable = $connection->getTableName(self::TABLE_NAME);
69+
$metaIssueNotificationTable = $this->_resources->getTableName(self::TABLE_NAME);
7070
$connection->insertOnDuplicate($metaIssueNotificationTable, $version_notification);
7171
}
7272

@@ -75,9 +75,9 @@ public function saveVersionNotification(MetaIssueNotificationInterface $notifica
7575
*/
7676
public function loadVersionNotification()
7777
{
78-
$connection = $this->getConnection();
78+
$connection = $this->_resources->getConnection();
7979

80-
$metaIssueNotificationTable = $connection->getTableName(self::TABLE_NAME);
80+
$metaIssueNotificationTable = $this->_resources->getTableName(self::TABLE_NAME);
8181
$select = $connection->select()
8282
->from($metaIssueNotificationTable)
8383
->where('notification_id = ?', MetaIssueNotification::VERSION_NOTIFICATION_ID);

app/code/Meta/BusinessExtension/Setup/Patch/Data/DeleteLegacyData.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(
3030
*/
3131
public static function getDependencies(): array
3232
{
33-
return [];
33+
return [];
3434
}
3535

3636
/**
@@ -44,11 +44,26 @@ public function getAliases(): array
4444
}
4545

4646
/**
47-
* Delete unnecessary data from the legacy version of the extension
47+
* Apply patch
4848
*
4949
* @return void
5050
*/
5151
public function apply(): void
52+
{
53+
$connection = $this->moduleDataSetup->getConnection();
54+
$connection->startSetup();
55+
56+
$this->deleteLegacyData();
57+
58+
$connection->endSetup();
59+
}
60+
61+
/**
62+
* Delete unnecessary data from the legacy version of the extension
63+
*
64+
* @return void
65+
*/
66+
private function deleteLegacyData(): void
5267
{
5368
$productAttributesToDelete = [
5469
'facebook_age_group',
@@ -98,21 +113,23 @@ public function apply(): void
98113

99114
$connection = $this->moduleDataSetup->getConnection();
100115

101-
$connection->startSetup();
102-
103116
// drop legacy facebook_business_extension_config table
104117
$connection->dropTable('facebook_business_extension_config');
105118

106119
// delete legacy product attributes
107-
$eavAttributeTable = $connection->getTableName('eav_attribute');
120+
$eavAttributeTable = $this->moduleDataSetup->getTable('eav_attribute');
108121
foreach ($productAttributesToDelete as $attributeCode) {
109-
$connection->delete($eavAttributeTable, ['attribute_code = ?' => $attributeCode]);
122+
$connection->delete(
123+
$eavAttributeTable,
124+
['attribute_code = ?' => $attributeCode]
125+
);
110126
}
111127

112128
// delete legacy attribute group
113-
$eavAttributeGroupTable = $connection->getTableName('eav_attribute_group');
114-
$connection->delete($eavAttributeGroupTable, ['attribute_group_name = ?' => 'Facebook Attribute Group']);
115-
116-
$connection->endSetup();
129+
$eavAttributeGroupTable = $this->moduleDataSetup->getTable('eav_attribute_group');
130+
$connection->delete(
131+
$eavAttributeGroupTable,
132+
['attribute_group_name = ?' => 'Facebook Attribute Group']
133+
);
117134
}
118135
}

app/code/Meta/BusinessExtension/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
"Meta\\BusinessExtension\\": ""
2424
}
2525
}
26-
}
26+
}

app/code/Meta/Catalog/Model/ResourceModel/FacebookCatalogUpdate.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ public function addProductIds(array $ids, string $method): int
106106
}
107107
}
108108
if (!empty($dataToInsert)) {
109-
$connection = $this->getConnection();
109+
$connection = $this->_resources->getConnection();
110110

111-
$facebookCatalogUpdateTable = $connection->getTableName(self::TABLE_NAME);
111+
$facebookCatalogUpdateTable = $this->_resources->getTableName(self::TABLE_NAME);
112112
return $connection->insertMultiple($facebookCatalogUpdateTable, $dataToInsert);
113113
}
114114
return 0;
@@ -147,10 +147,10 @@ public function reserveProductsForBatchId(string $method, string $batchId): int
147147
$collection->setPageSize(self::BATCH_LIMIT);
148148
$data = $collection->getData();
149149

150-
$connection = $this->getConnection();
150+
$connection = $this->_resources->getConnection();
151151
$updateWhere = $connection->quoteInto('row_id IN (?)', $data);
152152

153-
$facebookCatalogUpdateTable = $connection->getTableName(self::TABLE_NAME);
153+
$facebookCatalogUpdateTable = $this->_resources->getTableName(self::TABLE_NAME);
154154
return $connection->update($facebookCatalogUpdateTable, ['batch_id' => $batchId], $updateWhere);
155155
}
156156

@@ -162,9 +162,9 @@ public function reserveProductsForBatchId(string $method, string $batchId): int
162162
*/
163163
public function deleteBatch(string $batchId): int
164164
{
165-
$connection = $this->getConnection();
165+
$connection = $this->_resources->getConnection();
166166

167-
$facebookCatalogUpdateTable = $connection->getTableName(self::TABLE_NAME);
167+
$facebookCatalogUpdateTable = $this->_resources->getTableName(self::TABLE_NAME);
168168
return $connection->delete($facebookCatalogUpdateTable, ['batch_id = ?' => $batchId]);
169169
}
170170

@@ -176,9 +176,9 @@ public function deleteBatch(string $batchId): int
176176
*/
177177
public function clearBatchId(string $batchId): int
178178
{
179-
$connection = $this->getConnection();
179+
$connection = $this->_resources->getConnection();
180180

181-
$facebookCatalogUpdateTable = $connection->getTableName(self::TABLE_NAME);
181+
$facebookCatalogUpdateTable = $this->_resources->getTableName(self::TABLE_NAME);
182182
return $connection->update($facebookCatalogUpdateTable, ['batch_id' => null], ['batch_id = ?' => $batchId]);
183183
}
184184

@@ -215,10 +215,10 @@ public function getReservedProducts(string $batchId): Collection
215215
private function getChildProductLinks(array $parentIds)
216216
{
217217
$parentLinkField = $this->optionProvider->getProductEntityLinkField();
218-
$connection = $this->getConnection();
218+
$connection = $this->_resources->getConnection();
219219

220-
$catalogProductSuperLinkTable = $connection->getTableName('catalog_product_super_link');
221-
$catalogProductEntityTable = $connection->getTableName('catalog_product_entity');
220+
$catalogProductSuperLinkTable = $this->_resources->getTableName('catalog_product_super_link');
221+
$catalogProductEntityTable = $this->_resources->getTableName('catalog_product_entity');
222222

223223
$select = $connection->select()
224224
->from($catalogProductSuperLinkTable, ['parent_id', 'product_id'])
@@ -238,9 +238,9 @@ public function deleteUpdateProductEntries($sku): void
238238
if ($sku === null) {
239239
return;
240240
}
241-
$connection = $this->getConnection();
241+
$connection = $this->_resources->getConnection();
242242

243-
$facebookCatalogUpdateTable = $connection->getTableName(self::TABLE_NAME);
243+
$facebookCatalogUpdateTable = $this->_resources->getTableName(self::TABLE_NAME);
244244
$connection->delete($facebookCatalogUpdateTable, ['sku = ?' => $sku, 'method = ?' => 'update']);
245245
}
246246

@@ -274,9 +274,9 @@ public function addProductsWithChildren(array $productIds, string $method)
274274
public function cleanupTable()
275275
{
276276
$dateLimit = $this->dateTime->date(null, '-7 days');
277-
$connection = $this->getConnection();
277+
$connection = $this->_resources->getConnection();
278278

279-
$facebookCatalogUpdateTable = $connection->getTableName(self::TABLE_NAME);
279+
$facebookCatalogUpdateTable = $this->_resources->getTableName(self::TABLE_NAME);
280280
return $connection->delete(
281281
$facebookCatalogUpdateTable,
282282
[

app/code/Meta/Catalog/Setup/Patch/Data/AddCatalogSwitch.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class AddCatalogSwitch implements DataPatchInterface
1515
* @var ModuleDataSetupInterface
1616
*/
1717
private ModuleDataSetupInterface $moduleDataSetup;
18+
1819
/**
1920
* @var SystemConfig
2021
*/
@@ -28,7 +29,7 @@ class AddCatalogSwitch implements DataPatchInterface
2829
*/
2930
public function __construct(
3031
ModuleDataSetupInterface $moduleDataSetup,
31-
SystemConfig $systemConfig
32+
SystemConfig $systemConfig
3233
) {
3334
$this->moduleDataSetup = $moduleDataSetup;
3435
$this->systemConfig = $systemConfig;
@@ -82,7 +83,7 @@ public function apply(): void
8283
private function updateStoreCatalogIntegration($storeId): void
8384
{
8485
$connection = $this->moduleDataSetup->getConnection();
85-
$coreConfigTable = $connection->getTableName(self::CORE_CONFIG_TABLE);
86+
$coreConfigTable = $this->moduleDataSetup->getTable(self::CORE_CONFIG_TABLE);
8687

8788
$isDailyFeedSyncEnabled = $this->fetchValue(
8889
$storeId,
@@ -154,15 +155,18 @@ private function updateStoreCatalogIntegration($storeId): void
154155
private function fetchValue($storeId, $configPath)
155156
{
156157
$connection = $this->moduleDataSetup->getConnection();
158+
157159
$scopeCondition = $connection->prepareSqlCondition('scope_id', [
158160
'eq' => $storeId,
159161
]);
160162
$pathCondition = $connection->prepareSqlCondition('path', [
161163
'eq' => $configPath,
162164
]);
165+
166+
$coreConfigTable = $this->moduleDataSetup->getTable(self::CORE_CONFIG_TABLE);
163167
$query = $connection
164168
->select()
165-
->from($connection->getTableName(self::CORE_CONFIG_TABLE))
169+
->from($coreConfigTable)
166170
->where($scopeCondition)
167171
->where($pathCondition);
168172

app/code/Meta/Catalog/Setup/Patch/Data/UpdateMetaCatalogSourceAttribute.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class UpdateMetaCatalogSourceAttribute implements DataPatchInterface
1010
/**
1111
* @var ModuleDataSetupInterface
1212
*/
13-
private $moduleDataSetup;
13+
private ModuleDataSetupInterface $moduleDataSetup;
1414

1515
/**
1616
* @param ModuleDataSetupInterface $moduleDataSetup
@@ -28,7 +28,7 @@ public function __construct(
2828
*/
2929
public static function getDependencies(): array
3030
{
31-
return [];
31+
return [];
3232
}
3333

3434
/**
@@ -48,11 +48,12 @@ public function getAliases(): array
4848
*/
4949
public function apply(): void
5050
{
51-
$this->moduleDataSetup->getConnection()->startSetup();
51+
$connection = $this->moduleDataSetup->getConnection();
52+
$connection->startSetup();
5253

5354
$this->updateAttribute();
5455

55-
$this->moduleDataSetup->getConnection()->endSetup();
56+
$connection->endSetup();
5657
}
5758

5859
/**
@@ -61,13 +62,14 @@ public function apply(): void
6162
private function updateAttribute()
6263
{
6364
$connection = $this->moduleDataSetup->getConnection();
64-
$tableName = $this->moduleDataSetup->getTable('eav_attribute');
65+
$eavAttributeTable = $this->moduleDataSetup->getTable('eav_attribute');
66+
6567
// phpcs:disable
6668
$oldSourceModel = 'Facebook\BusinessExtension\Model\Config\Source\Product\GoogleProductCategory';
6769
$newSourceModel = \Meta\Catalog\Model\Config\Source\Product\GoogleProductCategory::class;
6870

6971
$connection->update(
70-
$tableName,
72+
$eavAttributeTable,
7173
['source_model' => $newSourceModel],
7274
$connection->quoteInto('source_model = ?', $oldSourceModel)
7375
);
@@ -93,12 +95,13 @@ public function revert(): void
9395
private function revertAttributeUpdate()
9496
{
9597
$connection = $this->moduleDataSetup->getConnection();
96-
$tableName = $this->moduleDataSetup->getTable('eav_attribute');
98+
$eavAttributeTable = $this->moduleDataSetup->getTable('eav_attribute');
99+
97100
// phpcs:disable
98101
$oldSourceModel = 'Facebook\BusinessExtension\Model\Config\Source\Product\GoogleProductCategory';
99102
$newSourceModel = \Meta\Catalog\Model\Config\Source\Product\GoogleProductCategory::class;
100103
$connection->update(
101-
$tableName,
104+
$eavAttributeTable,
102105
['source_model' => $oldSourceModel],
103106
$connection->quoteInto('source_model = ?', $newSourceModel)
104107
);

0 commit comments

Comments
 (0)