Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve #19047 #19211

Merged
merged 3 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/db/mysql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ protected function loadColumnSchema($info)
* See details here: https://mariadb.com/kb/en/library/now/#description
*/
if (($column->type === 'timestamp' || $column->type === 'datetime')
&& isset($info['default'])
&& preg_match('/^current_timestamp(?:\(([0-9]*)\))?$/i', $info['default'], $matches)) {
$column->defaultValue = new Expression('CURRENT_TIMESTAMP' . (!empty($matches[1]) ? '(' . $matches[1] . ')' : ''));
} elseif (isset($type) && $type === 'bit') {
Expand Down
31 changes: 29 additions & 2 deletions tests/framework/db/mysql/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace yiiunit\framework\db\mysql;
use yii\db\Expression;

use yii\db\mysql\ColumnSchema;
use yii\db\mysql\Schema;
use yiiunit\framework\db\AnyCaseValue;

/**
Expand Down Expand Up @@ -101,7 +103,7 @@ public function testAlternativeDisplayOfDefaultCurrentTimestampInMariaDB()
* We do not have a real database MariaDB >= 10.2.3 for tests, so we emulate the information that database
* returns in response to the query `SHOW FULL COLUMNS FROM ...`
*/
$schema = new \yii\db\mysql\Schema();
$schema = new Schema();
$column = $this->invokeMethod($schema, 'loadColumnSchema', [[
'field' => 'emulated_MariaDB_field',
'type' => 'timestamp',
Expand All @@ -114,11 +116,36 @@ public function testAlternativeDisplayOfDefaultCurrentTimestampInMariaDB()
'comment' => '',
]]);

$this->assertInstanceOf(\yii\db\mysql\ColumnSchema::className(), $column);
$this->assertInstanceOf(ColumnSchema::className(), $column);
$this->assertInstanceOf(Expression::className(), $column->defaultValue);
$this->assertEquals('CURRENT_TIMESTAMP', $column->defaultValue);
}

/**
* When displayed in the INFORMATION_SCHEMA.COLUMNS table, a default CURRENT TIMESTAMP is provided
* as NULL.
*
* @see https://github.com/yiisoft/yii2/issues/19047
*/
public function testAlternativeDisplayOfDefaultCurrentTimestampAsNullInMariaDB()
{
$schema = new Schema();
$column = $this->invokeMethod($schema, 'loadColumnSchema', [[
'field' => 'emulated_MariaDB_field',
'type' => 'timestamp',
'collation' => NULL,
'null' => 'NO',
'key' => '',
'default' => NULL,
'extra' => '',
'privileges' => 'select,insert,update,references',
'comment' => '',
]]);

$this->assertInstanceOf(ColumnSchema::className(), $column);
$this->assertEquals(NULL, $column->defaultValue);
}

public function getExpectedColumns()
{
$version = $this->getConnection()->getSchema()->getServerVersion();
Expand Down