Skip to content

Commit 388c7ab

Browse files
author
Sergii Kovalenko
committed
MAGETWO-81024: Declarative Dry Run Mode
1 parent 74a70a0 commit 388c7ab

File tree

12 files changed

+402
-29
lines changed

12 files changed

+402
-29
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
return ['CREATE TABLE `reference_table` (
7+
`tinyint_ref` tinyint(7) NOT NULL AUTO_INCREMENT ,
8+
`tinyint_without_padding` tinyint(2) NOT NULL DEFAULT 0 ,
9+
`bigint_without_padding` bigint(20) NOT NULL DEFAULT 0 ,
10+
`smallint_without_padding` smallint(5) NOT NULL DEFAULT 0 ,
11+
`integer_without_padding` int(11) NOT NULL DEFAULT 0 ,
12+
`smallint_with_big_padding` smallint(254) NOT NULL DEFAULT 0 ,
13+
`smallint_without_default` smallint(2) NULL ,
14+
`int_without_unsigned` int(2) NULL ,
15+
`int_unsigned` int(2) UNSIGNED NULL ,
16+
`bigint_default_nullable` bigint(2) UNSIGNED NULL DEFAULT 1 ,
17+
`bigint_not_default_not_nullable` bigint(2) UNSIGNED NOT NULL ,
18+
CONSTRAINT PRIMARY KEY (`tinyint_ref`)
19+
) ENGINE=innodb
20+
21+
CREATE TABLE `auto_increment_test` (
22+
`int_auto_increment_with_nullable` int(12) UNSIGNED NOT NULL AUTO_INCREMENT ,
23+
`int_disabled_auto_increment` smallint(12) UNSIGNED NULL DEFAULT 0 ,
24+
CONSTRAINT `unique_null_key` UNIQUE KEY (`int_auto_increment_with_nullable`)
25+
) ENGINE=innodb
26+
27+
CREATE TABLE `test_table` (
28+
`smallint` smallint(3) NOT NULL AUTO_INCREMENT ,
29+
`tinyint` tinyint(7) NULL ,
30+
`bigint` bigint(13) NULL DEFAULT 0 ,
31+
`float` float(12, 4) NULL DEFAULT 0 ,
32+
`double` decimal(14, 6) NULL DEFAULT 11111111.111111 ,
33+
`decimal` decimal(15, 4) NULL DEFAULT 0 ,
34+
`date` date NULL ,
35+
`timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
36+
`datetime` datetime NULL DEFAULT 0 ,
37+
`longtext` longtext NULL ,
38+
`mediumtext` mediumtext NULL ,
39+
`varchar` varchar(254) NULL ,
40+
`mediumblob` mediumblob NULL ,
41+
`blob` blob NULL ,
42+
`boolean` BOOLEAN NULL ,
43+
CONSTRAINT `some_unique_key` UNIQUE KEY (`smallint`,`bigint`),
44+
CONSTRAINT `some_foreign_key` FOREIGN KEY (`tinyint`) REFERENCES `reference_table` (`tinyint_ref`) ON DELETE NO ACTION,
45+
INDEX `speedup_index` (`tinyint`,`bigint`)
46+
) ENGINE=innodb
47+
48+
CREATE TABLE `patch_list` (
49+
`patch_id` int(11) NOT NULL AUTO_INCREMENT COMMENT "Patch Auto Increment",
50+
`patch_name` varchar(1024) NOT NULL COMMENT "Patch Class Name",
51+
CONSTRAINT PRIMARY KEY (`patch_id`)
52+
) ENGINE=innodb COMMENT="List of data/schema patches"
53+
54+
'];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
return ['ALTER TABLE `reference_table` MODIFY COLUMN `tinyint_without_padding` tinyint(2) NOT NULL , MODIFY COLUMN `bigint_default_nullable` bigint(2) UNSIGNED NULL DEFAULT 123 , MODIFY COLUMN `bigint_not_default_not_nullable` bigint(20) NOT NULL
7+
8+
ALTER TABLE `auto_increment_test` MODIFY COLUMN `int_auto_increment_with_nullable` int(15) UNSIGNED NULL
9+
10+
ALTER TABLE `test_table` MODIFY COLUMN `float` float(12, 10) NULL DEFAULT 0 , MODIFY COLUMN `double` double(245, 10) NULL , MODIFY COLUMN `timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP , MODIFY COLUMN `varchar` varchar(100) NULL , MODIFY COLUMN `boolean` BOOLEAN NULL DEFAULT 1
11+
12+
'];

dev/tests/setup-integration/framework/Magento/TestFramework/Deploy/CliCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function enableModule($moduleName)
6565
{
6666
$initParams = $this->parametersHolder->getInitParams();
6767
$enableModuleCommand = 'php -f ' . BP . '/bin/magento module:enable ' . $moduleName
68-
. ' -n -vvv --magento-init-params=' . $initParams['magento-init-params'];
68+
. ' -n -vvv --magento-init-params="' . $initParams['magento-init-params'] . '"';
6969
return $this->shell->execute($enableModuleCommand);
7070
}
7171

@@ -78,10 +78,10 @@ public function enableModule($moduleName)
7878
public function upgrade($installParams = [])
7979
{
8080
$initParams = $this->parametersHolder->getInitParams();
81-
$upgradeCommand = 'php -f ' . BP . '/bin/magento setup:upgrade -vvv -n --magento-init-params='
82-
. $initParams['magento-init-params'];
83-
84-
$upgradeCommand .= ' ' . implode(" ", $this->toCliArguments(array_keys($installParams)));
81+
$upgradeCommand = 'php -f ' . BP . '/bin/magento setup:upgrade -vvv -n --magento-init-params="'
82+
. $initParams['magento-init-params'] . '"';
83+
$installParams = $this->toCliArguments($installParams);
84+
$upgradeCommand .= ' ' . implode(" ", array_keys($installParams));
8585

8686
return $this->shell->execute($upgradeCommand, array_values($installParams));
8787
}

dev/tests/setup-integration/framework/Magento/TestFramework/Deploy/ParametersHolder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ private function getCustomDirs()
6363
{
6464
$installDir = TESTS_TEMP_DIR;
6565
$path = DirectoryList::PATH;
66+
$var = "{$installDir}/var";
6667
$customDirs = [
6768
DirectoryList::CONFIG => [$path => "{$installDir}/etc"],
69+
DirectoryList::VAR_DIR => [$path => $var],
6870
];
6971
return $customDirs;
7072
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Setup;
8+
9+
use Magento\Setup\Model\Declaration\Schema\DryRunLogger;
10+
use Magento\TestFramework\Deploy\CliCommand;
11+
use Magento\TestFramework\Deploy\TestModuleManager;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\TestCase\SetupTestCase;
14+
15+
/**
16+
* The purpose of this test is verifying declarative installation works.
17+
*/
18+
class DryRunTest extends SetupTestCase
19+
{
20+
/**
21+
* @var TestModuleManager
22+
*/
23+
private $moduleManager;
24+
25+
/**
26+
* @var CliCommand
27+
*/
28+
private $cliCommad;
29+
30+
public function setUp()
31+
{
32+
$objectManager = Bootstrap::getObjectManager();
33+
$this->moduleManager = $objectManager->get(TestModuleManager::class);
34+
$this->cliCommad = $objectManager->get(CliCommand::class);
35+
}
36+
37+
/**
38+
* @moduleName Magento_TestSetupDeclarationModule1
39+
* @dataProviderFromFile Magento/TestSetupDeclarationModule1/fixture/dry_run_log.php
40+
*/
41+
public function testDryRunOnCleanDatabase()
42+
{
43+
$logFileName = TESTS_TEMP_DIR . '/var/log/' . DryRunLogger::FILE_NAME;
44+
$this->cliCommad->install(
45+
['Magento_TestSetupDeclarationModule1'],
46+
['dry-run' => true]
47+
);
48+
self::assertFileExists($logFileName);
49+
$data = file_get_contents($logFileName);
50+
self::assertEquals($data, $this->getData()[0]);
51+
}
52+
53+
/**
54+
* @moduleName Magento_TestSetupDeclarationModule1
55+
* @dataProviderFromFile Magento/TestSetupDeclarationModule1/fixture/dry_run_log_on_upgrade.php
56+
*/
57+
public function testDryRunOnUpgrade()
58+
{
59+
$logFileName = TESTS_TEMP_DIR . '/var/log/' . DryRunLogger::FILE_NAME;
60+
$this->cliCommad->install(['Magento_TestSetupDeclarationModule1']);
61+
$this->moduleManager->updateRevision(
62+
'Magento_TestSetupDeclarationModule1',
63+
'column_modifications',
64+
'db_schema.xml',
65+
'etc'
66+
);
67+
$this->cliCommad->upgrade(['dry-run' => true]);
68+
self::assertFileExists($logFileName);
69+
$data = file_get_contents($logFileName);
70+
self::assertEquals($data, $this->getData()[0]);
71+
}
72+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ class InstallCommand extends AbstractSetupCommand
7777
*/
7878
const INPUT_KEY_DATA_RESTORE = 'data-restore';
7979

80+
/**
81+
* We will run installation or upgrade in Dry Run mode
82+
*/
83+
const INPUT_KEY_DRY_RUN_MODE = 'dry-run';
84+
8085
/**
8186
* Regex for sales_order_increment_prefix validation.
8287
*/
@@ -197,6 +202,13 @@ protected function configure()
197202
InputOption::VALUE_NONE,
198203
'Restore removed data from dumps'
199204
),
205+
new InputOption(
206+
self::INPUT_KEY_DRY_RUN_MODE,
207+
null,
208+
InputOption::VALUE_OPTIONAL,
209+
'Magento Installation will be run in dry-run mode',
210+
false
211+
),
200212
]);
201213
$this->setName('setup:install')
202214
->setDescription('Installs the Magento application')

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ protected function configure()
9494
InputOption::VALUE_NONE,
9595
'Restore removed data from dumps'
9696
),
97+
new InputOption(
98+
InstallCommand::INPUT_KEY_DRY_RUN_MODE,
99+
null,
100+
InputOption::VALUE_OPTIONAL,
101+
'Magento Installation will be run in dry-run mode',
102+
false
103+
),
97104
];
98105
$this->setName('setup:upgrade')
99106
->setDescription('Upgrades the Magento application, DB data, and schema')
@@ -112,7 +119,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
112119
$installer = $this->installerFactory->create(new ConsoleLogger($output));
113120
$installer->updateModulesSequence($keepGenerated);
114121
$installer->installSchema($request);
115-
$installer->installDataFixtures();
122+
$installer->installDataFixtures($request);
116123

