Skip to content

Commit 2eb925b

Browse files
committed
B2B-1712: Magento setup upgrade failed when new auto incremental field added to an existing table
- add integration tests
1 parent a05bafb commit 2eb925b

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

dev/tests/integration/testsuite/Magento/Framework/DB/Adapter/Pdo/MysqlTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,67 @@ public function testCreateTableColumnWithExpressionAsColumnDefaultValue()
207207
$this->assertEquals('varchar', $stringColumn['DATA_TYPE'], 'Incorrect column type');
208208
$this->assertEquals('default test text', $stringColumn['DEFAULT'], 'Incorrect column default string value');
209209
}
210+
211+
/**
212+
* Test get auto increment field
213+
*
214+
* @param array $options
215+
* @param string|bool $expected
216+
* @throws \Zend_Db_Exception
217+
* @dataProvider getAutoIncrementFieldDataProvider
218+
*/
219+
public function testGetAutoIncrementField(array $options, $expected)
220+
{
221+
$adapter = $this->getDbAdapter();
222+
$tableName = 'table_auto_increment_field';
223+
224+
$table = $adapter
225+
->newTable($tableName)
226+
->addColumn(
227+
'row_id',
228+
Table::TYPE_INTEGER,
229+
null,
230+
$options,
231+
'Row Id'
232+
)
233+
->addColumn(
234+
'created_at',
235+
Table::TYPE_DATETIME,
236+
null,
237+
['default' => new \Zend_Db_Expr('CURRENT_TIMESTAMP')]
238+
)
239+
->addColumn(
240+
'integer_column',
241+
Table::TYPE_INTEGER,
242+
11,
243+
['default' => 123456]
244+
)->addColumn(
245+
'string_column',
246+
Table::TYPE_TEXT,
247+
255,
248+
['default' => 'default test text']
249+
)
250+
->setComment('Test table column with expression as column default value');
251+
$adapter->createTable($table);
252+
$autoIncrementField = $adapter->getAutoIncrementField($tableName);
253+
$this->assertEquals($expected, $autoIncrementField);
254+
255+
//clean up database from test table
256+
$adapter->dropTable($tableName);
257+
258+
}
259+
260+
public function getAutoIncrementFieldDataProvider()
261+
{
262+
return [
263+
'auto increment field' => [
264+
'field options' => ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
265+
'expected result' => 'row_id',
266+
],
267+
'non auto increment field' => [
268+
'field options' => ['unsigned' => true, 'nullable' => false,],
269+
'expected result' => false,
270+
]
271+
];
272+
}
210273
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Setup\Declaration\Schema\Db\MySQL;
7+
8+
use Magento\Framework\App\ResourceConnection;
9+
use Magento\Framework\DB\Ddl\Table;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
/**
13+
* Test DB schema writer
14+
*
15+
* @magentoDbIsolation disabled
16+
*/
17+
class DbSchemaWriterTest extends \PHPUnit\Framework\TestCase
18+
{
19+
/**
20+
* @var ResourceConnection
21+
*/
22+
private $resourceConnection;
23+
24+
/**
25+
* @var DbSchemaWriter
26+
*/
27+
private $dbSchemaWriter;
28+
29+
protected function setUp(): void
30+
{
31+
set_error_handler(null);
32+
$this->resourceConnection = Bootstrap::getObjectManager()->get(ResourceConnection::class);
33+
$this->dbSchemaWriter = Bootstrap::getObjectManager()->get(DbSchemaWriter::class);
34+
}
35+
36+
protected function tearDown(): void
37+
{
38+
restore_error_handler();
39+
}
40+
41+
/**
42+
* Retrieve database adapter instance
43+
*
44+
* @return \Magento\Framework\DB\Adapter\Pdo\Mysql
45+
*/
46+
private function getDbAdapter()
47+
{
48+
return $this->resourceConnection->getConnection();
49+
}
50+
51+
/**
52+
* Test reset auto increment
53+
*
54+
* @param array $options
55+
* @param string|bool $expected
56+
* @throws \Zend_Db_Exception
57+
* @dataProvider getAutoIncrementFieldDataProvider
58+
*/
59+
public function testResetAutoIncrement(array $options, $expected)
60+
{
61+
$adapter = $this->getDbAdapter();
62+
$tableName = 'table_auto_increment_field';
63+
64+
$table = $adapter
65+
->newTable($tableName)
66+
->addColumn(
67+
'row_id',
68+
Table::TYPE_INTEGER,
69+
null,
70+
$options,
71+
'Row Id'
72+
)
73+
->addColumn(
74+
'created_at',
75+
Table::TYPE_DATETIME,
76+
null,
77+
['default' => new \Zend_Db_Expr('CURRENT_TIMESTAMP')]
78+
)
79+
->addColumn(
80+
'integer_column',
81+
Table::TYPE_INTEGER,
82+
11,
83+
['default' => 123456]
84+
)->addColumn(
85+
'string_column',
86+
Table::TYPE_TEXT,
87+
255,
88+
['default' => 'default test text']
89+
)
90+
->setComment('Test table column with expression as column default value');
91+
$adapter->createTable($table);
92+
93+
$dbStatement = $this->dbSchemaWriter->resetAutoIncrement($tableName, 'default');
94+
$this->assertEquals($expected, $dbStatement->getStatement());
95+
96+
//clean up database from test table
97+
$adapter->dropTable($tableName);
98+
99+
}
100+
101+
public function getAutoIncrementFieldDataProvider()
102+
{
103+
return [
104+
'auto increment field' => [
105+
'field options' => ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
106+
'expected result' => 'AUTO_INCREMENT = 0',
107+
],
108+
'non auto increment field' => [
109+
'field options' => ['unsigned' => true, 'nullable' => false,],
110+
'expected result' => 'AUTO_INCREMENT = 1',
111+
]
112+
];
113+
}
114+
}

0 commit comments

Comments
 (0)