Skip to content

Commit 031d8a8

Browse files
committed
Merge remote-tracking branch 'magento-l3/ACP2E-1709' into MAR212023_PR_sarmistha
2 parents fe2c9b5 + 7262162 commit 031d8a8

File tree

8 files changed

+151
-24
lines changed

8 files changed

+151
-24
lines changed

app/code/Magento/Developer/Model/Setup/Declaration/Schema/WhitelistGenerator.php

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ private function persistModule(Schema $schema, string $moduleName)
136136
. Diff::GENERATED_WHITELIST_FILE_NAME;
137137

138138
//We need to load whitelist file and update it with new revision of code.
139+
// phpcs:disable Magento2.Functions.DiscouragedFunction
139140
if (file_exists($whiteListFileName)) {
140141
$content = json_decode(file_get_contents($whiteListFileName), true);
141142
}
@@ -183,6 +184,7 @@ private function getElementsWithFixedName(array $tableData): array
183184
* @param string $tableName
184185
* @param array $tableData
185186
* @return array
187+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
186188
*/
187189
private function getElementsWithAutogeneratedName(Schema $schema, string $tableName, array $tableData) : array
188190
{
@@ -192,35 +194,42 @@ private function getElementsWithAutogeneratedName(Schema $schema, string $tableN
192194
$elementType = 'index';
193195
if (!empty($tableData[$elementType])) {
194196
foreach ($tableData[$elementType] as $tableElementData) {
195-
$indexName = $this->elementNameResolver->getFullIndexName(
196-
$table,
197-
$tableElementData['column'],
198-
$tableElementData['indexType'] ?? null
199-
);
200-
$declaredStructure[$elementType][$indexName] = true;
197+
if (isset($tableElementData['column'])) {
198+
$indexName = $this->elementNameResolver->getFullIndexName(
199+
$table,
200+
$tableElementData['column'],
201+
$tableElementData['indexType'] ?? null
202+
);
203+
$declaredStructure[$elementType][$indexName] = true;
204+
}
201205
}
202206
}
203207

204208
$elementType = 'constraint';
205209
if (!empty($tableData[$elementType])) {
206210
foreach ($tableData[$elementType] as $tableElementData) {
207-
if ($tableElementData['type'] === 'foreign') {
208-
$referenceTable = $schema->getTableByName($tableElementData['referenceTable']);
209-
$column = $table->getColumnByName($tableElementData['column']);
210-
$referenceColumn = $referenceTable->getColumnByName($tableElementData['referenceColumn']);
211-
$constraintName = ($column !== false && $referenceColumn !== false) ?
212-
$this->elementNameResolver->getFullFKName(
211+
$constraintName = null;
212+
if (isset($tableElementData['type'], $tableElementData['column'])) {
213+
if ($tableElementData['type'] === 'foreign') {
214+
$column = $table->getColumnByName($tableElementData['column']);
215+
$referenceTable = $schema->getTableByName($tableElementData['referenceTable'] ?? null);
216+
$referenceColumn = ($referenceTable !== false)
217+
? $referenceTable->getColumnByName($tableElementData['referenceColumn'] ?? null) : false;
218+
219+
$constraintName = ($column !== false && $referenceColumn !== false) ?
220+
$this->elementNameResolver->getFullFKName(
221+
$table,
222+
$column,
223+
$referenceTable,
224+
$referenceColumn
225+
) : null;
226+
} else {
227+
$constraintName = $this->elementNameResolver->getFullIndexName(
213228
$table,
214-
$column,
215-
$referenceTable,
216-
$referenceColumn
217-
) : null;
218-
} else {
219-
$constraintName = $this->elementNameResolver->getFullIndexName(
220-
$table,
221-
$tableElementData['column'],
222-
$tableElementData['type']
223-
);
229+
$tableElementData['column'],
230+
$tableElementData['type']
231+
);
232+
}
224233
}
225234
if ($constraintName) {
226235
$declaredStructure[$elementType][$constraintName] = true;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
10+
<table name="test_table" resource="default">
11+
<!--Columns-->
12+
<column xsi:type="tinyint" name="tinyint" disabled="true"/>
13+
<column xsi:type="tinyint" name="customint" unsigned="false" nullable="true"
14+
onCreate="migrateDataFrom(tinyint)" comment="Version Id"/>
15+
16+
<!--Constraints-->
17+
<constraint xsi:type="foreign" referenceId="TEST_TABLE_TINYINT_REFERENCE" disabled="1"/>
18+
<constraint xsi:type="foreign" referenceId="TEST_TABLE_CUSTOMINT_REFERENCE"
19+
column="customint" table="test_table"
20+
referenceTable="reference_table" referenceColumn="tinyint_ref" onDelete="NO ACTION"/>
21+
<!--Indexes-->
22+
<index referenceId="TEST_TABLE_INDEX" indexType="btree" disabled="1"/>
23+
<index referenceId="TEST_TABLE_CUSTOMINT_INDEX" indexType="btree">
24+
<column name="customint"/>
25+
<column name="bigint"/>
26+
</index>
27+
</table>
28+
</schema>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"test_table": {
3+
"column": {
4+
"tinyint": true,
5+
"customint": true
6+
},
7+
"index": {
8+
"TEST_TABLE_TINYINT_REFERENCE": true,
9+
"TEST_TABLE_CUSTOMINT_REFERENCE": true
10+
},
11+
"constraint": {
12+
"TEST_TABLE_INDEX": true,
13+
"TEST_TABLE_CUSTOMINT_INDEX": true
14+
}
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_TestSetupDeclarationModule10" setup_version="0.0.1"/>
10+
<sequence>
11+
<module name="Magento_TestSetupDeclarationModule1"/>
12+
</sequence>
13+
</config>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
use Magento\Framework\Component\ComponentRegistrar;
9+
10+
$registrar = new ComponentRegistrar();
11+
if ($registrar->getPath(ComponentRegistrar::MODULE, 'Magento_TestSetupDeclarationModule10') === null) {
12+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_TestSetupDeclarationModule10', __DIR__);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
10+
<table name="test_table" resource="default">
11+
<!--Columns-->
12+
<column xsi:type="tinyint" name="tinyint" disabled="true"/>
13+
<column xsi:type="tinyint" name="customint" unsigned="false" nullable="true"
14+
onCreate="migrateDataFrom(tinyint)" comment="Version Id"/>
15+
16+
<!--Constraints-->
17+
<constraint xsi:type="foreign" referenceId="TEST_TABLE_TINYINT_REFERENCE" disabled="1"/>
18+
<constraint xsi:type="foreign" referenceId="TEST_TABLE_CUSTOMINT_REFERENCE"
19+
column="customint" table="test_table"
20+
referenceTable="reference_table" referenceColumn="tinyint_ref" onDelete="NO ACTION"/>
21+
<!--Indexes-->
22+
<index referenceId="TEST_TABLE_INDEX" indexType="btree" disabled="1"/>
23+
<index referenceId="TEST_TABLE_CUSTOMINT_INDEX" indexType="btree">
24+
<column name="customint"/>
25+
<column name="bigint"/>
26+
</index>
27+
</table>
28+
</schema>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ protected function setUp(): void
7070
*
7171
* @moduleName Magento_TestSetupDeclarationModule1
7272
* @moduleName Magento_TestSetupDeclarationModule8
73+
* @moduleName Magento_TestSetupDeclarationModule10
7374
* @throws \Exception
7475
*/
7576
public function testExecute()
7677
{
7778
$modules = [
7879
'Magento_TestSetupDeclarationModule1',
7980
'Magento_TestSetupDeclarationModule8',
81+
'Magento_TestSetupDeclarationModule10',
8082
];
8183

8284
$this->cliCommand->install($modules);
@@ -114,7 +116,7 @@ private function checkWhitelistFile(string $moduleName)
114116
$this->assertEmpty($this->tester->getDisplay());
115117

116118
$whitelistFileContent = file_get_contents($whiteListFileName);
117-
$expectedWhitelistContent = file_get_contents(
119+
$expectedWhitelistContent = rtrim(file_get_contents(
118120
dirname(__DIR__, 2)
119121
. DIRECTORY_SEPARATOR
120122
. implode(
@@ -126,7 +128,7 @@ private function checkWhitelistFile(string $moduleName)
126128
'db_schema_whitelist.json'
127129
]
128130
)
129-
);
131+
), "\n");
130132
$this->assertEquals($expectedWhitelistContent, $whitelistFileContent);
131133
}
132134
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"test_table": {
3+
"column": {
4+
"tinyint": true,
5+
"customint": true
6+
},
7+
"index": {
8+
"TEST_TABLE_TINYINT_REFERENCE": true,
9+
"TEST_TABLE_CUSTOMINT_REFERENCE": true,
10+
"TEST_TABLE_CUSTOMINT_BIGINT": true
11+
},
12+
"constraint": {
13+
"TEST_TABLE_INDEX": true,
14+
"TEST_TABLE_CUSTOMINT_INDEX": true,
15+
"TEST_TABLE_CUSTOMINT_REFERENCE_TABLE_TINYINT_REF": true
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)