From 0cbf3917f072961705083a6f5aa0cfd25b4bd965 Mon Sep 17 00:00:00 2001 From: Jan Jakes Date: Wed, 7 Aug 2024 12:03:00 +0200 Subject: [PATCH] Add handling for adding/modifying columns with FIRST/AFTER --- tests/WP_SQLite_Translator_Tests.php | 25 ++++++++++++++++ .../sqlite/class-wp-sqlite-translator.php | 29 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/tests/WP_SQLite_Translator_Tests.php b/tests/WP_SQLite_Translator_Tests.php index d0e5c193..f514068c 100644 --- a/tests/WP_SQLite_Translator_Tests.php +++ b/tests/WP_SQLite_Translator_Tests.php @@ -1027,6 +1027,31 @@ public function testAlterTableAddNotNullVarcharColumn() { ); } + public function testAlterTableWithColumnFirstAndAfter() { + $this->assertQuery( + "CREATE TABLE _tmp_table ( + id int(11) NOT NULL, + name varchar(20) NOT NULL default '' + );" + ); + + $this->assertQuery( + "ALTER TABLE _tmp_table ADD COLUMN new_first_column VARCHAR(255) NOT NULL DEFAULT '' FIRST" + ); + + $this->assertQuery( + "ALTER TABLE _tmp_table ADD COLUMN new_column VARCHAR(255) NOT NULL DEFAULT '' AFTER id" + ); + + $this->assertQuery( + "ALTER TABLE _tmp_table CHANGE id id int(11) NOT NULL DEFAULT '' FIRST" + ); + + $this->assertQuery( + "ALTER TABLE _tmp_table CHANGE id id int(11) NOT NULL DEFAULT '' AFTER name" + ); + } + public function testAlterTableAddIndex() { $result = $this->assertQuery( "CREATE TABLE _tmp_table ( diff --git a/wp-includes/sqlite/class-wp-sqlite-translator.php b/wp-includes/sqlite/class-wp-sqlite-translator.php index 431c9c0e..015a38c7 100644 --- a/wp-includes/sqlite/class-wp-sqlite-translator.php +++ b/wp-includes/sqlite/class-wp-sqlite-translator.php @@ -2939,6 +2939,35 @@ private function execute_alter() { WP_SQLite_Token::FLAG_KEYWORD_DATA_TYPE ) ); + + // Drop "FIRST" and "AFTER ", as these are not supported in SQLite. + $column_position = $this->rewriter->peek( + array( + 'type' => WP_SQLite_Token::TYPE_KEYWORD, + 'value' => array( 'FIRST', 'AFTER' ), + ) + ); + + $comma = $this->rewriter->peek( + array( + 'type' => WP_SQLite_Token::TYPE_OPERATOR, + 'value' => ',', + ) + ); + + if ( $column_position && ( ! $comma || $column_position->position < $comma->position ) ) { + $this->rewriter->consume( + array( + 'type' => WP_SQLite_Token::TYPE_KEYWORD, + 'value' => 'AFTER', + ) + ); + $this->rewriter->drop_last(); + if ( 'AFTER' === strtoupper( $column_position->value ) ) { + $this->rewriter->skip(); + } + } + $this->update_data_type_cache( $this->table_name, $column_name,