Skip to content

Commit e712a3d

Browse files
committed
fix(postgres): handle nil values correctly
1 parent 784f84e commit e712a3d

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

backend/postgres/store.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -277,23 +277,22 @@ func (store *EventStore) Insert(ctx context.Context, events ...event.Event) erro
277277
return fmt.Errorf("marshal %q event data: %w", evt.Name(), err)
278278
}
279279

280-
aggregateIDVal := aggregateID.String()
281-
aggregateVersionVal := strconv.Itoa(aggregateVersion)
280+
var (
281+
aggregateNameVal any = aggregateName
282+
aggregateIDVal any = aggregateID.String()
283+
aggregateVersionVal any = strconv.Itoa(aggregateVersion)
284+
)
285+
286+
if aggregateVersion == 0 && aggregateName == "" && aggregateID == uuid.Nil {
287+
aggregateVersionVal = nil
288+
}
282289

283290
if aggregateID == uuid.Nil {
284-
aggregateIDVal = "NULL"
285-
} else {
286-
aggregateIDVal = singleQuote(aggregateIDVal)
291+
aggregateIDVal = nil
287292
}
288293

289294
if aggregateName == "" {
290-
aggregateName = "NULL"
291-
} else {
292-
aggregateName = singleQuote(aggregateName)
293-
}
294-
295-
if aggregateVersion == 0 {
296-
aggregateVersionVal = "NULL"
295+
aggregateNameVal = nil
297296
}
298297

299298
builder := squirrel.
@@ -306,9 +305,7 @@ func (store *EventStore) Insert(ctx context.Context, events ...event.Event) erro
306305
columnAggregateName,
307306
columnAggregateVersion,
308307
columnData,
309-
).
310-
Values(evt.ID(), evt.Name(), evt.Time().UnixNano(), aggregateIDVal, aggregateName, aggregateVersionVal, b).
311-
PlaceholderFormat(squirrel.Dollar)
308+
).Values(evt.ID(), evt.Name(), evt.Time().UnixNano(), aggregateIDVal, aggregateNameVal, aggregateVersionVal, b).PlaceholderFormat(squirrel.Dollar)
312309

313310
sql, args, err := builder.ToSql()
314311
if err != nil {
@@ -340,7 +337,8 @@ func (store *EventStore) Find(ctx context.Context, id uuid.UUID) (event.Event, e
340337
columnData,
341338
).
342339
From(store.table).
343-
Where(squirrel.Eq{columnID: id})
340+
Where(squirrel.Eq{columnID: id}).
341+
PlaceholderFormat(squirrel.Dollar)
344342

345343
sql, args, err := builder.ToSql()
346344
if err != nil {
@@ -643,7 +641,3 @@ func indexSQL(name, table string, fields []string, unique bool) string {
643641
}
644642
return fmt.Sprintf("CREATE %s INDEX IF NOT EXISTS %s ON %s (%s)", uniqueOpt, name, table, strings.Join(fields, ", "))
645643
}
646-
647-
func singleQuote(s string) string {
648-
return "'" + s + "'"
649-
}

0 commit comments

Comments
 (0)