Skip to content

Commit 41b5b97

Browse files
author
Sergii Kovalenko
committed
MAGETWO-87928: Implement infrastructure for safe-rollback feature
1 parent 0bf8327 commit 41b5b97

File tree

6 files changed

+71
-98
lines changed

6 files changed

+71
-98
lines changed

setup/src/Magento/Setup/Console/Command/InstallCommand.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ class InstallCommand extends AbstractSetupCommand
6767
*/
6868
const INPUT_KEY_INTERACTIVE_SETUP_SHORTCUT = 'i';
6969

70+
/**
71+
* Parameter says that in this mode all destructive operations, like column removal will be dumped
72+
*/
73+
const INPUT_KEY_SAFE_INSTALLER_MODE = 'safe-mode';
74+
75+
/**
76+
* Parameter allows to restore data, that was dumped with safe mode before
77+
*/
78+
const INPUT_KEY_DATA_RESTORE = 'data-restore';
79+
7080
/**
7181
* Regex for sales_order_increment_prefix validation.
7282
*/
@@ -175,6 +185,18 @@ protected function configure()
175185
InputOption::VALUE_NONE,
176186
'Interactive Magento instalation'
177187
),
188+
new InputOption(
189+
self::INPUT_KEY_SAFE_INSTALLER_MODE,
190+
null,
191+
InputOption::VALUE_NONE,
192+
'Safe installation of Magento with dumps on destructive operations, like column removal'
193+
),
194+
new InputOption(
195+
self::INPUT_KEY_DATA_RESTORE,
196+
null,
197+
InputOption::VALUE_NONE,
198+
'Restore removed data from dumps'
199+
),
178200
]);
179201
$this->setName('setup:install')
180202
->setDescription('Installs the Magento application')

setup/src/Magento/Setup/Console/Command/UpgradeCommand.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,19 @@ protected function configure()
8181
InputOption::VALUE_OPTIONAL,
8282
'Allows to convert old scripts (InstallSchema, UpgradeSchema) to db_schema.xml format',
8383
false
84-
)
84+
),
85+
new InputOption(
86+
InstallCommand::INPUT_KEY_SAFE_INSTALLER_MODE,
87+
null,
88+
InputOption::VALUE_NONE,
89+
'Safe installation of Magento with dumps on destructive operations, like column removal'
90+
),
91+
new InputOption(
92+
InstallCommand::INPUT_KEY_DATA_RESTORE,
93+
null,
94+
InputOption::VALUE_NONE,
95+
'Restore removed data from dumps'
96+
),
8597
];
8698
$this->setName('setup:upgrade')
8799
->setDescription('Upgrades the Magento application, DB data, and schema')

setup/src/Magento/Setup/Model/Declaration/Schema/Diff/Diff.php

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,6 @@ private function canBeRegistered(ElementInterface $object, $operation)
177177
return isset($whiteList[$object->getNameWithoutPrefix()]);
178178
}
179179

180-
/**
181-
* Register request for installation.
182-
*
183-
* @param Request $request
184-
* @return void
185-
*/
186-
public function registerInstallationRequest(Request $request)
187-
{
188-
$this->request = $request;
189-
}
190-
191180
/**
192181
* Register DTO object.
193182
*
@@ -215,34 +204,4 @@ public function register(
215204
$this->debugChanges[$operation][] = $history;
216205
return $this;
217206
}
218-
219-
/**
220-
* @inheritdoc
221-
*/
222-
public function registerSchema(Schema $schema)
223-
{
224-
$this->schema = $schema;
225-
}
226-
227-
/**
228-
* Retrieve current schema.
229-
* This function needs for rollback functionality.
230-
*
231-
* @return Schema
232-
*/
233-
public function getCurrentSchemaState()
234-
{
235-
return $this->schema;
236-
}
237-
238-
/**
239-
* Request holds some information from cli command or UI
240-
* like: save mode or dry-run mode.
241-
*
242-
* @return Request
243-
*/
244-
public function getCurrentInstallationRequest()
245-
{
246-
return $this->request;
247-
}
248207
}

setup/src/Magento/Setup/Model/Declaration/Schema/Diff/DiffInterface.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,4 @@ public function register(
4949
$operation,
5050
ElementInterface $oldDtoObject = null
5151
);
52-
53-
/**
54-
* Register current state of schema to registry.
55-
*
56-
* @param Schema $schema
57-
* @return void
58-
*/
59-
public function registerSchema(Schema $schema);
60-
61-
/**
62-
* Retrieve current schema object.
63-
*
64-
* @return Schema
65-
*/
66-
public function getCurrentSchemaState();
67-
68-
/**
69-
* Return current installation request.
70-
*
71-
* @return Request
72-
*/
73-
public function getCurrentInstallationRequest();
74-
75-
/**
76-
* Register installation request with all needed options.
77-
*
78-
* @param Request $request
79-
* @return void
80-
*/
81-
public function registerInstallationRequest(Request $request);
8252
}

