Skip to content

Commit ec5e7b1

Browse files
committed
fix: guard proto fields against missing SQL columns
1 parent 9dbecc3 commit ec5e7b1

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

db_proto/sql/database.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"time"
88

9+
"github.com/golang/protobuf/protoc-gen-go/descriptor"
910
"github.com/jhump/protoreflect/desc"
1011
"github.com/jhump/protoreflect/dynamic"
1112
sink "github.com/streamingfast/substreams-sink"
@@ -107,8 +108,13 @@ func (d *BaseDatabase) WalkMessageDescriptorAndInsertWithDialect(dm *dynamic.Mes
107108
d.logger.Debug("Walking message descriptor", zap.String("message_descriptor_name", md.GetName()), zap.Any("table_info", tableInfo))
108109
// Keep the actual PK value handy so we don't rely on slice indexes later
109110
var primaryKeyValue any
111+
var columnDescriptors map[*desc.FieldDescriptor]struct{}
110112
if tableInfo != nil {
111113
if table := dialect.GetTable(tableInfo.Name); table != nil {
114+
columnDescriptors = make(map[*desc.FieldDescriptor]struct{}, len(table.Columns))
115+
for _, col := range table.Columns {
116+
columnDescriptors[col.FieldDescriptor] = struct{}{}
117+
}
112118
if table.PrimaryKey != nil {
113119
// Robust PK retrieval: always use the original protobuf field name
114120
// from the descriptor (not the possibly renamed SQL column name).
@@ -143,6 +149,14 @@ func (d *BaseDatabase) WalkMessageDescriptorAndInsertWithDialect(dm *dynamic.Mes
143149
}
144150
}
145151
}
152+
153+
if fd.GetType() != descriptor.FieldDescriptorProto_TYPE_MESSAGE {
154+
if columnDescriptors != nil {
155+
if _, ok := columnDescriptors[fd]; !ok {
156+
continue
157+
}
158+
}
159+
}
146160
fv := dm.GetField(fd)
147161
if v, ok := fv.([]interface{}); ok {
148162
// Check if this is an array of messages or native values

0 commit comments

Comments
 (0)