Skip to content

Commit 8689dd7

Browse files
author
Sergii Kovalenko
committed
MAGETWO-87551: Convert existing data install/upgrade scripts
--create generate command for patches
1 parent bb2ec1f commit 8689dd7

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

app/code/Magento/Customer/Setup/Patch/Data/DefaultCustomerGroupsAndAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(
5757
public function apply()
5858
{
5959
/** @var CustomerSetup $customerSetup */
60-
$customerSetup = $this->customerSetupFactory->create(['resourceConnection' => $this->resourceConnection]);
60+
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
6161

6262
// insert default customer groups
6363
$this->resourceConnection->getConnection()->insertForce(

app/code/Magento/Customer/Setup/Patch/Data/MigrateStoresAllowedCountriesToWebsite.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Customer\Setup\Patch\Data;
88

99
use Magento\Directory\Model\AllowedCountries;
10+
use Magento\Directory\Model\AllowedCountriesFactory;
1011
use Magento\Store\Model\ScopeInterface;
1112
use Magento\Store\Model\StoreManagerInterface;
1213
use Magento\Framework\App\ResourceConnection;
@@ -34,12 +35,12 @@ class MigrateStoresAllowedCountriesToWebsite implements DataPatchInterface, Patc
3435
* MigrateStoresAllowedCountriesToWebsite constructor.
3536
* @param ResourceConnection $resourceConnection
3637
* @param StoreManagerInterface $storeManager
37-
* @param AllowedCountriesFactory $allowedCountries
38+
* @param AllowedCountries $allowedCountries
3839
*/
3940
public function __construct(
4041
ResourceConnection $resourceConnection,
4142
\Magento\Store\Model\StoreManagerInterface $storeManager,
42-
\Magento\Directory\Model\AllowedCountriesFactory $allowedCountries
43+
\Magento\Directory\Model\AllowedCountries $allowedCountries
4344
) {
4445
$this->resourceConnection = $resourceConnection;
4546
$this->storeManager = $storeManager;

app/code/Magento/Eav/Setup/EavSetup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ public function addAttributeGroup($entityTypeId, $setId, $name, $sortOrder = nul
578578
$data['attribute_group_code'] = $attributeGroupCode;
579579
}
580580
$this->resourceConnection->getConnection()->insert(
581-
$this->resourceConnection->getTable('eav_attribute_group'),
581+
$this->resourceConnection->getTableName('eav_attribute_group'),
582582
$data
583583
);
584584
}

setup/src/Magento/Setup/Model/Installer.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,10 @@ private function handleDBSchemaData($setup, $type)
906906
if ($type === 'schema') {
907907
$patchApplier = $this->patchApplierFactory->create(['schemaSetup' => $setup]);
908908
} elseif ($type === 'data') {
909-
$patchApplier = $this->patchApplierFactory->create(['moduleDataSetup' => $setup]);
909+
$patchApplier = $this->patchApplierFactory->create([
910+
'moduleDataSetup' => $setup,
911+
'objectManager' => $this->objectManagerProvider->get()
912+
]);
910913
}
911914

912915
foreach ($moduleNames as $moduleName) {
@@ -922,6 +925,11 @@ private function handleDBSchemaData($setup, $type)
922925
if ($upgrader) {
923926
$this->log->logInline("Upgrading $type.. ");
924927
$upgrader->upgrade($setup, $moduleContextList[$moduleName]);
928+
if ($type === 'schema') {
929+
$resource->setDbVersion($moduleName, $configVer);
930+
} elseif ($type === 'data') {
931+
$resource->setDataVersion($moduleName, $configVer);
932+
}
925933
}
926934
}
927935
} elseif ($configVer) {
@@ -935,7 +943,16 @@ private function handleDBSchemaData($setup, $type)
935943
$this->log->logInline("Upgrading $type... ");
936944
$upgrader->upgrade($setup, $moduleContextList[$moduleName]);
937945
}
946+
947+
if ($installer || $upgrader) {
948+
if ($type === 'schema') {
949+
$resource->setDbVersion($moduleName, $configVer);
950+
} elseif ($type === 'data') {
951+
$resource->setDataVersion($moduleName, $configVer);
952+
}
953+
}
938954
}
955+
939956
/**
940957
* Applying data patches after old upgrade data scripts
941958
*/
@@ -945,12 +962,6 @@ private function handleDBSchemaData($setup, $type)
945962
$patchApplier->applyDataPatch($moduleName);
946963
}
947964

948-
if ($type === 'schema') {
949-
$resource->setDbVersion($moduleName, $configVer);
950-
} elseif ($type === 'data') {
951-
$resource->setDataVersion($moduleName, $configVer);
952-
}
953-
954965
$this->logProgress();
955966
}
956967

setup/src/Magento/Setup/Model/Patch/PatchApplier.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\App\ResourceConnection;
1010
use Magento\Framework\Module\ModuleResource;
11+
use Magento\Framework\ObjectManagerInterface;
1112
use Magento\Framework\Setup\ModuleDataSetupInterface;
1213
use Magento\Framework\Setup\SchemaSetupInterface;
1314
use Magento\Setup\Exception;
@@ -62,6 +63,11 @@ class PatchApplier
6263
*/
6364
private $moduleDataSetup;
6465

66+
/**
67+
* @var ObjectManagerInterface
68+
*/
69+
private $objectManager;
70+
6571
/**
6672
* PatchApplier constructor.
6773
* @param PatchReader $dataPatchReader
@@ -71,6 +77,7 @@ class PatchApplier
7177
* @param ModuleResource $moduleResource
7278
* @param PatchHistory $patchHistory
7379
* @param PatchFactory $patchFactory
80+
* @param ObjectManagerInterface $objectManager
7481
* @param \Magento\Framework\Setup\SchemaSetupInterface $schemaSetup
7582
* @param ModuleDataSetupInterface $moduleDataSetup
7683
*/
@@ -82,6 +89,7 @@ public function __construct(
8289
ModuleResource $moduleResource,
8390
PatchHistory $patchHistory,
8491
PatchFactory $patchFactory,
92+
ObjectManagerInterface $objectManager,
8593
\Magento\Framework\Setup\SchemaSetupInterface $schemaSetup = null,
8694
\Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup = null
8795
) {
@@ -94,6 +102,7 @@ public function __construct(
94102
$this->patchFactory = $patchFactory;
95103
$this->schemaSetup = $schemaSetup;
96104
$this->moduleDataSetup = $moduleDataSetup;
105+
$this->objectManager = $objectManager;
97106
}
98107

99108
/**
@@ -121,9 +130,6 @@ public function applyDataPatch($moduleName = null)
121130
$dataPatches = $this->dataPatchReader->read($moduleName);
122131
$registry = $this->prepareRegistry($dataPatches);
123132
$adapter = $this->resourceConnection->getConnection();
124-
/**
125-
* @var DataPatchInterface $dataPatch
126-
*/
127133
foreach ($registry as $dataPatch) {
128134

129135
/**
@@ -135,7 +141,7 @@ public function applyDataPatch($moduleName = null)
135141

136142
try {
137143
$adapter->beginTransaction();
138-
$dataPatch = $this->patchFactory->create($dataPatch, ['moduleDataSetup' => $this->moduleDataSetup]);
144+
$dataPatch = $this->objectManager->create('\\' . $dataPatch, ['moduleDataSetup' => $this->moduleDataSetup]);
139145
if (!$dataPatch instanceof DataPatchInterface) {
140146
throw new Exception(
141147
sprintf("Patch %s should implement DataPatchInterface", $dataPatch)

0 commit comments

Comments
 (0)