Skip to content

Commit 5df1c20

Browse files
committed
MAGETWO-95595: Index names are ignored by declarative schema
1 parent 8bd8b4d commit 5df1c20

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

app/code/Magento/Developer/Console/Command/TablesWhitelistGenerateCommand.php

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
use Magento\Framework\Component\ComponentRegistrar;
1111
use Magento\Framework\Config\FileResolverByModule;
1212
use Magento\Framework\Module\Dir;
13+
use Magento\Framework\Setup\Declaration\Schema\Declaration\SchemaBuilder;
1314
use Magento\Framework\Setup\Declaration\Schema\Diff\Diff;
15+
use Magento\Framework\Setup\Declaration\Schema\Dto\Schema;
16+
use Magento\Framework\Setup\Declaration\Schema\Dto\SchemaFactory;
1417
use Magento\Framework\Setup\JsonPersistor;
1518
use Magento\Framework\Setup\Declaration\Schema\Declaration\ReaderComposite;
1619
use Symfony\Component\Console\Command\Command;
@@ -50,22 +53,38 @@ class TablesWhitelistGenerateCommand extends Command
5053
*/
5154
private $primaryDbSchema;
5255

56+
/**
57+
* @var SchemaFactory
58+
*/
59+
private $schemaFactory;
60+
61+
/**
62+
* @var SchemaBuilder
63+
*/
64+
private $schemaBuilder;
65+
5366
/**
5467
* @param ComponentRegistrar $componentRegistrar
5568
* @param ReaderComposite $readerComposite
5669
* @param JsonPersistor $jsonPersistor
70+
* @param SchemaFactory $schemaFactory
71+
* @param SchemaBuilder $schemaBuilder
5772
* @param string|null $name
5873
*/
5974
public function __construct(
6075
ComponentRegistrar $componentRegistrar,
6176
ReaderComposite $readerComposite,
6277
JsonPersistor $jsonPersistor,
78+
SchemaFactory $schemaFactory,
79+
SchemaBuilder $schemaBuilder,
6380
$name = null
6481
) {
6582
parent::__construct($name);
6683
$this->componentRegistrar = $componentRegistrar;
6784
$this->readerComposite = $readerComposite;
6885
$this->jsonPersistor = $jsonPersistor;
86+
$this->schemaFactory = $schemaFactory;
87+
$this->schemaBuilder = $schemaBuilder;
6988
}
7089

7190
/**
@@ -98,6 +117,7 @@ protected function configure()
98117
*
99118
* @param string $moduleName
100119
* @return void
120+
* @throws \Magento\Framework\Setup\Exception
101121
*/
102122
private function persistModule($moduleName)
103123
{
@@ -113,15 +133,20 @@ private function persistModule($moduleName)
113133
$content = json_decode(file_get_contents($whiteListFileName), true);
114134
}
115135

116-
$newContent = $this->filterPrimaryTables($this->readerComposite->read($moduleName));
136+
$schema = $this->schemaFactory->create();
137+
$data = $this->filterPrimaryTables($this->readerComposite->read($moduleName));
138+
if (isset($data['table'])) {
139+
$this->schemaBuilder->addTablesData($data['table']);
140+
$schema = $this->schemaBuilder->build($schema);
117141

118-
//Do merge between what we have before, and what we have now and filter to only certain attributes.
119-
$content = array_replace_recursive(
120-
$content,
121-
$this->filterAttributeNames($newContent)
122-
);
123-
if (!empty($content)) {
124-
$this->jsonPersistor->persist($content, $whiteListFileName);
142+
//Do merge between what we have before, and what we have now and filter to only certain attributes.
143+
$content = array_replace_recursive(
144+
$content,
145+
$this->getDeclaredContent($schema)
146+
);
147+
if (!empty($content)) {
148+
$this->jsonPersistor->persist($content, $whiteListFileName);
149+
}
125150
}
126151
}
127152

@@ -149,27 +174,30 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
149174
}
150175

151176
/**
152-
* Filter attribute names
177+
* Convert Schema into a whitelist structure.
153178
*
154179
* As for whitelist we do not need any specific attributes like nullable or indexType, we need to choose only names.
155180
*
156-
* @param array $content
181+
* @param Schema $schema
157182
* @return array
158183
*/
159-
private function filterAttributeNames(array $content) : array
184+
private function getDeclaredContent(Schema $schema) : array
160185
{
161186
$names = [];
162-
$types = ['column', 'index', 'constraint'];
163-
164-
foreach ($content['table'] as $tableName => $tableContent) {
165-
foreach ($types as $type) {
166-
if (isset($tableContent[$type])) {
167-
//Add elements to whitelist
168-
foreach (array_keys($tableContent[$type]) as $elementName) {
169-
//Depends on flag column will be whitelisted or not
170-
$names[$tableName][$type][$elementName] = true;
171-
}
172-
}
187+
foreach ($schema->getTables() as $tableName => $table) {
188+
$columns = array_keys($table->getColumns());
189+
if ($columns) {
190+
$names[$tableName]['column'] = array_fill_keys($columns, true);
191+
}
192+
193+
$indexes = array_keys($table->getIndexes());
194+
if ($indexes) {
195+
$names[$tableName]['index'] = array_fill_keys($indexes, true);
196+
}
197+
198+
$constraints = array_keys($table->getConstraints());
199+
if ($constraints) {
200+
$names[$tableName]['constraint'] = array_fill_keys($constraints, true);
173201
}
174202
}
175203

dev/tests/setup-integration/_files/Magento/TestSetupDeclarationModule1/etc/db_schema.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@
5353
<column xsi:type="boolean" name="boolean"/>
5454
<column xsi:type="varbinary" name="varbinary_rename" default="10101" disabled="true"/>
5555
<!--Constraints-->
56-
<constraint xsi:type="unique" referenceId="TEST_TABLE_SMALLINT_BIGINT">
56+
<constraint xsi:type="unique" referenceId="TEST_TABLE_UNIQUE">
5757
<column name="smallint"/>
5858
<column name="bigint"/>
5959
</constraint>
60-
<constraint xsi:type="foreign" referenceId="TEST_TABLE_TINYINT_REFERENCE_TABLE_TINYINT_REF"
60+
<constraint xsi:type="foreign" referenceId="TEST_TABLE_TINYINT_REFERENCE"
6161
column="tinyint" table="test_table"
6262
referenceTable="reference_table" referenceColumn="tinyint_ref" onDelete="NO ACTION"/>
6363
<!--Indexes-->
64-
<index referenceId="TEST_TABLE_TINYINT_BIGINT" indexType="btree">
64+
<index referenceId="TEST_TABLE_INDEX" indexType="btree">
6565
<column name="tinyint"/>
6666
<column name="bigint"/>
6767
</index>

dev/tests/setup-integration/testsuite/Magento/Developer/Console/Command/TablesWhitelistGenerateCommandTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function setUp()
6565
*
6666
* @moduleName Magento_TestSetupDeclarationModule1
6767
* @dataProvider contentsDataProvider
68+
* @throws \Exception
6869
*/
6970
public function testExecute(array $expectedWhitelistContent)
7071
{
@@ -119,6 +120,7 @@ public function contentsDataProvider(): array
119120
'constraint' =>
120121
[
121122
'tinyint_primary' => true,
123+
'PRIMARY' => true,
122124
],
123125
],
124126
'auto_increment_test' =>

0 commit comments

Comments
 (0)