117124
if ($this->deploymentConfig->isAvailable()) {
118125
$importConfigCommand = $this->getApplication()->find(ConfigImportCommand::COMMAND_NAME);

setup/src/Magento/Setup/Model/Declaration/Schema/Db/DbSchemaWriterInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public function dropElement($resource, $elementName, $tableName, $type);
109109
* Compile statements and make SQL request from them.
110110
*
111111
* @param StatementAggregator $statementAggregator
112+
* @param bool $dryRun
112113
* @return void
113114
*/
114-
public function compile(StatementAggregator $statementAggregator);
115+
public function compile(StatementAggregator $statementAggregator, $dryRun);
115116
}

setup/src/Magento/Setup/Model/Declaration/Schema/Db/MySQL/DbSchemaWriter.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Setup\Model\Declaration\Schema\Db\Statement;
1414
use Magento\Setup\Model\Declaration\Schema\Db\StatementAggregator;
1515
use Magento\Setup\Model\Declaration\Schema\Db\StatementFactory;
16+
use Magento\Setup\Model\Declaration\Schema\DryRunLogger;
1617
use Magento\Setup\Model\Declaration\Schema\Dto\Column;
1718
use Magento\Setup\Model\Declaration\Schema\Dto\Constraint;
1819
use Magento\Setup\Model\Declaration\Schema\Dto\Constraints\Reference;
@@ -43,18 +44,26 @@ class DbSchemaWriter implements DbSchemaWriterInterface
4344
*/
4445
private $statementFactory;
4546

