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

Rails/SquishedSQLHeredocs not safe for autocorrect #365

Closed
Edderic opened this issue Sep 30, 2020 · 0 comments · Fixed by #366
Closed

Rails/SquishedSQLHeredocs not safe for autocorrect #365

Edderic opened this issue Sep 30, 2020 · 0 comments · Fixed by #366
Labels
enhancement New feature or request

Comments

@Edderic
Copy link

Edderic commented Sep 30, 2020

It is currently unsafe for rubocop -a to add .squish on SQL heredocs. SQL code
in Postgresql breaks when those containing new line comments are squished.

Current code for squishing doesn't address ---type comments:


Expected behavior

When a SQL string has a comment, .squish is not appropriate:

For example, when we have some code like so:

<<~SQL
  -- some comment that should not be included
  select * from some_table;
SQL

rubocop -a should not add .squish:

<<~SQL
  -- some comment that should not be included
  select * from some_table;
SQL

Actual behavior

rubocop -a adds a .squish for SQL with comments:

<<~SQL.squish
  -- some comment that should not be included
  select * from some_table;
SQL

Squishing of comments starting with -- leads to Postgresql complaining. See article on Postgresql about how comments starting with -- should have a line break after it:

Syntax Using -- symbol
The syntax for creating a SQL comment in PostgreSQL using -- symbol is:

-- comment goes here
In PostgreSQL, a comment started with -- symbol is similar to a comment starting with # symbol. When using the -- symbol, the comment must be at the end of a line in your SQL statement with a line break after it. This method of commenting can only span a single line within your SQL and must be at the end of the line.

Steps to reproduce the problem

Put this in reproduce_squish_issue.rb:

<<~SQL
  -- some comment that should not be included
  select * from some_table;
SQL

Then run rubocop -a --only Rails/SquishedSQLHeredocs reproduce_squish_issue.rb

RuboCop version

$ rubocop -V
0.88.0 (using Parser 2.7.1.4, rubocop-ast 0.3.0, running on ruby 2.6.6 x86_64-darwin19)
@Drenmi Drenmi added the enhancement New feature or request label Sep 30, 2020
tejasbubane added a commit to tejasbubane/rubocop-rails that referenced this issue Sep 30, 2020
tejasbubane added a commit to tejasbubane/rubocop-rails that referenced this issue Nov 23, 2020
tejasbubane added a commit to tejasbubane/rubocop-rails that referenced this issue Nov 23, 2020
tejasbubane added a commit to tejasbubane/rubocop-rails that referenced this issue Nov 23, 2020
tejasbubane added a commit to tejasbubane/rubocop-rails that referenced this issue Nov 24, 2020
@koic koic closed this as completed in #366 Nov 24, 2020
koic added a commit that referenced this issue Nov 24, 2020
[Fix #365] Mark `Rails/SquishedSQLHeredocs` unsafe for autocorrection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants