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

Do not use Arel.star when ignored_columns #27918

Closed
wants to merge 2 commits into from

Conversation

maclover7
Copy link
Contributor

Summary

If there are any ignored columns, we will now list out all columns we
want to be returned from the database.

Other Information

  • Includes a regression test.
  • Re: the test I had to change, it should have never been passing in the
    first place, due to the ignored first_name column on `Developer.


test "when #reload called, ignored columns' attribute methods are not defined" do
developer = Developer.create!(name: 'Developer')
refute developer.respond_to?(:first_name)
Copy link
Member

Choose a reason for hiding this comment

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

It looks like you could also use assert_not_respond_to developer, :first_name although I guess refute is used elsewhere in this file. Any reason to use one vs. the other?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@composerinteralia No real reason, just went with what was used elsewhere.

@@ -1042,7 +1042,11 @@ def build_select(arel)
if select_values.any?
arel.project(*arel_columns(select_values.uniq))
else
arel.project(@klass.arel_table[Arel.star])
if @klass.ignored_columns.any?
arel.project(*arel_columns(@klass.columns.map(&:name)))
Copy link
Member

Choose a reason for hiding this comment

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

There's a memoized column_names method you could use here.

@@ -1042,7 +1042,11 @@ def build_select(arel)
if select_values.any?
arel.project(*arel_columns(select_values.uniq))
else
arel.project(@klass.arel_table[Arel.star])
if @klass.ignored_columns.any?
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need this conditional. Just always do column_names.

Copy link
Member

Choose a reason for hiding this comment

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

We don't need it, but it does make for much nicer looking queries in the common case. 😕

@maclover7
Copy link
Contributor Author

updated @eugeneius @sgrif

@sgrif, once we're set on the new implementation in lib, I'm happy to fix any failing tests.

Copy link
Contributor

@sgrif sgrif left a comment

Choose a reason for hiding this comment

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

Looks fine other than the indentation thing

arel.project(@klass.arel_table[Arel.star])
end
columns_to_select = if select_values.any?
select_values.uniq
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just indent this 2 spaces rather than block indenting?

Copy link
Member

Choose a reason for hiding this comment

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

Rubocop is asking this indentation. I believe because this rule is broken and needs to be fixed https://github.com/rails/rails/blob/master/.rubocop.yml#L116.

refute developer.respond_to?(:first_name)
refute developer.respond_to?(:first_name=)

developer.reload
Copy link
Member

Choose a reason for hiding this comment

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

If we introduce changes to build_select, should we also assert that to_sql doesn't include the ignored column?

If there are any ignored columns, we will now list out all columns we
want to be returned from the database.

Includes a regression test.

Re: the test I had to change, it should have never been passing in the
first place, due to the ignored `first_name` column on `Developer.
@@ -1042,7 +1042,11 @@ def build_select(arel)
if select_values.any?
arel.project(*arel_columns(select_values.uniq))
else
Copy link
Member

Choose a reason for hiding this comment

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

Can we use a elsif here?

@sobrinho
Copy link
Contributor

sobrinho commented Oct 4, 2017

@maclover7 can you take a look at @rafaelfranca's feedback?

@sobrinho
Copy link
Contributor

Providing some feedback, I'm using this patch in production for a couple weeks now and everything is working as expected 👍

rafaelfranca added a commit to rafaelfranca/omg-rails that referenced this pull request Oct 20, 2017
Do not use `Arel.star` when `ignored_columns`
@rafaelfranca
Copy link
Member

@sobrinho feel free to grab this commit, do the changes necessary, fix the tests and open a new PR.

@sobrinho
Copy link
Contributor

@rafaelfranca thanks, done in #30980, this one can be closed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants