diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index e13c6573f6..024f1e4c10 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.16 under development ------------------------ +- Bug #15167: Fixed loading of default value `current_timestamp()` for MariaDB >= 10.2.3 (rugabarbo, bloodrain777, Skinka) - Bug #16253: Fixed empty checkboxlist validation (GHopperMSK) - Bug #15286: Fixed incorrect formatting of time with timezone information (rugabarbo) - Bug #17021: Fix to do not remove existing message category files in a subfolder (albertborsos) diff --git a/framework/db/mysql/Schema.php b/framework/db/mysql/Schema.php index 27a9736afa..95f9a47be5 100644 --- a/framework/db/mysql/Schema.php +++ b/framework/db/mysql/Schema.php @@ -287,7 +287,14 @@ SQL; $column->phpType = $this->getColumnPhpType($column); if (!$column->isPrimaryKey) { - if (($column->type === 'timestamp' || $column->type ==='datetime') && $info['default'] === 'CURRENT_TIMESTAMP') { + /** + * When displayed in the INFORMATION_SCHEMA.COLUMNS table, a default CURRENT TIMESTAMP is displayed + * as CURRENT_TIMESTAMP up until MariaDB 10.2.2, and as current_timestamp() from MariaDB 10.2.3. + * + * See details here: https://mariadb.com/kb/en/library/now/#description + */ + if (($column->type === 'timestamp' || $column->type === 'datetime') && + ($info['default'] === 'CURRENT_TIMESTAMP' || $info['default'] === 'current_timestamp()')) { $column->defaultValue = new Expression('CURRENT_TIMESTAMP'); } elseif (isset($type) && $type === 'bit') { $column->defaultValue = bindec(trim($info['default'], 'b\'')); diff --git a/tests/framework/db/mysql/SchemaTest.php b/tests/framework/db/mysql/SchemaTest.php index 8f85a5dd44..c182bb297e 100644 --- a/tests/framework/db/mysql/SchemaTest.php +++ b/tests/framework/db/mysql/SchemaTest.php @@ -61,4 +61,35 @@ SQL; $result['4: check'][2] = false; return $result; } + + /** + * When displayed in the INFORMATION_SCHEMA.COLUMNS table, a default CURRENT TIMESTAMP is displayed + * as CURRENT_TIMESTAMP up until MariaDB 10.2.2, and as current_timestamp() from MariaDB 10.2.3. + * + * @see https://mariadb.com/kb/en/library/now/#description + * @see https://github.com/yiisoft/yii2/issues/15167 + */ + 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(); + $column = $this->invokeMethod($schema, 'loadColumnSchema', [[ + 'field' => 'emulated_MariaDB_field', + 'type' => 'timestamp', + 'collation' => NULL, + 'null' => 'NO', + 'key' => '', + 'default' => 'current_timestamp()', + 'extra' => '', + 'privileges' => 'select,insert,update,references', + 'comment' => '', + ]]); + + $this->assertInstanceOf(\yii\db\mysql\ColumnSchema::className(), $column); + $this->assertInstanceOf(Expression::className(), $column->defaultValue); + $this->assertEquals('CURRENT_TIMESTAMP', $column->defaultValue); + } }