Skip to content

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.

@onerinas
Copy link

onerinas commented Sep 3, 2025

This would be a great addition.

I got a question, where would Rollup.writing_role = :writing go? Would that be a config change in initializer?

@onerinas
Copy link

onerinas commented Sep 3, 2025

Just throwing out a random idea incase it is possible without too much work.

Is there a possibility to instruct rollup to use replica? e.g. Rollup.use_replica = true

Then use rollup as usual?

Invoice.group(:project_id).rollup("Revenue per project") { |i| i.sum(:total_amount_cents) }

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

2 participants