Skip to content

Commit bb3b943

Browse files
committed
Unify column name representation in stored schema, support all representations 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).
1 parent 914f675 commit bb3b943

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3109,8 +3109,21 @@ private function execute_alter() {
31093109
if ( ! $token ) {
31103110
break;
31113111
}
3112-
if ( WP_SQLite_Token::TYPE_STRING !== $token->type
3113-
|| $from_name !== $this->normalize_column_name( $token->value ) ) {
3112+
3113+
// Column name can be represented as:
3114+
// 1. "column_name" (string)
3115+
// 2. `column_name` (symbol)
3116+
// 3. column_name (keyword — not a reserved one)
3117+
// While CREATE TABLE normalizes column names to strings, it's better to handle all
3118+
// variants to handle table definitions with names stored in any valid form.
3119+
$is_column_name = WP_SQLite_Token::TYPE_STRING === $token->type
3120+
|| WP_SQLite_Token::TYPE_SYMBOL === $token->type
3121+
|| (
3122+
WP_SQLite_Token::TYPE_KEYWORD === $token->type
3123+
&& ! ( WP_SQLite_Token::FLAG_KEYWORD_RESERVED & $token->flags )
3124+
);
3125+
3126+
if ( ! $is_column_name || $from_name !== $this->normalize_column_name( $token->value ) ) {
31143127
continue;
31153128
}
31163129

@@ -3145,7 +3158,7 @@ private function execute_alter() {
31453158
// Otherwise, just add the new name in place of the old name we dropped.
31463159
$create_table->add(
31473160
new WP_SQLite_Token(
3148-
'`' . ( $new_field ? $new_field->name : $from_name ) . '`',
3161+
'"' . ( $new_field ? $new_field->name : $from_name ) . '"',
31493162
WP_SQLite_Token::TYPE_KEYWORD
31503163
)
31513164
);

0 commit comments

Comments
 (0)