Skip to content

Update select_all for Rails 7.1#50

Merged
a-lavis merged 2 commits intomainfrom
update_select_all
Jan 17, 2024
Merged

Update select_all for Rails 7.1#50
a-lavis merged 2 commits intomainfrom
update_select_all

Conversation

@a-lavis
Copy link
Copy Markdown
Contributor

@a-lavis a-lavis commented Jan 9, 2024

@a-lavis a-lavis changed the title Update select_all for Rails 7 Update select_all for Rails 7.1 Jan 9, 2024
args[2] = binds
sql, binds, preparable = to_sql_and_binds(arel, binds, preparable)

if Gem::Version.new(ActiveRecord.version) < Gem::Version.new('5.1')
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

s.add_dependency('activerecord', ["> 6.0", "< 8.0"])

We don't support versions of activerecord below 5.1, so no need for this conditional anymore.

Comment on lines -21 to +22
def select_all(*args, **kwargs)
relation, name, raw_binds = args

if !query_cache_enabled || locked?(relation)
return route_to(current, :select_all, *args, **kwargs)
def select_all(arel, name = nil, binds = [], *args, preparable: nil, **kwargs)
if !query_cache_enabled || locked?(arel)
Copy link
Copy Markdown
Contributor Author

@a-lavis a-lavis Jan 9, 2024

Choose a reason for hiding this comment

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

I moved relation, name, and raw_binds into the method signature as arel, name, and binds because I thought it made the method much easier to understand / read, and also easier to compare with the method we are imitating from activerecord.

@a-lavis a-lavis marked this pull request as ready for review January 9, 2024 16:56

# duplicate binds_from_relation behavior introduced in 4.2.
if raw_binds.blank? && relation.is_a?(ActiveRecord::Relation)
arel, binds = relation.arel, relation.bind_values
Copy link
Copy Markdown
Contributor Author

@a-lavis a-lavis Jan 9, 2024

Choose a reason for hiding this comment

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

This was the line that was erroring. #bind_values hasn't existed since Rails 5.2 (see rails/rails@213796f), which means this line of code hasn't been ran in a long time. With the upgrade to Rails 7.1, we are running into this line of code again.

The reason this line of code is now running in Rails 7.1 is because the #ids method was redefined: rails/rails@8fd3369#diff-d736385b70592e7f46357f56e80d3fcf8baddf3f12b6725eb133554f66d4691cL325-R346.

An easy way to test this is by running the test test/serializers/comment_serializer_test.rb, which calls this line of code which uses #ids: https://github.com/kickstarter/kickstarter/blob/12bfc7a58a524f7ec200e6cb75f1a59ad96d22d9/app/models/comment.rb#L203


if !query_cache_enabled || locked?(relation)
return route_to(current, :select_all, *args, **kwargs)
def select_all(arel, name = nil, binds = [], *args, preparable: nil, **kwargs)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added preparable: because it is passed into to_sql_and_binds.

Comment thread spec/query_cache_spec.rb
Comment on lines +120 to +130
describe '.ids regression test' do
it 'should work with query caching' do
TestModel.connection.enable_query_cache!
expect(TestModel.ids.count).to eql TestModel.all.count
end

it 'should work if query cache is not enabled' do
TestModel.connection.disable_query_cache!
expect(TestModel.ids.count).to eql TestModel.all.count
end
end
Copy link
Copy Markdown
Contributor Author

@a-lavis a-lavis Jan 9, 2024

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

@warrenwegs warrenwegs left a comment

Choose a reason for hiding this comment

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

Pair reviewed with @a-lavis Looks good!

@a-lavis a-lavis merged commit db79b8f into main Jan 17, 2024
@a-lavis a-lavis deleted the update_select_all branch January 17, 2024 19:16
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