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

Add writing_role configuration for database connection switching #19

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

joshuajansen
Copy link

This PR fixes #18 by adding a writing_role setting that gets used to wrap the save_records call in the rollup method in. If not configured it'll call and return block directly by default.

Usage could look something like this for the implementing codebase:

Rollup.writing_role = :writing

ActiveRecord::Base.connected_to(role: :replica, prevent_writes: true) do
  Invoice.group(:project_id).rollup("Revenue per project") { |i| i.sum(:total_amount_cents) }
end

Which would then fetch the data from the replica and then switch to writing the rollup to rollups using the writing role.

I'm very open to a less monkey-patchy way of testing this! And of course to any feedback on the implementation in general.

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.

Switch database connection between reading and writing rollup
1 participant