Skip to content

Commit d8c3909

Browse files
authored
Fix bug in sqltemplate cache path around args order (#1243)
Related to #1242. Activating caching everywhere revealed a bug in that we were adding named args using `maputil.Values`. The problem with this is that `maputil.Values` returns values in an arbitrary order, resulting in all kinds of odd things potentially happening during a query as args are switched around. Here, fix the problem by appending args in the same order as incoming map keys.
1 parent 2109122 commit d8c3909

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- Fix unsafe concurrent producer map access in client. [PR #1236](https://github.com/riverqueue/river/pull/1236).
13+
- Fix bug in `sqltemplate` cached path in order in which named args are passed to a query (previously, the order was unstable). [PR #1243](https://github.com/riverqueue/river/pull/1243).
1314

1415
## [0.35.1] - 2026-04-26
1516

rivershared/sqlctemplate/sqlc_template.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,14 @@ func (r *Replacer) RunSafely(ctx context.Context, argPlaceholder, sql string, ar
156156
// If all input templates were stable, the finished SQL will have been cached.
157157
if cachedSQLOK {
158158
if len(container.NamedArgs) > 0 {
159-
args = append(args, maputil.Values(container.NamedArgs)...)
159+
// Named args must be appended in sorted order to match the
160+
// placeholder positions baked into the cached SQL during
161+
// RunSafely's cache miss path.
162+
sortedNamedArgs := maputil.Keys(container.NamedArgs)
163+
slices.Sort(sortedNamedArgs)
164+
for _, name := range sortedNamedArgs {
165+
args = append(args, container.NamedArgs[name])
166+
}
160167
}
161168
return cachedSQL, args, nil
162169
}

0 commit comments

Comments
 (0)