mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +08:00 
			
		
		
		
	Fix #20329: pgsql: Column Schema doesn't recognize PG type cast
This commit is contained in:
		@ -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
 | 
				
			||||||
 | 
				
			|||||||
@ -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(
 | 
					                        in_array(
 | 
				
			||||||
                            strtoupper($column->defaultValue),
 | 
					                            strtoupper($column->defaultValue),
 | 
				
			||||||
                            ['NOW()', 'CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME'],
 | 
					                            ['NOW()', 'CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME'],
 | 
				
			||||||
                            true
 | 
					                            true
 | 
				
			||||||
 | 
					                        ) ||
 | 
				
			||||||
 | 
					                        (false !== strpos($column->defaultValue, '('))
 | 
				
			||||||
                    )
 | 
					                    )
 | 
				
			||||||
                ) {
 | 
					                ) {
 | 
				
			||||||
                    $column->defaultValue = new Expression($column->defaultValue);
 | 
					                    $column->defaultValue = new Expression($column->defaultValue);
 | 
				
			||||||
 | 
				
			|||||||
@ -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();
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user