47+
/**
48+
* @var DryRunLogger
49+
*/
50+
private $dryRunLogger;
51+
4652
/**
4753
* Constructor.
4854
*
4955
* @param ResourceConnection $resourceConnection
5056
* @param StatementFactory $statementFactory
57+
* @param DryRunLogger $dryRunLogger
5158
*/
5259
public function __construct(
5360
ResourceConnection $resourceConnection,
54-
StatementFactory $statementFactory
61+
StatementFactory $statementFactory,
62+
DryRunLogger $dryRunLogger
5563
) {
5664
$this->resourceConnection = $resourceConnection;
5765
$this->statementFactory = $statementFactory;
66+
$this->dryRunLogger = $dryRunLogger;
5867
}
5968

6069
/**
@@ -217,26 +226,36 @@ public function resetAutoIncrement($tableName, $resource)
217226
/**
218227
* @inheritdoc
219228
*/
220-
public function compile(StatementAggregator $statementAggregator)
229+
public function compile(StatementAggregator $statementAggregator, $dryRun)
221230
{
222231
foreach ($statementAggregator->getStatementsBank() as $statementBank) {
223232
$statementsSql = [];
224233
/** @var Statement $statement */
225234
foreach ($statementBank as $statement) {
226235
$statementsSql[] = $statement->getStatement();
227236
}
228-
229237
$adapter = $this->resourceConnection->getConnection($statement->getResource());
230-
$adapter->query(
231-
sprintf(
232-
$this->statementDirectives[$statement->getType()],
233-
$adapter->quoteIdentifier($statement->getTableName()),
234-
implode(", ", $statementsSql)
235-
)
236-
);
237-
//Do post update, like SQL DML operations or etc...
238-
foreach ($statement->getTriggers() as $trigger) {
239-
call_user_func($trigger);
238+
239+
if ($dryRun) {
240+
$this->dryRunLogger->log(
241+
sprintf(
242+
$this->statementDirectives[$statement->getType()],
243+
$adapter->quoteIdentifier($statement->getTableName()),
244+
implode(", ", $statementsSql)
245+
)
246+
);
247+
} else {
248+
$adapter->query(
249+
sprintf(
250+
$this->statementDirectives[$statement->getType()],
251+
$adapter->quoteIdentifier($statement->getTableName()),
252+
implode(", ", $statementsSql)
253+
)
254+
);
255+
//Do post update, like SQL DML operations or etc...
256+
foreach ($statement->getTriggers() as $trigger) {
257+
call_user_func($trigger);
258+
}
240259
}
241260
}
242261
}

0 commit comments

Comments
 (0)