Skip to content

Commit 1176ba4

Browse files
dvobkyleconroy
authored andcommitted
fix(sqlite): Fix ADD COLUMN without typename
Use type name 'any' for ALTER TABLE t1 ADD COLUMN c1 where no type name for c1 is provided. This is the same logic as for CREATE TABLE. Fixes #3375
1 parent 0b952b4 commit 1176ba4

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

internal/engine/sqlite/catalog_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,30 @@ func TestUpdate(t *testing.T) {
8282
},
8383
},
8484
},
85+
{
86+
`
87+
CREATE TABLE foo (bar text);
88+
ALTER TABLE foo ADD COLUMN baz;
89+
`,
90+
&catalog.Schema{
91+
Name: "main",
92+
Tables: []*catalog.Table{
93+
{
94+
Rel: &ast.TableName{Name: "foo"},
95+
Columns: []*catalog.Column{
96+
{
97+
Name: "bar",
98+
Type: ast.TypeName{Name: "text"},
99+
},
100+
{
101+
Name: "baz",
102+
Type: ast.TypeName{Name: "any"},
103+
},
104+
},
105+
},
106+
},
107+
},
108+
},
85109
{
86110
`
87111
CREATE TABLE foo (bar text);

internal/engine/sqlite/convert.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ func identifier(id string) string {
3535
return strings.ToLower(id)
3636
}
3737

38+
func getTypeName(t parser.IType_nameContext) string {
39+
if t == nil {
40+
return "any"
41+
}
42+
return t.GetText()
43+
}
44+
3845
func NewIdentifier(t string) *ast.String {
3946
return &ast.String{Str: identifier(t)}
4047
}
@@ -72,10 +79,8 @@ func (c *cc) convertAlter_table_stmtContext(n *parser.Alter_table_stmtContext) a
7279
Name: &name,
7380
Subtype: ast.AT_AddColumn,
7481
Def: &ast.ColumnDef{
75-
Colname: name,
76-
TypeName: &ast.TypeName{
77-
Name: def.Type_name().GetText(),
78-
},
82+
Colname: name,
83+
TypeName: &ast.TypeName{Name: getTypeName(def.Type_name())},
7984
IsNotNull: hasNotNullConstraint(def.AllColumn_constraint()),
8085
},
8186
})
@@ -113,14 +118,10 @@ func (c *cc) convertCreate_table_stmtContext(n *parser.Create_table_stmtContext)
113118
}
114119
for _, idef := range n.AllColumn_def() {
115120
if def, ok := idef.(*parser.Column_defContext); ok {
116-
typeName := "any"
117-
if def.Type_name() != nil {
118-
typeName = def.Type_name().GetText()
119-
}
120121
stmt.Cols = append(stmt.Cols, &ast.ColumnDef{
121122
Colname: identifier(def.Column_name().GetText()),
122123
IsNotNull: hasNotNullConstraint(def.AllColumn_constraint()),
123-
TypeName: &ast.TypeName{Name: typeName},
124+
TypeName: &ast.TypeName{Name: getTypeName(def.Type_name())},
124125
})
125126
}
126127
}

0 commit comments

Comments
 (0)