Skip to content

Commit 0181d13

Browse files
authored
Fix creating a table with AUTOINCREMENT and ON UPDATE followed by PRIMARY KEY (#158)
This reverts e59eff8. The fix was wrong. When spotting it, I misunderstood that the first token was already consumed. Without the fix, the new test fails with: ``` Cannot combine AUTOINCREMENT and multiple primary keys in SQLite. ``` This is because we've consumed one extra token (`,`), and the `PRIMARY KEY` then gets assigned to the `created_at` column as well.
1 parent f4212a3 commit 0181d13

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

tests/WP_SQLite_Translator_Tests.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,27 @@ public function testColumnWithOnUpdateAndNoIdField() {
11961196
$this->assertRegExp( '/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/', $result[0]->created_at );
11971197
}
11981198

1199+
public function testColumnWithOnUpdateAndAutoincrementPrimaryKey() {
1200+
// CREATE TABLE with ON UPDATE, AUTO_INCREMENT, and PRIMARY KEY
1201+
$this->assertQuery(
1202+
'CREATE TABLE _tmp_table (
1203+
id int(11) NOT NULL AUTO_INCREMENT,
1204+
created_at timestamp NULL ON UPDATE CURRENT_TIMESTAMP,
1205+
PRIMARY KEY (id)
1206+
);'
1207+
);
1208+
1209+
// on INSERT, no timestamps are expected
1210+
$this->assertQuery( 'INSERT INTO _tmp_table (id) VALUES (1)' );
1211+
$result = $this->assertQuery( 'SELECT * FROM _tmp_table WHERE id = 1' );
1212+
$this->assertNull( $result[0]->created_at );
1213+
1214+
// on UPDATE, we expect timestamps in form YYYY-MM-DD HH:MM:SS
1215+
$this->assertQuery( 'UPDATE _tmp_table SET id = 2 WHERE id = 1' );
1216+
$result = $this->assertQuery( 'SELECT * FROM _tmp_table WHERE id = 2' );
1217+
$this->assertRegExp( '/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/', $result[0]->created_at );
1218+
}
1219+
11991220
public function testChangeColumnWithOnUpdate() {
12001221
// CREATE TABLE with ON UPDATE
12011222
$this->assertQuery(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,8 +1145,7 @@ private function parse_mysql_create_table_field() {
11451145
array( 'CURRENT_TIMESTAMP' )
11461146
)
11471147
) {
1148-
$this->rewriter->skip(); // ON UPDATE
1149-
$this->rewriter->skip(); // CURRENT_TIMESTAMP
1148+
$this->rewriter->skip();
11501149
$result->on_update = true;
11511150
continue;
11521151
}

0 commit comments

Comments
 (0)