Skip to content

Commit

Permalink
Add handling for adding/modifying columns with FIRST/AFTER
Browse files Browse the repository at this point in the history
  • Loading branch information
JanJakes committed Aug 7, 2024
1 parent 2561000 commit 2d21a4a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
29 changes: 29 additions & 0 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2939,6 +2939,35 @@ private function execute_alter() {
WP_SQLite_Token::FLAG_KEYWORD_DATA_TYPE
)
);

// Drop "FIRST" and "AFTER <another-column>" as these are not supported in SQLite.
$after = $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 ( $after && ( ! $comma || $after->position < $comma->position ) ) {
$this->rewriter->consume(
array(
'type' => WP_SQLite_Token::TYPE_KEYWORD,
'value' => 'AFTER',
)
);
$this->rewriter->drop_last();
if ( 'AFTER' === strtoupper( $after->value ) ) {
$this->rewriter->skip();
}
}

$this->update_data_type_cache(
$this->table_name,
$column_name,
Expand Down

0 comments on commit 2d21a4a

Please sign in to comment.