Skip to content

Commit 875b18e

Browse files
committed
MAGETWO-88814: [Performance] Some indexes become recreated on db with prefixes
1 parent c5f4f61 commit 875b18e

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Setup\Declaration;
9+
10+
use Magento\Framework\Component\ComponentRegistrar;
11+
use Magento\Framework\Component\ComponentRegistrarInterface;
12+
use Magento\Framework\Setup\Declaration\Schema\Dto\Constraint;
13+
use Magento\Framework\Setup\Declaration\Schema\Dto\Index;
14+
use Magento\Framework\Setup\Declaration\Schema\Dto\Schema;
15+
use Magento\Framework\Setup\Declaration\Schema\SchemaConfigInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
18+
/**
19+
* Class WhitelistDeclarationTest
20+
*/
21+
class WhitelistDeclarationTest extends \PHPUnit\Framework\TestCase
22+
{
23+
/**
24+
* @var ComponentRegistrarInterface
25+
*/
26+
private $componentRegistrar;
27+
/**
28+
* @var Schema
29+
*/
30+
private $declarativeSchema;
31+
32+
public function setUp()
33+
{
34+
$objectManager = Bootstrap::getObjectManager();
35+
$schemaConfig = $objectManager->get(SchemaConfigInterface::class);
36+
$this->declarativeSchema = $schemaConfig->getDeclarationConfig();
37+
$this->componentRegistrar = $objectManager->get(ComponentRegistrarInterface::class);
38+
}
39+
40+
/**
41+
* Checks that all declared table elements also declared into whitelist declaration.
42+
*
43+
* @throws \Exception
44+
*/
45+
public function testConstraintsAndIndexesAreWhitelisted()
46+
{
47+
$undeclaredElements = [];
48+
$resultMessage = "New table elements that do not exist in the whitelist declaration:\n";
49+
$whitelistTables = $this->getWhiteListTables();
50+
51+
foreach ($this->declarativeSchema->getTables() as $schemaTable) {
52+
$tableNameWithoutPrefix = $schemaTable->getNameWithoutPrefix();
53+
foreach ($schemaTable->getConstraints() as $constraint) {
54+
$constraintNameWithoutPrefix = $constraint->getNameWithoutPrefix();
55+
if (isset($whitelistTables[$tableNameWithoutPrefix][Constraint::TYPE][$constraintNameWithoutPrefix])) {
56+
continue;
57+
}
58+
59+
$undeclaredElements[$tableNameWithoutPrefix][Constraint::TYPE][] = $constraintNameWithoutPrefix;
60+
}
61+
62+
foreach ($schemaTable->getIndexes() as $index) {
63+
$indexNameWithoutPrefix = $index->getNameWithoutPrefix();
64+
if (isset($whitelistTables[$tableNameWithoutPrefix][Index::TYPE][$indexNameWithoutPrefix])) {
65+
continue;
66+
}
67+
68+
$undeclaredElements[$tableNameWithoutPrefix][Index::TYPE][] = $indexNameWithoutPrefix;
69+
}
70+
}
71+
72+
$undeclaredElements = $this->filterUndeclaredElements($undeclaredElements);
73+
74+
if (!empty($undeclaredElements)) {
75+
$resultMessage .= json_encode($undeclaredElements, JSON_PRETTY_PRINT);
76+
}
77+
78+
$this->assertEmpty($undeclaredElements, $resultMessage);
79+
}
80+
81+
/**
82+
* Excludes ignored elements from the list of undeclared table elements.
83+
*
84+
* @param array $undeclaredElements
85+
* @return array
86+
*/
87+
private function filterUndeclaredElements(array $undeclaredElements): array
88+
{
89+
$ignoredElements = json_decode(file_get_contents(__DIR__ . '/_files/ignore_whitelisting.json'), true);
90+
91+
return $this->arrayRecursiveDiff($undeclaredElements, $ignoredElements);
92+
}
93+
94+
/**
95+
* Performs a recursive comparison of two arrays.
96+
*
97+
* @param array $array1
98+
* @param array $array2
99+
* @return array
100+
*/
101+
private function arrayRecursiveDiff(array $array1, array $array2): array
102+
{
103+
$diffResult = [];
104+
105+
foreach ($array1 as $key => $value) {
106+
if (array_key_exists($key, $array2)) {
107+
if (is_array($value)) {
108+
$recursiveDiffResult = $this->arrayRecursiveDiff($value, $array2[$key]);
109+
if (count($recursiveDiffResult)) {
110+
$diffResult[$key] = $recursiveDiffResult;
111+
}
112+
} else {
113+
if (!in_array($value, $array2)) {
114+
$diffResult[] = $value;
115+
}
116+
}
117+
} else {
118+
$diffResult[$key] = $value;
119+
}
120+
}
121+
122+
return $diffResult;
123+
}
124+
125+
/**
126+
* @return array
127+
*/
128+
private function getWhiteListTables(): array
129+
{
130+
$whiteListTables = [];
131+
132+
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $path) {
133+
$whiteListPath = $path . DIRECTORY_SEPARATOR . 'etc' .
134+
DIRECTORY_SEPARATOR . 'db_schema_whitelist.json';
135+
136+
if (file_exists($whiteListPath)) {
137+
$whiteListTables = array_replace_recursive(
138+
$whiteListTables,
139+
json_decode(file_get_contents($whiteListPath), true)
140+
);
141+
}
142+
}
143+
144+
return $whiteListTables;
145+
}
146+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)