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

Conversation

brandur
Copy link
Contributor

@brandur brandur commented Mar 8, 2025

Follows up the addition of sqlctemplate in #794. I noticed while
adding functionality in with templates that it was quite easy to (1) add
a /* TEMPLATE tag to river_job.sql, (2) put in context parameters to
a driver, but then (3) forget to run make generate. The context
parameters are injected, but sqlctemplate no ops with a fast short
circuit because there's no /* TEMPLATE tag present in the generated
Go code that the driver is executing. This leads to confusion.

Here, add a few more error conditions:

  • If a context container is present without any /* TEMPLATE tags,
    error.

  • If any /* TEMPLATE tags are present without a context container,
    error.

This makes dumb bugs easier to catch because we get an explicit error
instead of them failing silently. Tests are updated to check for them.

@brandur brandur requested a review from bgentry March 8, 2025 08:53
Comment on lines 124 to 126
case !containerOK && !sqlContainsTemplate:
// Neither context container or template in SQL; short circuit fast because there's no work to do.
return sql, args, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch statements are executed top-to-bottom, so maybe you want this fast path short circuit to go first?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thx.

Follows up the addition of `sqlctemplate` in #794. I noticed while
adding functionality in with templates that it was quite easy to (1) add
a `/* TEMPLATE` tag to `river_job.sql`, (2) put in context parameters to
a driver, but then (3) forget to run `make generate`. The context
parameters are injected, but `sqlctemplate` no ops with a fast short
circuit because there's no `/* TEMPLATE` tag present in the generated
Go code that the driver is executing. This leads to confusion.

Here, add a few more error conditions:

* If a context container is present without any `/* TEMPLATE` tags,
  error.

* If any `/* TEMPLATE` tags are present without a context container,
  error.

This makes dumb bugs easier to catch because we get an explicit error
instead of them failing silently. Tests are updated to check for them.
@brandur brandur force-pushed the brandur-safer-template-usage branch from 565d684 to d261a51 Compare March 9, 2025 00:53
@brandur brandur merged commit 2ea8882 into master Mar 9, 2025
10 checks passed
@brandur brandur deleted the brandur-safer-template-usage branch March 9, 2025 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants