Skip to content

Commit 9f770c1

Browse files
committed
fix(sqlite): allow to use overrides for columns which were created using "alter table .. add column"
This fixes the failing test which was added in the previous commit.
1 parent 1851c0d commit 9f770c1

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

internal/compiler/analyze.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,36 @@ func convertColumn(c *analyzer.Column) *Column {
6565
}
6666
}
6767

68+
func mergeColumnOrigin(dst, src *Column) {
69+
if dst == nil || src == nil {
70+
return
71+
}
72+
73+
// Column overrides in the Go generator depend on the column's original
74+
// table identity. For SQLite, the database analyzer is often the most
75+
// accurate source for this information, especially for columns added by
76+
// ALTER TABLE ... ADD COLUMN.
77+
//
78+
// Keep the catalog-inferred name/type/nullability unless the existing
79+
// combine logic below decides to override the type. Only merge origin
80+
// metadata here.
81+
if src.OriginalName != "" {
82+
dst.OriginalName = src.OriginalName
83+
}
84+
if src.Table != nil {
85+
dst.Table = src.Table
86+
}
87+
if src.TableAlias != "" {
88+
dst.TableAlias = src.TableAlias
89+
}
90+
if src.Scope != "" {
91+
dst.Scope = src.Scope
92+
}
93+
if src.EmbedTable != nil {
94+
dst.EmbedTable = src.EmbedTable
95+
}
96+
}
97+
6898
func combineAnalysis(prev *analysis, a *analyzer.Analysis) *analysis {
6999
var cols []*Column
70100
for _, c := range a.Columns {
@@ -79,6 +109,7 @@ func combineAnalysis(prev *analysis, a *analyzer.Analysis) *analysis {
79109
}
80110
if len(prev.Columns) == len(cols) {
81111
for i := range prev.Columns {
112+
mergeColumnOrigin(prev.Columns[i], cols[i])
82113
// Only override column types if the analyzer provides a specific type
83114
// (not "any"), since the catalog-based inference may have better info
84115
if cols[i].DataType != "any" {

internal/engine/sqlite/convert.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,18 @@ 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())
71+
typeName := "any"
72+
if def.Type_name() != nil {
73+
typeName = def.Type_name().GetText()
74+
}
7175
stmt.Cmds.Items = append(stmt.Cmds.Items, &ast.AlterTableCmd{
7276
Name: &name,
7377
Subtype: ast.AT_AddColumn,
7478
Def: &ast.ColumnDef{
7579
Colname: name,
7680
TypeName: &ast.TypeName{
77-
Name: def.Type_name().GetText(),
81+
Name: typeName,
7882
},
7983
IsNotNull: hasNotNullConstraint(def.AllColumn_constraint()),
8084
},
@@ -88,7 +92,7 @@ func (c *cc) convertAlter_table_stmtContext(n *parser.Alter_table_stmtContext) a
8892
Table: parseTableName(n),
8993
Cmds: &ast.List{},
9094
}
91-
name := n.Column_name(0).GetText()
95+
name := identifier(n.Column_name(0).GetText())
9296
stmt.Cmds.Items = append(stmt.Cmds.Items, &ast.AlterTableCmd{
9397
Name: &name,
9498
Subtype: ast.AT_DropColumn,
@@ -826,7 +830,7 @@ func (c *cc) convertUnaryExpr(n *parser.Expr_unaryContext) ast.Node {
826830
if opCtx.MINUS() != nil {
827831
// Negative number: -expr
828832
return &ast.A_Expr{
829-
Name: &ast.List{Items: []ast.Node{&ast.String{Str: "-"}}},
833+
Name: &ast.List{Items: []ast.Node{&ast.String{Str: "-"}}},
830834
Rexpr: expr,
831835
}
832836
}
@@ -837,7 +841,7 @@ func (c *cc) convertUnaryExpr(n *parser.Expr_unaryContext) ast.Node {
837841
if opCtx.TILDE() != nil {
838842
// Bitwise NOT: ~expr
839843
return &ast.A_Expr{
840-
Name: &ast.List{Items: []ast.Node{&ast.String{Str: "~"}}},
844+
Name: &ast.List{Items: []ast.Node{&ast.String{Str: "~"}}},
841845
Rexpr: expr,
842846
}
843847
}

0 commit comments

Comments
 (0)