Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit 124805e

Browse files
committed
Fix values of "COLUMN_EXTRA" with "DEFAULT CURRENT TIMESTAMP"
1 parent 1612769 commit 124805e

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

tests/WP_SQLite_Driver_Tests.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,6 +3787,53 @@ public function testColumnDefaults(): void {
37873787
"
37883788
);
37893789

3790+
$result = $this->assertQuery( 'DESCRIBE t' );
3791+
$this->assertEquals(
3792+
array(
3793+
(object) array(
3794+
'Field' => 'name',
3795+
'Type' => 'varchar(255)',
3796+
'Null' => 'YES',
3797+
'Key' => '',
3798+
'Default' => 'CURRENT_TIMESTAMP',
3799+
'Extra' => '',
3800+
),
3801+
(object) array(
3802+
'Field' => 'type',
3803+
'Type' => 'varchar(255)',
3804+
'Null' => 'NO',
3805+
'Key' => '',
3806+
'Default' => 'DEFAULT',
3807+
'Extra' => '',
3808+
),
3809+
(object) array(
3810+
'Field' => 'description',
3811+
'Type' => 'varchar(250)',
3812+
'Null' => 'NO',
3813+
'Key' => '',
3814+
'Default' => '',
3815+
'Extra' => '',
3816+
),
3817+
(object) array(
3818+
'Field' => 'created_at',
3819+
'Type' => 'timestamp',
3820+
'Null' => 'YES',
3821+
'Key' => '',
3822+
'Default' => 'CURRENT_TIMESTAMP',
3823+
'Extra' => 'DEFAULT_GENERATED',
3824+
),
3825+
(object) array(
3826+
'Field' => 'updated_at',
3827+
'Type' => 'timestamp',
3828+
'Null' => 'NO',
3829+
'Key' => '',
3830+
'Default' => 'CURRENT_TIMESTAMP',
3831+
'Extra' => 'DEFAULT_GENERATED on update CURRENT_TIMESTAMP',
3832+
),
3833+
),
3834+
$result
3835+
);
3836+
37903837
$result = $this->assertQuery( 'SHOW CREATE TABLE t' );
37913838
$this->assertEquals(
37923839
"CREATE TABLE `t` (\n"

wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,20 +1440,28 @@ private function get_column_extra( WP_Parser_Node $node ): string {
14401440
return 'auto_increment';
14411441
}
14421442

1443-
// Check whether DEFAULT value is an expression.
1443+
// AUTO_INCREMENT columns can't have a DEFAULT value.
1444+
foreach ( $attributes as $attr ) {
1445+
if ( $attr->has_child_token( WP_MySQL_Lexer::AUTO_INCREMENT_SYMBOL ) ) {
1446+
return 'auto_increment';
1447+
}
1448+
}
1449+
1450+
// Check whether DEFAULT value is generated.
14441451
foreach ( $attributes as $attr ) {
14451452
if (
14461453
$attr->has_child_token( WP_MySQL_Lexer::DEFAULT_SYMBOL )
1447-
&& $attr->has_child_node( 'exprWithParentheses' )
1454+
&& (
1455+
$attr->has_child_node( 'exprWithParentheses' )
1456+
|| $attr->has_child_token( WP_MySQL_Lexer::NOW_SYMBOL )
1457+
)
14481458
) {
14491459
$extras[] = 'DEFAULT_GENERATED';
14501460
}
14511461
}
14521462

1463+
// Check for ON UPDATE CURRENT_TIMESTAMP.
14531464
foreach ( $attributes as $attr ) {
1454-
if ( $attr->has_child_token( WP_MySQL_Lexer::AUTO_INCREMENT_SYMBOL ) ) {
1455-
return 'auto_increment';
1456-
}
14571465
if (
14581466
$attr->has_child_token( WP_MySQL_Lexer::ON_SYMBOL )
14591467
&& $attr->has_child_token( WP_MySQL_Lexer::UPDATE_SYMBOL )
@@ -1462,6 +1470,7 @@ private function get_column_extra( WP_Parser_Node $node ): string {
14621470
}
14631471
}
14641472

1473+
// Check for generated columns.
14651474
if ( $node->get_first_descendant_token( WP_MySQL_Lexer::VIRTUAL_SYMBOL ) ) {
14661475
$extras[] = 'VIRTUAL GENERATED';
14671476
} elseif ( $node->get_first_descendant_token( WP_MySQL_Lexer::STORED_SYMBOL ) ) {

0 commit comments

Comments
 (0)