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

Lint to forbid single-line comments in .squished SQL heredocs #1273

Open
reslario opened this issue May 7, 2024 · 0 comments
Open

Lint to forbid single-line comments in .squished SQL heredocs #1273

reslario opened this issue May 7, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@reslario
Copy link

reslario commented May 7, 2024

Is your feature request related to a problem? Please describe.

The Rails/SquishedSQLHeredocs lint requires SQL heredocs to be .squished, which, as the docs mention, can change the meaning of the statement, especially when it contains single-line comments. Consider the following snippet:

execute <<-SQL.squish
  DELETE FROM
  records a
      USING records b
  WHERE a.updated_at < b.updated_at -- keep most recently updated record
  AND a.field = b.field;
SQL

If you're aware of this lint, you're probably writing <<-SQL.squish by default, and might not think about how it would affect your SQL.

Describe the solution you'd like

I think there should be a lint to forbid single-line comments in squished SQL heredocs. There are three reasons why they might exist:

  • The author didn't consider the implications. In this case, the comment could easily cause a bug.
  • The author is aware of the fact that the comment will affect the following lines as well, and is fine with this. I would argue that this is obtuse, and a multi-line comment should be used instead.
  • It doesn't matter, for example when the comment is at the very end of an SQL snippet. In this case, I think it's fine to require a multi-line comment anyway.

I assume that the biggest challenge with implementing this would be parsing the SQL, especially considering the possibility of string interpolation.

Describe alternatives you've considered

Expect users not to make this mistake.

@koic koic added the bug Something isn't working label May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants