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

Commit a6d5c38

Browse files
committed
Support type names as KEY names
TODO: - find a better way of checking for the key condition
1 parent 271163b commit a6d5c38

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

tests/WP_SQLite_Translator_Tests.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,51 @@ public function testColumnWithOnUpdate() {
12521252
$this->assertNull( $result[0]->updated_at );
12531253
}
12541254

1255+
public function testTimestampColumnNamedTimestamp() {
1256+
// CREATE TABLE with ON UPDATE
1257+
$this->assertQuery(
1258+
"CREATE TABLE `_tmp_table` (
1259+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
1260+
`timestamp` datetime NOT NULL,
1261+
`numeric` int(11) NOT NULL,
1262+
PRIMARY KEY (`id`),
1263+
KEY timestamp (timestamp),
1264+
KEY numeric (numeric)
1265+
);"
1266+
);
1267+
$results = $this->assertQuery( 'DESCRIBE _tmp_table;' );
1268+
var_dump( $results );
1269+
$this->assertEquals(
1270+
array(
1271+
(object) array(
1272+
'Field' => 'id',
1273+
'Type' => 'bigint(20) unsigned',
1274+
'Null' => 'NO',
1275+
'Key' => 'PRI',
1276+
'Default' => '0',
1277+
'Extra' => '',
1278+
),
1279+
(object) array(
1280+
'Field' => 'timestamp',
1281+
'Type' => 'datetime',
1282+
'Null' => 'NO',
1283+
'Key' => '',
1284+
'Default' => null,
1285+
'Extra' => '',
1286+
),
1287+
(object) array(
1288+
'Field' => 'numeric',
1289+
'Type' => 'int(11)',
1290+
'Null' => 'NO',
1291+
'Key' => '',
1292+
'Default' => '0',
1293+
'Extra' => '',
1294+
),
1295+
),
1296+
$results
1297+
);
1298+
}
1299+
12551300
public function testColumnWithOnUpdateAndNoIdField() {
12561301
// CREATE TABLE with ON UPDATE
12571302
$this->assertQuery(

wp-includes/sqlite/class-wp-sqlite-translator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,13 +1017,20 @@ private function parse_create_table() {
10171017
* token is a data type.
10181018
*/
10191019
$second_token = $this->rewriter->peek_nth( 2 );
1020+
$current_token = $this->rewriter->peek();
10201021

10211022
if ( $second_token->matches(
10221023
WP_SQLite_Token::TYPE_KEYWORD,
10231024
WP_SQLite_Token::FLAG_KEYWORD_DATA_TYPE
1025+
) && !$current_token->matches(
1026+
WP_SQLite_Token::TYPE_KEYWORD,
1027+
WP_SQLite_Token::FLAG_KEYWORD_RESERVED,
1028+
array( 'KEY' )
10241029
) ) {
1030+
echo 'field';
10251031
$result->fields[] = $this->parse_mysql_create_table_field();
10261032
} else {
1033+
echo 'constraint';
10271034
$result->constraints[] = $this->parse_mysql_create_table_constraint();
10281035
}
10291036

@@ -1236,6 +1243,7 @@ private function parse_mysql_create_table_constraint() {
12361243
}
12371244

12381245
$result->value = $this->normalize_mysql_index_type( $constraint->value );
1246+
echo 'result value: ' . $result->value . "\n";
12391247
if ( $result->value ) {
12401248
$this->rewriter->skip(); // Constraint type.
12411249

0 commit comments

Comments
 (0)