Skip to content

Commit 639ae40

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 a8fbac0 commit 639ae40

File tree

8 files changed

+131
-49
lines changed

8 files changed

+131
-49
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
10+
<module name="Magento_TestSetupDeclarationModule11" setup_version="1.0.0" />
11+
</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_TestSetupDeclarationModule11') === null) {
12+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_TestSetupDeclarationModule11', __DIR__);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
4+
<table name="module11_test_main_table" resource="default" engine="innodb" comment="Main Test Table for Module11">
5+
<column xsi:type="int" name="module11_email_contact_id" padding="10" unsigned="true" nullable="false"
6+
identity="true" comment="Entity ID"/>
7+
<column xsi:type="int" name="module11_guest_score" padding="5" unsigned="true" nullable="true"
8+
identity="false" comment="Guest Score"/>
9+
<column xsi:type="int" name="module11_guest_id" padding="10" unsigned="true" nullable="true" identity="false"
10+
comment="Guest ID"/>
11+
<column xsi:type="date" name="module11_created_at" comment="Created At"/>
12+
<constraint xsi:type="primary" referenceId="PRIMARY">
13+
<column name="module11_email_contact_id"/>
14+
</constraint>
15+
<constraint xsi:type="unique" referenceId="MODULE11_INSTALL_UNIQUE_INDEX_1">
16+
<column name="module11_email_contact_id"/>
17+
<column name="module11_guest_id"/>
18+
</constraint>
19+
</table>
20+
</schema>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
4+
<table name="module11_test_main_table" resource="default" engine="innodb" comment="Main Test Table for Module11">
5+
<column xsi:type="int" name="module11_email_contact_id" padding="10" unsigned="true" nullable="false"
6+
identity="true" comment="Entity ID"/>
7+
<column xsi:type="decimal" name="module11_guest_score" scale="4" precision="20" comment="Guest Score"/>
8+
<column xsi:type="int" name="module11_guest_id" padding="10" unsigned="true" nullable="true" identity="false"
9+
comment="Guest ID"/>
10+
<column xsi:type="date" name="module11_created_at" comment="Created At"/>
11+
<constraint xsi:type="primary" referenceId="PRIMARY">
12+
<column name="module11_email_contact_id"/>
13+
</constraint>
14+
<constraint xsi:type="unique" referenceId="MODULE11_INSTALL_UNIQUE_INDEX_1">
15+
<column name="module11_email_contact_id"/>
16+
<column name="module11_guest_id"/>
17+
</constraint>
18+
</table>
19+
</schema>

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

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2019 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -95,6 +95,32 @@ public function testUpgradeWithConverting()
9595
}
9696
}
9797

98+
/**
99+
* Test upgrading a module with a modified db_schema.xml column type.
100+
*
101+
* @moduleName Magento_TestSetupDeclarationModule11
102+
* @throws \Exception
103+
*/
104+
public function testUpgradeWithColumnTypeChange()
105+
{
106+
$moduleName = 'Magento_TestSetupDeclarationModule11';
107+
$this->moduleManager->updateRevision(
108+
$moduleName,
109+
'initial_install',
110+
'db_schema.xml',
111+
'etc'
112+
);
113+
$this->cliCommand->install([$moduleName]);
114+
$this->moduleManager->updateRevision(
115+
$moduleName,
116+
'upgrade_column_type',
117+
'db_schema.xml',
118+
'etc'
119+
);
120+
$this->cliCommand->upgrade();
121+
122+
$this->assertSchemaColumnTypeChange($moduleName);
123+
}
98124
/**
99125
* Convert file content in the DOM document.
100126
*
@@ -170,4 +196,21 @@ private function getSchemaFixturePath(string $moduleName, string $suffix): strin
170196

171197
return $schemaFixturePath;
172198
}
199+
200+
/**
201+
* Assert that the column type has changed as expected.
202+
*
203+
* @param string $moduleName
204+
*/
205+
private function assertSchemaColumnTypeChange(string $moduleName): void
206+
{
207+
$generatedSchema = $this->getGeneratedSchema($moduleName);
208+
$expectedSchema = $this->getSchemaDocument($this->getSchemaFixturePath($moduleName, 'upgrade'));
209+
210+
$this->assertEquals(
211+
$expectedSchema->saveXML(),
212+
$generatedSchema->saveXML(),
213+
"The generated db_schema.xml does not match the expected schema after upgrade."
214+
);
215+
}
173216
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
4+
<table name="module11_test_main_table" resource="default" engine="innodb" comment="Main Test Table for Module11">
5+
<column xsi:type="int" name="module11_email_contact_id" padding="10" unsigned="true" nullable="false"
6+
identity="true" comment="Entity ID"/>
7+
<column xsi:type="decimal" name="module11_guest_score" scale="4" precision="20" comment="Guest Score"/>
8+
<column xsi:type="int" name="module11_guest_id" padding="10" unsigned="true" nullable="true" identity="false"
9+
comment="Guest ID"/>
10+
<column xsi:type="date" name="module11_created_at" comment="Created At"/>
11+
<constraint xsi:type="primary" referenceId="PRIMARY">
12+
<column name="module11_email_contact_id"/>
13+
</constraint>
14+
<constraint xsi:type="unique" referenceId="MODULE11_INSTALL_UNIQUE_INDEX_1">
15+
<column name="module11_email_contact_id"/>
16+
<column name="module11_guest_id"/>
17+
</constraint>
18+
</table>
19+
</schema>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Filesystem implements \Magento\Framework\Config\ReaderInterface
8484
/**
8585
* Name of an attribute that stands for data type of node values
8686
*
87-
* @var string
87+
* @var string|null
8888
*/
8989
private $typeAttributeName;
9090

@@ -99,7 +99,7 @@ class Filesystem implements \Magento\Framework\Config\ReaderInterface
9999
* @param array $idAttributes
100100
* @param string $domDocumentClass
101101
* @param string $defaultScope
102-
* @param string $typeAttributeName
102+
* @param string|null $typeAttributeName
103103
*/
104104
public function __construct(
105105
\Magento\Framework\Config\FileResolverInterface $fileResolver,

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

Lines changed: 2 additions & 45 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 © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
55
*/
66
declare(strict_types=1);
77

@@ -15,7 +15,6 @@
1515
use Magento\Framework\Config\ValidationStateInterface;
1616
use PHPUnit\Framework\MockObject\MockObject;
1717
use PHPUnit\Framework\TestCase;
18-
use Magento\Framework\Config\Dom;
1918

2019
/**
2120
* Test for
@@ -54,11 +53,6 @@ class FilesystemTest extends TestCase
5453
*/
5554
protected $_file;
5655

57-
/**
58-
* @var Filesystem
59-
*/
60-
private $filesystem;
61-
6256
protected function setUp(): void
6357
{
6458
if (!function_exists('libxml_set_external_entity_loader')) {
@@ -70,17 +64,6 @@ protected function setUp(): void
7064
$this->_schemaLocatorMock = $this->getMockForAbstractClass(SchemaLocatorInterface::class);
7165
$this->_validationStateMock = $this->getMockForAbstractClass(ValidationStateInterface::class);
7266
$this->urnResolver = new UrnResolver();
73-
$this->filesystem = new Filesystem(
74-
$this->_fileResolverMock,
75-
$this->_converterMock,
76-
$this->_schemaLocatorMock,
77-
$this->_validationStateMock,
78-
'test.xml',
79-
['/test/node' => 'id'],
80-
Dom::class,
81-
'global',
82-
'xsi:type'
83-
);
8467
}
8568

8669
public function testRead()
@@ -187,30 +170,4 @@ public function testReadException()
187170
);
188171
$model->read();
189172
}
190-
191-
public function testCreateConfigMergerWithTypeAttributeSuccess()
192-
{
193-
$initialContents = '<?xml version="1.0"?><config><test id="1"/></config>';
194-
$reflection = new \ReflectionClass($this->filesystem);
195-
$method = $reflection->getMethod('_createConfigMerger');
196-
$result = $method->invokeArgs(
197-
$this->filesystem,
198-
[Dom::class, $initialContents]
199-
);
200-
$this->assertInstanceOf(\Magento\Framework\Config\Dom::class, $result);
201-
$domReflection = new \ReflectionClass($result);
202-
$typeAttributeProperty = $domReflection->getProperty('typeAttributeName');
203-
$this->assertEquals('xsi:type', $typeAttributeProperty->getValue($result));
204-
}
205-
206-
public function testCreateConfigMergerWithTypeAttributeThrowsException()
207-
{
208-
$initialContents = '<?xml version="1.0"?><config><test id="1"/></config>';
209-
$wrongClass = \stdClass::class;
210-
$reflection = new \ReflectionClass($this->filesystem);
211-
$method = $reflection->getMethod('_createConfigMerger');
212-
$this->expectException(\UnexpectedValueException::class);
213-
$this->expectExceptionMessage("Instance of the DOM config merger is expected, got {$wrongClass} instead.");
214-
$method->invokeArgs($this->filesystem, [$wrongClass, $initialContents]);
215-
}
216173
}

0 commit comments

Comments
 (0)