Skip to content

Commit 8766a52

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 cc372b2 commit 8766a52

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -3040,8 +3040,21 @@ private function execute_alter() {
30403040
if ( ! $token ) {
30413041
break;
30423042
}
3043-
if ( WP_SQLite_Token::TYPE_STRING !== $token->type
3044-
|| $from_name !== $this->normalize_column_name( $token->value ) ) {
3043+
3044+
// Column name can be represented as:
3045+
// 1. "column_name" (string)
3046+
// 2. `column_name` (symbol)
3047+
// 3. column_name (keyword — not a reserved one)
3048+
// While CREATE TABLE normalizes column names to strings, it's better to handle all
3049+
// variants to handle table definitions with names stored in any valid form.
3050+
$is_column_name = WP_SQLite_Token::TYPE_STRING === $token->type
3051+
|| WP_SQLite_Token::TYPE_SYMBOL === $token->type
3052+
|| (
3053+
WP_SQLite_Token::TYPE_KEYWORD === $token->type
3054+
&& ! ( WP_SQLite_Token::FLAG_KEYWORD_RESERVED & $token->flags )
3055+
);
3056+
3057+
if ( ! $is_column_name || $from_name !== $this->normalize_column_name( $token->value ) ) {
30453058
continue;
30463059
}
30473060

@@ -3076,7 +3089,7 @@ private function execute_alter() {
30763089
// Otherwise, just add the new name in place of the old name we dropped.
30773090
$create_table->add(
30783091
new WP_SQLite_Token(
3079-
'`' . ( $new_field ? $new_field->name : $from_name ) . '`',
3092+
'"' . ( $new_field ? $new_field->name : $from_name ) . '"',
30803093
WP_SQLite_Token::TYPE_KEYWORD
30813094
)
30823095
);

0 commit comments

Comments
 (0)