-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DB-side UPSERT optimiziation support #138
Conversation
Signed-off-by: Peter Broadhurst <[email protected]>
Signed-off-by: Peter Broadhurst <[email protected]>
Signed-off-by: Peter Broadhurst <[email protected]>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #138 +/- ##
=======================================
Coverage 99.96% 99.96%
=======================================
Files 79 80 +1
Lines 6507 6567 +60
=======================================
+ Hits 6505 6565 +60
Misses 1 1
Partials 1 1 ☔ View full report in Codecov by Sentry. |
Signed-off-by: Peter Broadhurst <[email protected]>
Signed-off-by: Peter Broadhurst <[email protected]>
if err != nil || !ok { | ||
return insert, i18n.NewError(ctx, i18n.MsgDBErrorBuildingStatement, err) | ||
} | ||
return insert.Suffix(fmt.Sprintf("ON CONFLICT (%s) DO UPDATE", idColumn)).SuffixExpr(sq.Expr(updateSQL, updateValues...)).Suffix("RETURNING " + returnCol), nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this alternate cheat would also work (using a single space as the table name, and then not performing any stripping):
insert = sq.Insert(table).Columns(insertCols...).Values(insertValues...)
update := sq.Update(" " /* cheat to avoid table name */)
for _, c := range updateCols {
update = update.Set(c, values[c])
}
updateSQL, updateValues, err := update.ToSql()
if err != nil {
return insert, i18n.NewError(ctx, i18n.MsgDBErrorBuildingStatement, err)
}
return insert.Suffix(fmt.Sprintf("ON CONFLICT (%s) DO", idColumn)).
SuffixExpr(sq.Expr(updateSQL, updateValues...)).
Suffix("RETURNING " + returnCol), nil
Happy for you to keep it as is or switch it (not sure either is better/worse - just noting it as a possibility).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Going to merge for now, thank you though 👍
UPSERT
functionalitysq
semantics called out in comments