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

Use Heredocs for multiline SQL #147

Merged
merged 1 commit into from Jul 1, 2015
Merged

Conversation

kurtzur
Copy link
Contributor

@kurtzur kurtzur commented Jun 28, 2015

I didn't see anything in this guide covering the format of multiline SQL strings used with methods like find_by_sql or ActiveRecord::Base.connection.execute. I know the preferred approach is to use ActiveRecord helpers, but as even the Rails guides themselves admit, this isn't always sufficient. As such, I decided to include the practice we currently follow at OneLogin.

The content I added is pretty self-explanatory, but we use heredocs because they're recommended for multiline strings and enable SQL syntax highlighting in many tools. Because the indentations and line breaks are only meant for readers of the source, the heredoc is immediately followed by squish to suppress any extra characters in server logs.

@@ -658,6 +658,32 @@ when you need to retrieve a single record by some attributes.
# good
User.where.not(id: id)
```
* <a name="squished-heredocs"</a>
Copy link

Choose a reason for hiding this comment

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

I believe you're missing a > from the link.

@kurtzur kurtzur force-pushed the squished-heredocs branch 2 times, most recently from fc8989d to d8d8463 Compare June 29, 2015 06:24
@onebree
Copy link

onebree commented Jun 29, 2015

+1 for this pull request. Readability greatly increases, and you can better follow the flow of the query. A couple weeks ago I had to write a long query, but kept it as a normal string, because I did not know about heredocs.

@ausmith
Copy link

ausmith commented Jun 30, 2015

👍

1 similar comment
@paradox460
Copy link

👍

SQL
```

`squish` removes the indentation and newline characters so that your server
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe this should be String#squish and be a link to the docs (which might outdated, though).

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 1, 2015

Looks good overall. I'd just suggest capitalising the commit message and making it a bit more specific, as it's pretty generic right now.

@kurtzur
Copy link
Contributor Author

kurtzur commented Jul 1, 2015

@bbatsov done

bbatsov added a commit that referenced this pull request Jul 1, 2015
@bbatsov bbatsov merged commit 7de8209 into rubocop:master Jul 1, 2015
@bbatsov
Copy link
Collaborator

bbatsov commented Jul 1, 2015

Perfect! Thanks!

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.

None yet

5 participants