Skip to content

Commit

Permalink
Unify column name representation in stored schema, support all repres…
Browse files Browse the repository at this point in the history
…entations when parsing

At the moment, CREATE TABLE statements normalize column names to use double quotes
and they are stored as such in sqlite_master and returned by get_sqlite_create_table().

However, the else branch of CHANGE COLUMN statements was using backticks instead,
which caused incorrect parsing for the next ALTER COLUMN statements now, when some
CHANGE statements without column definition are supported (SET/DROP DEFAULT).

This commit fixes both — 1) the name representation to use double quotes instead of backticks,
and 2) the parsing to support all column name representation (double quotes, backticks, nothing).
  • Loading branch information
JanJakes committed Aug 12, 2024
1 parent 914f675 commit bb3b943
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3109,8 +3109,21 @@ private function execute_alter() {
if ( ! $token ) {
break;
}
if ( WP_SQLite_Token::TYPE_STRING !== $token->type
|| $from_name !== $this->normalize_column_name( $token->value ) ) {

// Column name can be represented as:
// 1. "column_name" (string)
// 2. `column_name` (symbol)
// 3. column_name (keyword — not a reserved one)
// While CREATE TABLE normalizes column names to strings, it's better to handle all
// variants to handle table definitions with names stored in any valid form.
$is_column_name = WP_SQLite_Token::TYPE_STRING === $token->type
|| WP_SQLite_Token::TYPE_SYMBOL === $token->type
|| (
WP_SQLite_Token::TYPE_KEYWORD === $token->type
&& ! ( WP_SQLite_Token::FLAG_KEYWORD_RESERVED & $token->flags )
);

if ( ! $is_column_name || $from_name !== $this->normalize_column_name( $token->value ) ) {
continue;
}

Expand Down Expand Up @@ -3145,7 +3158,7 @@ private function execute_alter() {
// Otherwise, just add the new name in place of the old name we dropped.
$create_table->add(
new WP_SQLite_Token(
'`' . ( $new_field ? $new_field->name : $from_name ) . '`',
'"' . ( $new_field ? $new_field->name : $from_name ) . '"',
WP_SQLite_Token::TYPE_KEYWORD
)
);
Expand Down

0 comments on commit bb3b943

Please sign in to comment.