Skip to content

Commit de9fbe2

Browse files
authored
ENGCOM-8029: Fix setup upgrade command re enables caches #28491
2 parents b87c1b7 + a85f600 commit de9fbe2

File tree

3 files changed

+163
-28
lines changed

3 files changed

+163
-28
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
144144
$searchConfig->validateSearchEngine();
145145
$installer->removeUnusedTriggers();
146146
$installer->installSchema($request);
147-
$installer->installDataFixtures($request);
147+
$installer->installDataFixtures($request, true);
148148

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

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

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

99
use Magento\Backend\Setup\ConfigOptionsList as BackendConfigOptionsList;
1010
use Magento\Framework\App\Cache\Manager;
11+
use Magento\Framework\App\Cache\Manager as CacheManager;
1112
use Magento\Framework\App\Cache\Type\Block as BlockCache;
1213
use Magento\Framework\App\Cache\Type\Config as ConfigCache;
1314
use Magento\Framework\App\Cache\Type\Layout as LayoutCache;
@@ -920,18 +921,25 @@ private function convertationOfOldScriptsIsAllowed(array $request)
920921
* Installs data fixtures
921922
*
922923
* @param array $request
924+
* @param boolean $keepCacheStatuses
923925
* @return void
924926
* @throws Exception
925927
* @throws \Magento\Framework\Setup\Exception
926928
*/
927-
public function installDataFixtures(array $request = [])
929+
public function installDataFixtures(array $request = [], $keepCacheStatuses = false)
928930
{
929931
$frontendCaches = [
930932
PageCache::TYPE_IDENTIFIER,
931933
BlockCache::TYPE_IDENTIFIER,
932934
LayoutCache::TYPE_IDENTIFIER,
933935
];
934936

937+
if ($keepCacheStatuses) {
938+
$disabledCaches = $this->getDisabledCacheTypes($frontendCaches);
939+
940+
$frontendCaches = array_diff($frontendCaches, $disabledCaches);
941+
}
942+
935943
/** @var \Magento\Framework\Registry $registry */
936944
$registry = $this->objectManagerProvider->get()->get(\Magento\Framework\Registry::class);
937945
//For backward compatibility in install and upgrade scripts with enabled parallelization.
@@ -942,11 +950,20 @@ public function installDataFixtures(array $request = [])
942950
$setup = $this->dataSetupFactory->create();
943951
$this->checkFilePermissionsForDbUpgrade();
944952
$this->log->log('Data install/update:');
945-
$this->log->log('Disabling caches:');
946-
$this->updateCaches(false, $frontendCaches);
947-
$this->handleDBSchemaData($setup, 'data', $request);
948-
$this->log->log('Enabling caches:');
949-
$this->updateCaches(true, $frontendCaches);
953+
954+
if ($frontendCaches) {
955+
$this->log->log('Disabling caches:');
956+
$this->updateCaches(false, $frontendCaches);
957+
}
958+
959+
try {
960+
$this->handleDBSchemaData($setup, 'data', $request);
961+
} finally {
962+
if ($frontendCaches) {
963+
$this->log->log('Enabling caches:');
964+
$this->updateCaches(true, $frontendCaches);
965+
}
966+
}
950967

951968
$registry->unregister('setup-mode-enabled');
952969
}
@@ -995,7 +1012,7 @@ private function throwExceptionForNotWritablePaths(array $paths)
9951012
*/
9961013
private function handleDBSchemaData($setup, $type, array $request)
9971014
{
998-
if (!($type === 'schema' || $type === 'data')) {
1015+
if ($type !== 'schema' && $type !== 'data') {
9991016
// phpcs:ignore Magento2.Exceptions.DirectThrow
10001017
throw new Exception("Unsupported operation type $type is requested");
10011018
}
@@ -1014,17 +1031,13 @@ private function handleDBSchemaData($setup, $type, array $request)
10141031
'objectManager' => $this->objectManagerProvider->get()
10151032
]
10161033
);
1034+
1035+
$patchApplierParams = $type === 'schema' ?
1036+
['schemaSetup' => $setup] :
1037+
['moduleDataSetup' => $setup, 'objectManager' => $this->objectManagerProvider->get()];
1038+
10171039
/** @var PatchApplier $patchApplier */
1018-
if ($type === 'schema') {
1019-
$patchApplier = $this->patchApplierFactory->create(['schemaSetup' => $setup]);
1020-
} elseif ($type === 'data') {
1021-
$patchApplier = $this->patchApplierFactory->create(
1022-
[
1023-
'moduleDataSetup' => $setup,
1024-
'objectManager' => $this->objectManagerProvider->get()
1025-
]
1026-
);
1027-
}
1040+
$patchApplier = $this->patchApplierFactory->create($patchApplierParams);
10281041

