Fix #20329: pgsql: Column Schema doesn't recognize PG type cast

This commit is contained in:
Vladimir
2025-02-28 00:33:35 +10:00
committed by GitHub
parent dbb82a25a2
commit e38f62e55b
3 changed files with 68 additions and 4 deletions

View File

@ -5,6 +5,7 @@ Yii Framework 2 Change Log
------------------------ ------------------------
- Enh #20309: Add custom attributes support to style tags (nzwz) - Enh #20309: Add custom attributes support to style tags (nzwz)
- Bug #20329: pgsql: Column Schema doesn't recognize PG type cast (arkhamvm)
2.0.52 February 13, 2025 2.0.52 February 13, 2025

View File

@ -552,10 +552,13 @@ SQL;
} elseif ($column->defaultValue) { } elseif ($column->defaultValue) {
if ( if (
in_array($column->type, [self::TYPE_TIMESTAMP, self::TYPE_DATE, self::TYPE_TIME], true) && in_array($column->type, [self::TYPE_TIMESTAMP, self::TYPE_DATE, self::TYPE_TIME], true) &&
in_array( (
strtoupper($column->defaultValue), in_array(
['NOW()', 'CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME'], strtoupper($column->defaultValue),
true ['NOW()', 'CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME'],
true
) ||
(false !== strpos($column->defaultValue, '('))
) )
) { ) {
$column->defaultValue = new Expression($column->defaultValue); $column->defaultValue = new Expression($column->defaultValue);

View File

@ -341,6 +341,66 @@ class SchemaTest extends \yiiunit\framework\db\SchemaTest
$this->assertNull($tableSchema->getColumn('timestamp')->defaultValue); $this->assertNull($tableSchema->getColumn('timestamp')->defaultValue);
} }
/**
* @see https://github.com/yiisoft/yii2/issues/20329
*/
public function testTimestampUtcNowDefaultValue()
{
$db = $this->getConnection(false);
if ($db->schema->getTableSchema('test_timestamp_utc_now_default') !== null) {
$db->createCommand()->dropTable('test_timestamp_utc_now_default')->execute();
}
$db->createCommand()->createTable('test_timestamp_utc_now_default', [
'id' => 'pk',
'timestamp' => 'timestamp DEFAULT timezone(\'UTC\'::text, now()) NOT NULL',
])->execute();
$db->schema->refreshTableSchema('test_timestamp_utc_now_default');
$tableSchema = $db->schema->getTableSchema('test_timestamp_utc_now_default');
$this->assertEquals(new Expression('timezone(\'UTC\'::text, now())'), $tableSchema->getColumn('timestamp')->defaultValue);
}
/**
* @see https://github.com/yiisoft/yii2/issues/20329
*/
public function testTimestampNowDefaultValue()
{
$db = $this->getConnection(false);
if ($db->schema->getTableSchema('test_timestamp_now_default') !== null) {
$db->createCommand()->dropTable('test_timestamp_now_default')->execute();
}
$db->createCommand()->createTable('test_timestamp_now_default', [
'id' => 'pk',
'timestamp' => 'timestamp DEFAULT now()',
])->execute();
$db->schema->refreshTableSchema('test_timestamp_now_default');
$tableSchema = $db->schema->getTableSchema('test_timestamp_now_default');
$this->assertEquals(new Expression('now()'), $tableSchema->getColumn('timestamp')->defaultValue);
}
/**
* @see https://github.com/yiisoft/yii2/issues/20329
*/
public function testTimestampUtcStringDefaultValue()
{
$db = $this->getConnection(false);
if ($db->schema->getTableSchema('test_timestamp_utc_string_default') !== null) {
$db->createCommand()->dropTable('test_timestamp_utc_string_default')->execute();
}
$db->createCommand()->createTable('test_timestamp_utc_string_default', [
'id' => 'pk',
'timestamp' => 'timestamp DEFAULT timezone(\'UTC\'::text, \'1970-01-01 00:00:00+00\'::timestamp with time zone) NOT NULL',
])->execute();
$db->schema->refreshTableSchema('test_timestamp_utc_string_default');
$tableSchema = $db->schema->getTableSchema('test_timestamp_utc_string_default');
$this->assertEquals(new Expression('timezone(\'UTC\'::text, \'1970-01-01 00:00:00+00\'::timestamp with time zone)'), $tableSchema->getColumn('timestamp')->defaultValue);
}
public function constraintsProvider() public function constraintsProvider()
{ {
$result = parent::constraintsProvider(); $result = parent::constraintsProvider();