Skip to content

Commit

Permalink
MAGETWO-67404: Fix to allow Zend_Db_Expr as column default #9131
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Korshenko committed Apr 12, 2017
2 parents 2e5e5e5 + c985f72 commit ba5b323
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\Framework\App\ResourceConnection;
use Magento\TestFramework\Helper\CacheCleaner;
use Magento\Framework\DB\Ddl\Table;

class MysqlTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -157,4 +158,70 @@ public function testDescribeTable()
$this->getDbAdapter()->describeTable($tableName)
);
}

/**
* Test that Zend_Db_Expr can be used as a column default value.
* @see https://github.com/magento/magento2/pull/9131
*/
public function testCreateTableColumnWithExpressionAsColumnDefaultValue()
{
$adapter = $this->getDbAdapter();
$tableName = 'table_column_with_expression_as_column_default_value';

$table = $adapter
->newTable($tableName)
->addColumn(
'row_id',
Table::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Row Id'
)
->addColumn(
'created_at',
Table::TYPE_DATETIME,
null,
['default' => new \Zend_Db_Expr('CURRENT_TIMESTAMP')]
)
->addColumn(
'integer_column',
Table::TYPE_INTEGER,
11,
['default' => 123456]
)->addColumn(
'string_column',
Table::TYPE_TEXT,
255,
['default' => 'default test text']
)
->setComment('Test table column with expression as column default value');
$adapter->createTable($table);

$tableDescription = $adapter->describeTable($tableName);

//clean up database from test table
$adapter->dropTable($tableName);

$this->assertArrayHasKey('created_at', $tableDescription, 'Column created_at does not exists');
$this->assertArrayHasKey('integer_column', $tableDescription, 'Column integer_column does not exists');
$this->assertArrayHasKey('string_column', $tableDescription, 'Column string_column does not exists');
$dateColumn = $tableDescription['created_at'];
$intColumn = $tableDescription['integer_column'];
$stringColumn = $tableDescription['string_column'];

//Test default value with expression
$this->assertEquals('created_at', $dateColumn['COLUMN_NAME'], 'Incorrect column name');
$this->assertEquals(Table::TYPE_DATETIME, $dateColumn['DATA_TYPE'], 'Incorrect column type');
$this->assertEquals('CURRENT_TIMESTAMP', $dateColumn['DEFAULT'], 'Incorrect column default expression value');

//Test default value with integer value
$this->assertEquals('integer_column', $intColumn['COLUMN_NAME'], 'Incorrect column name');
$this->assertEquals('int', $intColumn['DATA_TYPE'], 'Incorrect column type');
$this->assertEquals(123456, $intColumn['DEFAULT'], 'Incorrect column default integer value');

//Test default value with string value
$this->assertEquals('string_column', $stringColumn['COLUMN_NAME'], 'Incorrect column name');
$this->assertEquals('varchar', $stringColumn['DATA_TYPE'], 'Incorrect column type');
$this->assertEquals('default test text', $stringColumn['DEFAULT'], 'Incorrect column default string value');
}
}
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -2422,7 +2422,7 @@ protected function _getColumnDefinition($options, $ddlType = null)
* where default value can be quoted already.
* We need to avoid "double-quoting" here
*/
if ($cDefault !== null && strlen($cDefault)) {
if ($cDefault !== null && is_string($cDefault) && strlen($cDefault)) {
$cDefault = str_replace("'", '', $cDefault);
}

Expand Down

0 comments on commit ba5b323

Please sign in to comment.