10291042
foreach ($moduleNames as $moduleName) {
10301043
if ($this->isDryRun($request)) {
@@ -1086,11 +1099,11 @@ private function handleDBSchemaData($setup, $type, array $request)
10861099

10871100
if ($type === 'schema') {
10881101
$this->log->log('Schema post-updates:');
1089-
$handlerType = 'schema-recurring';
10901102
} elseif ($type === 'data') {
10911103
$this->log->log('Data post-updates:');
1092-
$handlerType = 'data-recurring';
10931104
}
1105+
$handlerType = $type === 'schema' ? 'schema-recurring' : 'data-recurring';
1106+
10941107
foreach ($moduleNames as $moduleName) {
10951108
if ($this->isDryRun($request)) {
10961109
$this->log->log("Module '{$moduleName}':");
@@ -1726,4 +1739,27 @@ public function removeUnusedTriggers(): void
17261739
$this->triggerCleaner->removeTriggers();
17271740
$this->cleanCaches();
17281741
}
1742+
1743+
/**
1744+
* Returns list of disabled cache types
1745+
*
1746+
* @param array $cacheTypesToCheck
1747+
* @return array
1748+
*/
1749+
private function getDisabledCacheTypes(array $cacheTypesToCheck): array
1750+
{
1751+
$disabledCaches = [];
1752+
1753+
/** @var CacheManager $cacheManager */
1754+
$cacheManager = $this->objectManagerProvider->get()->create(CacheManager::class);
1755+
$cacheStatus = $cacheManager->getStatus();
1756+
1757+
foreach ($cacheTypesToCheck as $cacheType) {
1758+
if (isset($cacheStatus[$cacheType]) && $cacheStatus[$cacheType] === 0) {
1759+
$disabledCaches[] = $cacheType;
1760+
}
1761+
}
1762+
1763+
return $disabledCaches;
1764+
}
17291765
}

setup/src/Magento/Setup/Test/Unit/Model/InstallerTest.php

Lines changed: 107 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,18 @@
6262
class InstallerTest extends TestCase
6363
{
6464
/**
65-
* @var \Magento\Setup\Model\Installer
65+
* @var array
66+
*/
67+
private $request = [
68+
ConfigOptionsListConstants::INPUT_KEY_DB_HOST => '127.0.0.1',
69+
ConfigOptionsListConstants::INPUT_KEY_DB_NAME => 'magento',
70+
ConfigOptionsListConstants::INPUT_KEY_DB_USER => 'magento',
71+
ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY => 'encryption_key',
72+
ConfigOptionsList::INPUT_KEY_BACKEND_FRONTNAME => 'backend',
73+
];
74+
75+
/**
76+
* @var Installer
6677
*/
6778
private $object;
6879

@@ -426,13 +437,7 @@ public function installDataProvider()
426437
{
427438
return [
428439
[
429-
'request' => [
430-
ConfigOptionsListConstants::INPUT_KEY_DB_HOST => '127.0.0.1',
431-
ConfigOptionsListConstants::INPUT_KEY_DB_NAME => 'magento',
432-
ConfigOptionsListConstants::INPUT_KEY_DB_USER => 'magento',
433-
ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY => 'encryption_key',
434-
ConfigOptionsList::INPUT_KEY_BACKEND_FRONTNAME => 'backend',
435-
],
440+
'request' => $this->request,
436441
'logMessages' => [
437442
['Starting Magento installation:'],
438443
['File permissions check...'],
@@ -526,6 +531,100 @@ public function installDataProvider()
526531
];
527532
}
528533

534+
/**
535+
* Test for InstallDataFixtures
536+
*
537+
* @dataProvider testInstallDataFixturesDataProvider
538+
*
539+
* @param bool $keepCache
540+
* @param array $expectedToEnableCacheTypes
541+
* @return void
542+
*/
543+
public function testInstallDataFixtures(bool $keepCache, array $expectedToEnableCacheTypes): void
544+
{
545+
$cacheManagerMock = $this->createMock(Manager::class);
546+
//simulate disabled layout cache type
547+
$cacheManagerMock->expects($this->atLeastOnce())
548+
->method('getStatus')
549+
->willReturn(['layout' => 0]);
550+
$cacheManagerMock->expects($this->atLeastOnce())
551+
->method('getAvailableTypes')
552+
->willReturn(['block_html', 'full_page', 'layout' , 'config', 'collections']);
553+
$cacheManagerMock->expects($this->exactly(2))
554+
->method('setEnabled')
555+
->withConsecutive([$expectedToEnableCacheTypes, false], [$expectedToEnableCacheTypes, true])
556+
->willReturn([]);
557+
558+
$this->objectManager->expects($this->atLeastOnce())
559+
->method('create')
560+
->willReturnMap([
561+
[Manager::class, [], $cacheManagerMock],
562+
[
563+
PatchApplierFactory::class,
564+
['objectManager' => $this->objectManager],
565+
$this->patchApplierFactoryMock
566+
],
567+
]);
568+
569+
$registryMock = $this->createMock(Registry::class);
570+
$this->objectManager->expects($this->atLeastOnce())
571+
->method('get')
572+
->with(Registry::class)
573+
->willReturn($registryMock);
574+
575+
$this->config->expects($this->atLeastOnce())
576+
->method('get')
577+
->willReturn(true);
578+
579+
$this->filePermissions->expects($this->atLeastOnce())
580+
->method('getMissingWritableDirectoriesForDbUpgrade')
581+
->willReturn([]);
582+
583+
$connection = $this->getMockBuilder(AdapterInterface::class)
584+
->addMethods(['getSchemaListener'])
585+
->getMockForAbstractClass();
586+
$connection->expects($this->once())
587+
->method('getSchemaListener')
588+
->willReturn($this->schemaListenerMock);
589+
590+
$resource = $this->createMock(ResourceConnection::class);
591+
$resource->expects($this->atLeastOnce())
592+
->method('getConnection')
593+
->willReturn($connection);
594+
$this->contextMock->expects($this->once())
595+
->method('getResources')
596+
->willReturn($resource);
597+
598+
$dataSetup = $this->createMock(DataSetup::class);
599+
$dataSetup->expects($this->once())
600+
->method('getConnection')
601+
->willReturn($connection);
602+
603+
$this->dataSetupFactory->expects($this->atLeastOnce())
604+
->method('create')
605+
->willReturn($dataSetup);
606+
607+
$this->object->installDataFixtures($this->request, $keepCache);
608+
}
609+
610+
/**
611+
* DataProvider for testInstallDataFixtures
612+
*
613+
* @return array
614+
*/
615+
public function testInstallDataFixturesDataProvider(): array
616+
{
617+
return [
618+
'keep cache' => [
619+
true, ['block_html', 'full_page']
620+
],
621+
'do not keep a cache' => [
622+
false,
623+
['block_html', 'full_page', 'layout']
624+
],
625+
];
626+
}
627+
529628
public function testCheckInstallationFilePermissions()
530629
{
531630
$this->filePermissions

0 commit comments

Comments
 (0)