setup/src/Magento/Setup/Model/Declaration/Schema/OperationsExecutor.php

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Setup\Model\Declaration\Schema;
88

99
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Setup\Console\Command\InstallCommand;
1011
use Magento\Setup\Model\Declaration\Schema\DataSavior\DataSaviorInterface;
1112
use Magento\Setup\Model\Declaration\Schema\Db\DbSchemaWriterInterface;
1213
use Magento\Setup\Model\Declaration\Schema\Db\StatementAggregatorFactory;
@@ -157,11 +158,12 @@ private function operationIsOppositeToDestructive(OperationInterface $operation)
157158
*
158159
* @see OperationInterface
159160
* @param DiffInterface $diff
161+
* @param array $requestData
160162
* @return void
161163
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
162164
* @SuppressWarnings(PHPMD.NPathComplexity)
163165
*/
164-
public function execute(DiffInterface $diff)
166+
public function execute(DiffInterface $diff, array $requestData)
165167
{
166168
$this->startSetupForAllConnections();
167169
$tableHistories = $diff->getAll();
@@ -186,9 +188,9 @@ public function execute(DiffInterface $diff)
186188
}
187189
}
188190

189-
$this->doDump($destructiveElements);
191+
$this->doDump($destructiveElements, $requestData);
190192
$this->dbSchemaWriter->compile($statementAggregator);
191-
$this->doRestore($oppositeToDestructiveElements);
193+
$this->doRestore($oppositeToDestructiveElements, $requestData);
192194
}
193195
}
194196

@@ -199,17 +201,23 @@ public function execute(DiffInterface $diff)
199201
* Do restore of destructive operations
200202
*
201203
* @param array $elements
204+
* @param array $requestData
202205
*/
203-
private function doRestore(array $elements)
206+
private function doRestore(array $elements, array $requestData)
204207
{
205-
/**
206-
* @var ElementInterface $element
207-
*/
208-
foreach ($elements as $element) {
209-
foreach ($this->dataSaviorsCollection as $dataSavior) {
210-
if ($dataSavior->isAcceptable($element)) {
211-
$dataSavior->restore($element);
212-
break;
208+
$restoreMode = isset($requestData[InstallCommand::INPUT_KEY_DATA_RESTORE]) &&
209+
$requestData[InstallCommand::INPUT_KEY_DATA_RESTORE];
210+
211+
if ($restoreMode) {
212+
/**
213+
* @var ElementInterface $element
214+
*/
215+
foreach ($elements as $element) {
216+
foreach ($this->dataSaviorsCollection as $dataSavior) {
217+
if ($dataSavior->isAcceptable($element)) {
218+
$dataSavior->restore($element);
219+
break;
220+
}
213221
}
214222
}
215223
}
@@ -219,17 +227,23 @@ private function doRestore(array $elements)
219227
* Do dump of destructive operations
220228
*
221229
* @param array $elements
230+
* @param array $requestData
222231
*/
223-
private function doDump(array $elements)
232+
private function doDump(array $elements, array $requestData)
224233
{
225-
/**
226-
* @var ElementInterface $element
227-
*/
228-
foreach ($elements as $element) {
229-
foreach ($this->dataSaviorsCollection as $dataSavior) {
230-
if ($dataSavior->isAcceptable($element)) {
231-
$dataSavior->dump($element);
232-
break;
234+
$safeMode = isset($requestData[InstallCommand::INPUT_KEY_SAFE_INSTALLER_MODE]) &&
235+
$requestData[InstallCommand::INPUT_KEY_SAFE_INSTALLER_MODE];
236+
237+
if ($safeMode) {
238+
/**
239+
* @var ElementInterface $element
240+
*/
241+
foreach ($elements as $element) {
242+
foreach ($this->dataSaviorsCollection as $dataSavior) {
243+
if ($dataSavior->isAcceptable($element)) {
244+
$dataSavior->dump($element);
245+
break;
246+
}
233247
}
234248
}
235249
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ public function installSchema(array $requestData)
6666
$declarativeSchema = $this->schemaConfig->getDeclarationConfig();
6767
$dbSchema = $this->schemaConfig->getDbConfig();
6868
$diff = $this->schemaDiff->diff($declarativeSchema, $dbSchema);
69-
$diff->registerSchema($declarativeSchema);
70-
$diff->registerInstallationRequest(
71-
$this->requestFactory->create($requestData)
72-
);
73-
$this->operationsExecutor->execute($diff);
69+
$this->operationsExecutor->execute($diff, $requestData);
7470
}
7571
}

0 commit comments

Comments
 (0)