Skip to content
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

Safer template usage with more aggressive errors on likely problems #799

Merged
merged 1 commit into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions rivershared/sqlctemplate/sqlc_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,20 @@ func (r *Replacer) Run(ctx context.Context, sql string, args []any) (string, []a
// RunSafely is the same as Run, but returns an error in case of missing or
// extra templates.
func (r *Replacer) RunSafely(ctx context.Context, sql string, args []any) (string, []any, error) {
// If nothing present in context, short circuit quickly.
container, containerOK := ctx.Value(contextKey{}).(*contextContainer)
if !containerOK {
var (
container, containerOK = ctx.Value(contextKey{}).(*contextContainer)
sqlContainsTemplate = strings.Contains(sql, "/* TEMPLATE")
)
switch {
case !containerOK && !sqlContainsTemplate:
// Neither context container or template in SQL; short circuit fast because there's no work to do.
return sql, args, nil

case containerOK && !sqlContainsTemplate:
return "", nil, errors.New("sqlctemplate found context container but SQL contains no templates; bug?")

case !containerOK && sqlContainsTemplate:
return "", nil, errors.New("sqlctemplate found template(s) in SQL, but no context container; bug?")
}

r.cacheMu.RLock()
Expand All @@ -134,10 +144,6 @@ func (r *Replacer) RunSafely(ctx context.Context, sql string, args []any) (strin
return cachedSQL, args, nil
}

if !strings.Contains(sql, "/* TEMPLATE") {
return sql, args, nil
}

var (
templatesExpected = maputil.Keys(container.Templates)
templatesMissing []string // not preallocated because we don't expect any missing parameters in the common case
Expand Down
25 changes: 16 additions & 9 deletions rivershared/sqlctemplate/sqlc_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,43 @@ func TestReplacer(t *testing.T) {
return &Replacer{}, &testBundle{}
}

t.Run("NoContainer", func(t *testing.T) {
t.Run("NoContainerError", func(t *testing.T) {
t.Parallel()

replacer, _ := setup(t)

updatedSQL, args, err := replacer.RunSafely(ctx, `
_, _, err := replacer.RunSafely(ctx, `
SELECT /* TEMPLATE: schema */river_job;
`, nil)
require.NoError(t, err)
require.Nil(t, args)
require.Equal(t, `
SELECT /* TEMPLATE: schema */river_job;
`, updatedSQL)
require.EqualError(t, err, "sqlctemplate found template(s) in SQL, but no context container; bug?")
})

t.Run("NoTemplate", func(t *testing.T) {
t.Run("NoTemplateError", func(t *testing.T) {
t.Parallel()

replacer, _ := setup(t)

ctx := WithReplacements(ctx, map[string]Replacement{}, nil)

_, _, err := replacer.RunSafely(ctx, `
SELECT 1;
`, nil)
require.EqualError(t, err, "sqlctemplate found context container but SQL contains no templates; bug?")
})

t.Run("NoContainerOrTemplate", func(t *testing.T) {
t.Parallel()

replacer, _ := setup(t)

updatedSQL, args, err := replacer.RunSafely(ctx, `
SELECT 1;
`, nil)
require.NoError(t, err)
require.Nil(t, args)
require.Equal(t, `
SELECT 1;
`, updatedSQL)
require.Nil(t, args)
})

t.Run("BasicTemplate", func(t *testing.T) {
Expand Down
Loading