Skip to content

Commit

Permalink
Fix creating a table with AUTOINCREMENT and ON UPDATE followed by…
Browse files Browse the repository at this point in the history
… `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.
  • Loading branch information
JanJakes authored Aug 20, 2024
1 parent f4212a3 commit 0181d13
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
21 changes: 21 additions & 0 deletions tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,27 @@ public function testColumnWithOnUpdateAndNoIdField() {
$this->assertRegExp( '/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/', $result[0]->created_at );
}

public function testColumnWithOnUpdateAndAutoincrementPrimaryKey() {
// CREATE TABLE with ON UPDATE, AUTO_INCREMENT, and PRIMARY KEY
$this->assertQuery(
'CREATE TABLE _tmp_table (
id int(11) NOT NULL AUTO_INCREMENT,
created_at timestamp NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);'
);

// on INSERT, no timestamps are expected
$this->assertQuery( 'INSERT INTO _tmp_table (id) VALUES (1)' );
$result = $this->assertQuery( 'SELECT * FROM _tmp_table WHERE id = 1' );
$this->assertNull( $result[0]->created_at );

// on UPDATE, we expect timestamps in form YYYY-MM-DD HH:MM:SS
$this->assertQuery( 'UPDATE _tmp_table SET id = 2 WHERE id = 1' );
$result = $this->assertQuery( 'SELECT * FROM _tmp_table WHERE id = 2' );
$this->assertRegExp( '/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/', $result[0]->created_at );
}

public function testChangeColumnWithOnUpdate() {
// CREATE TABLE with ON UPDATE
$this->assertQuery(
Expand Down
3 changes: 1 addition & 2 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1145,8 +1145,7 @@ private function parse_mysql_create_table_field() {
array( 'CURRENT_TIMESTAMP' )
)
) {
$this->rewriter->skip(); // ON UPDATE
$this->rewriter->skip(); // CURRENT_TIMESTAMP
$this->rewriter->skip();
$result->on_update = true;
continue;
}
Expand Down

0 comments on commit 0181d13

Please sign in to comment.