Skip to content

Commit 0334707

Browse files
author
User
committed
fix(sqlite): normalize column names in ALTER TABLE ADD/DROP COLUMN
Fixes #4307 When ALTER TABLE ADD COLUMN or DROP COLUMN statements use mixed-case column names (e.g., barBaz), the column name was stored in the catalog with its original case. However, SELECT queries normalize all unquoted identifiers to lowercase via the identifier() function, causing a mismatch and 'column does not exist' errors. This fix ensures ALTER TABLE ADD/DROP COLUMN also normalize column names using identifier(), matching the behavior of CREATE TABLE and SELECT queries. This aligns with SQLite's case-insensitive identifier handling. Changes: - internal/engine/sqlite/convert.go: Apply identifier() to column names in ALTER TABLE ADD COLUMN and DROP COLUMN handlers - internal/engine/sqlite/catalog_test.go: Add regression test for mixed-case column names in ALTER TABLE ADD COLUMN
1 parent 744558d commit 0334707

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

internal/engine/sqlite/catalog_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,38 @@ func TestUpdate(t *testing.T) {
8282
},
8383
},
8484
},
85+
{
86+
// Regression test for #4307: mixed-case column name in ALTER TABLE ADD COLUMN
87+
`
88+
CREATE TABLE foo (id INTEGER NOT NULL, xYz INTEGER NOT NULL DEFAULT 0);
89+
ALTER TABLE foo ADD COLUMN barBaz TEXT NOT NULL DEFAULT '';
90+
`,
91+
&catalog.Schema{
92+
Name: "main",
93+
Tables: []*catalog.Table{
94+
{
95+
Rel: &ast.TableName{Name: "foo"},
96+
Columns: []*catalog.Column{
97+
{
98+
Name: "id",
99+
Type: ast.TypeName{Name: "INTEGER"},
100+
IsNotNull: true,
101+
},
102+
{
103+
Name: "xyz",
104+
Type: ast.TypeName{Name: "INTEGER"},
105+
IsNotNull: true,
106+
},
107+
{
108+
Name: "barbaz",
109+
Type: ast.TypeName{Name: "TEXT"},
110+
IsNotNull: true,
111+
},
112+
},
113+
},
114+
},
115+
},
116+
},
85117
{
86118
`
87119
CREATE TABLE foo (bar text);

internal/engine/sqlite/convert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (c *cc) convertAlter_table_stmtContext(n *parser.Alter_table_stmtContext) a
6767
Table: parseTableName(n),
6868
Cmds: &ast.List{},
6969
}
70-
name := def.Column_name().GetText()
70+
name := identifier(def.Column_name().GetText())
7171
stmt.Cmds.Items = append(stmt.Cmds.Items, &ast.AlterTableCmd{
7272
Name: &name,
7373
Subtype: ast.AT_AddColumn,
@@ -88,7 +88,7 @@ func (c *cc) convertAlter_table_stmtContext(n *parser.Alter_table_stmtContext) a
8888
Table: parseTableName(n),
8989
Cmds: &ast.List{},
9090
}
91-
name := n.Column_name(0).GetText()
91+
name := identifier(n.Column_name(0).GetText())
9292
stmt.Cmds.Items = append(stmt.Cmds.Items, &ast.AlterTableCmd{
9393
Name: &name,
9494
Subtype: ast.AT_DropColumn,

0 commit comments

Comments
 (0)