Skip to content

Commit 67749dc

Browse files
committed
ACP2E-3709: [Cloud] Changing table column type from Int to Decimal using db_schema.xml file In Magento 2 Results In Errors
1 parent 518e799 commit 67749dc

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

app/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,7 @@
15791579
<item name="/schema/table/index/column" xsi:type="string">name</item>
15801580
<item name="/schema/table/constraint/column" xsi:type="string">name</item>
15811581
</argument>
1582+
<argument name="typeAttributeName" xsi:type="string">xsi:type</argument>
15821583
</arguments>
15831584
</virtualType>
15841585
<type name="Magento\Framework\Setup\Declaration\Schema\OperationsExecutor">

lib/internal/Magento/Framework/Config/Reader/Filesystem.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright 2014 Adobe
4-
* All Rights Reserved.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66

77
namespace Magento\Framework\Config\Reader;
@@ -83,8 +83,10 @@ class Filesystem implements \Magento\Framework\Config\ReaderInterface
8383

8484
/**
8585
* Name of an attribute that stands for data type of node values
86+
*
87+
* @var string
8688
*/
87-
private const TYPE_ATTRIBUTE = 'xsi:type';
89+
protected $_typeAttributeName;
8890

8991
/**
9092
* Constructor
@@ -97,6 +99,7 @@ class Filesystem implements \Magento\Framework\Config\ReaderInterface
9799
* @param array $idAttributes
98100
* @param string $domDocumentClass
99101
* @param string $defaultScope
102+
* @param string $typeAttributeName
100103
*/
101104
public function __construct(
102105
\Magento\Framework\Config\FileResolverInterface $fileResolver,
@@ -106,7 +109,8 @@ public function __construct(
106109
$fileName,
107110
$idAttributes = [],
108111
$domDocumentClass = \Magento\Framework\Config\Dom::class,
109-
$defaultScope = 'global'
112+
$defaultScope = 'global',
113+
$typeAttributeName = null,
110114
) {
111115
$this->_fileResolver = $fileResolver;
112116
$this->_converter = $converter;
@@ -118,6 +122,7 @@ public function __construct(
118122
? $schemaLocator->getPerFileSchema() : null;
119123
$this->_domDocumentClass = $domDocumentClass;
120124
$this->_defaultScope = $defaultScope;
125+
$this->_typeAttributeName = $typeAttributeName;
121126
}
122127

123128
/**
@@ -153,6 +158,9 @@ protected function _readFiles($fileList)
153158
foreach ($fileList as $key => $content) {
154159
try {
155160
if (!$configMerger) {
161+
if ($this->isDbSchemaFile($key)) {
162+
$this->_typeAttributeName = 'xsi:type';
163+
}
156164
$configMerger = $this->_createConfigMerger($this->_domDocumentClass, $content);
157165
} else {
158166
$configMerger->merge($content);
@@ -176,6 +184,9 @@ protected function _readFiles($fileList)
176184
$configMerger = null;
177185
foreach ($fileList as $key => $content) {
178186
if (!$configMerger) {
187+
if ($this->isDbSchemaFile($key)) {
188+
$this->_typeAttributeName = 'xsi:type';
189+
}
179190
$configMerger = $this->_createConfigMerger($this->_domDocumentClass, $content);
180191
} else {
181192
$configMerger->merge($content);
@@ -206,6 +217,7 @@ protected function _readFiles($fileList)
206217
*
207218
* @param string $mergerClass
208219
* @param string $initialContents
220+
* @param string $typeAttributeName
209221
* @return \Magento\Framework\Config\Dom
210222
* @throws \UnexpectedValueException
211223
*/
@@ -215,7 +227,7 @@ protected function _createConfigMerger($mergerClass, $initialContents)
215227
$initialContents,
216228
$this->validationState,
217229
$this->_idAttributes,
218-
self::TYPE_ATTRIBUTE,
230+
$this->_typeAttributeName,
219231
$this->_perFileSchema
220232
);
221233
if (!$result instanceof \Magento\Framework\Config\Dom) {
@@ -225,4 +237,16 @@ protected function _createConfigMerger($mergerClass, $initialContents)
225237
}
226238
return $result;
227239
}
240+
241+
/**
242+
* Check schema file, return true if it is db_schema.xml
243+
*
244+
* @param string $filePath
245+
* @return bool
246+
*/
247+
private function isDbSchemaFile(string $filePath): bool {
248+
// Check only if "db_schema.xml" is at the very end of the path
249+
$pattern = '/db_schema\.xml$/';
250+
return preg_match($pattern, $filePath) === 1;
251+
}
228252
}

lib/internal/Magento/Framework/Config/Test/Unit/Reader/FilesystemTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function testReadException()
187187
$model->read();
188188
}
189189

190-
public function testCreateConfigMergerSuccess()
190+
public function testCreateConfigMergerWithTypeAttributeSuccess()
191191
{
192192
$initialContents = '<?xml version="1.0"?><config><test id="1"/></config>';
193193
$reflection = new \ReflectionClass($this->filesystem);
@@ -202,16 +202,14 @@ public function testCreateConfigMergerSuccess()
202202
$this->assertEquals('xsi:type', $typeAttributeProperty->getValue($result));
203203
}
204204

205-
public function testCreateConfigMergerThrowsException()
205+
public function testCreateConfigMergerWithTypeAttributeThrowsException()
206206
{
207207
$initialContents = '<?xml version="1.0"?><config><test id="1"/></config>';
208208
$wrongClass = \stdClass::class;
209209
$reflection = new \ReflectionClass($this->filesystem);
210210
$method = $reflection->getMethod('_createConfigMerger');
211211
$this->expectException(\UnexpectedValueException::class);
212-
$this->expectExceptionMessage(
213-
"Instance of the DOM config merger is expected, got {$wrongClass} instead."
214-
);
212+
$this->expectExceptionMessage("Instance of the DOM config merger is expected, got {$wrongClass} instead.");
215213
$method->invokeArgs($this->filesystem, [$wrongClass, $initialContents]);
216214
}
217215
}

0 commit comments

Comments
 (0)