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

railtie: Add relation extension #41

Merged
merged 6 commits into from
Dec 13, 2023
Merged

railtie: Add relation extension #41

merged 6 commits into from
Dec 13, 2023

Conversation

ethan-readyset
Copy link
Contributor

This commit adds two methods to ActiveRecord::Relation:

  • Relation#create_readyset_cache!, which creates a new cache based on the query represented by the relation; and
  • Relation#drop_cache!, which drops the cache associated with the query represented by the relation (if one exists)

@ethan-readyset ethan-readyset marked this pull request as draft December 7, 2023 23:39
@ethan-readyset ethan-readyset removed the request for review from helpotters December 7, 2023 23:39
@ethan-readyset ethan-readyset marked this pull request as ready for review December 8, 2023 22:30
@ethowitz ethowitz force-pushed the add-relation-extension branch from 111b5b2 to 78d61ad Compare December 8, 2023 22:38
This commit adds two methods to `ActiveRecord::Relation`:
- `Relation#create_readyset_cache!`, which creates a new cache based on
  the query represented by the relation; and
- `Relation#drop_cache!`, which drops the cache associated with the
  query represented by the relation (if one exists)
@ethowitz ethowitz force-pushed the add-relation-extension branch from 78d61ad to 193fe1b Compare December 8, 2023 22:46
@ethan-readyset ethan-readyset changed the base branch from controller-extensions to main December 8, 2023 22:51
@helpotters
Copy link
Contributor

Regarding the extension, I'm looking into how we can confirm that we're handling it safely, especially if we're aiming for good encapsulation of behaviors.

Like we're assuming that users won't just make a web interface and allow users to pass in whatever they want to this module.

I'll provide the actual feedback soon!

@ethan-readyset
Copy link
Contributor Author

@helpotters What kinds of safety concerns do you have? If you're worried about SQL injections, I think it'd be up to users to ensure that they are properly escaping their queries before create/drop_readyset_cache! is invoked

@ethan-readyset
Copy link
Contributor Author

In terms of whether we should be extending ActiveRecord::Relation, we could always have users include the Readyset::RelationExtension module into their ApplicationRecord base class instead. The methods we defined here seemed pretty low risk though -- we're not changing any of the relation's internal state, and the method names are very unlikely to ever conflict with any methods defined internally by Rails. But curious to hear what you think!

@helpotters
Copy link
Contributor

@helpotters What kinds of safety concerns do you have? If you're worried about SQL injections, I think it'd be up to users to ensure that they are properly escaping their queries before create/drop_readyset_cache! is invoked

It's that and also the query builder (string concat vs adding to an array). I personally just don't know enough to be certain in either direction. So, I'm just going to look over some examples.

ethan-readyset and others added 2 commits December 11, 2023 11:13
Our approach to routing queries requires more granularity than is
provided by the ActiveRecord middleware, so this commit removes the code
that integrates with that middleware.

NOTE: This PR is stacked on top of #41 and must be merged after that is
merged
lib/readyset.rb Outdated Show resolved Hide resolved
lib/readyset.rb Show resolved Hide resolved
spec/ready_set_spec.rb Show resolved Hide resolved
Copy link
Contributor

@helpotters helpotters left a comment

Choose a reason for hiding this comment

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

Overall, this is good to go. The only requested changes are to the query builder (which could maybe be extracted to a method if we're going to use it anywhere else).

This is fantastic work, Ethan! Good test coverage.

@helpotters
Copy link
Contributor

Also, after reviewing this gem, I decided to finish up PR #32 (Logger).

Methinks this PR could be made more robust for the developer by providing more feedback about what's going on.

For example, within #create_cache:

  Readyset::Logger.info("Creating cache with name: #{name}")

  begin
    # Existing code for cache creation
  rescue ArgumentError => e
    Readyset::Logger.error("ArgumentError in create_cache!: #{e.message}")
    raise
  rescue => e
    Readyset::Logger.error("Unexpected error in create_cache!: #{e.message}")
    raise
  end

  Readyset::Logger.info("Cache successfully created with name: #{name}")

Here are the specs for Readyset::Logger as reference:

it_behaves_like 'a logger method', :debug, 'This is a debug message'
it_behaves_like 'a logger method', :info, 'This is an info message'
it_behaves_like 'a logger method', :warn, 'This is a warn message'
it_behaves_like 'a logger method', :error, 'This is an error message'
it_behaves_like 'a logger method', :fatal, 'This is an fatal message'
it_behaves_like 'a logger method', :unknown, 'This is an unknown message'

context 'with invalid log level' do
  it 'raises an error' do
    expect { Readyset::Logger.log(:invalid_level, 'message') }.to raise_error(ArgumentError)
  end
end

@ethan-readyset
Copy link
Contributor Author

@helpotters Ready for another pass -- thanks for the great comments!

Copy link
Contributor

@helpotters helpotters left a comment

Choose a reason for hiding this comment

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

After taking a second look at the spec, the query let introduces Mystery Guests to the specs, mainly due to it being outside of the spec itself and also it being redefined later.

Everything else is solid, but I left some commentary on the topic of subject that I don't think warrants the effort of a change due to the time crunch.

lib/readyset.rb Show resolved Hide resolved
spec/ready_set_spec.rb Show resolved Hide resolved
lib/readyset.rb Show resolved Hide resolved
@helpotters
Copy link
Contributor

I've given some thought to my review process, and I think it has been both delayed and causing blocks in our progress.

The feature itself is solid, I only had minor nitpicks about specs. While that does reflect on the overall codebase, given the dependencies of other PRs on this one, and the fact that the feature itself wouldn't be changed by the remaining feedback, it would be best to simply merge it.

Granted, I think we should consider a better git workflow, mainly by working with a dev branch, and allowing dependent PRs to work off feature branches (rebasing and merging when appropriate). I'm experimenting with this by creating a dependent feature branch (origin/feature-query-annotation off of this PR's branch origin/add-route-and-exclude.

@helpotters helpotters added this pull request to the merge queue Dec 13, 2023
Merged via the queue into main with commit ecb7b06 Dec 13, 2023
8 checks passed
helpotters pushed a commit that referenced this pull request Dec 13, 2023
Our approach to routing queries requires more granularity than is
provided by the ActiveRecord middleware, so this commit removes the code
that integrates with that middleware.

NOTE: This PR is stacked on top of #41 and should only be merged after
that is merged
github-merge-queue bot pushed a commit that referenced this pull request Dec 13, 2023
Our approach to routing queries requires more granularity than is
provided by the ActiveRecord middleware, so this commit removes the code
that integrates with that middleware.

@helpotters I think this got lost somehow when #41 was merged, so
reopening here!
helpotters added a commit that referenced this pull request Dec 15, 2023
This commit adds two methods to `ActiveRecord::Relation`:
- `Relation#create_readyset_cache!`, which creates a new cache based on
the query represented by the relation; and
- `Relation#drop_cache!`, which drops the cache associated with the
query represented by the relation (if one exists)

---------

Co-authored-by: Paul Lemus <paullemus@protonmail.com>
helpotters pushed a commit that referenced this pull request Dec 15, 2023
Our approach to routing queries requires more granularity than is
provided by the ActiveRecord middleware, so this commit removes the code
that integrates with that middleware.

@helpotters I think this got lost somehow when #41 was merged, so
reopening here!
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.

Add extensions to ActiveRecord query syntax to create and drop caches
3 participants