Reputation: 89373
Heroku's database followers are pretty cool. They allow you to create a separate database that automatically keeps in sync with your master DB.
I want to use this technology to access my follower database instead of my master database for expensive read operations (but obviously I always want to write to my master database)
Is it possible to access both databases (the master for writing and the follower for reading) while I'm in my production environment? My ideal API would be something like Post.use_db(:follower).find(1)
or Post.find(1, :use_db => :follower)
?
Upvotes: 3
Views: 1789
Reputation: 4400
DbCharmer Has Been Suspended
In the last 2 years (while trying to make DbCharmer compatible with Rails 4.0) it has become more and more apparent, that I simply do not want to do this anymore. I do not need DbCharmer to support Rails 4.0+, while it is very clear that many users need it and constant nagging in the issues and the mailing list, asking for updates generated a lot of anxiety for me, anxiety I couldn’t do much about (the worst kind). As the result, since I simply do not see any good reasons to keep fighting this uphill battle (and developing stuff like this for ActiveRecord IS a constant battle!) I officially give up. read more
Makara might be an alternative: github.com/taskrabbit/makara
Original answer
As far as I know, Rails doesn't offer this feature out of the box. However I have found a gem called db-charmer, which seems to be very close to what you are looking for.
Here is the best part (Per-Query Connection Management) :
Sometimes you have select queries that you know you want to run on the master. This could happen for example when you have just added some data and need to read it back and not sure if it made it all the way to the slave yet or no. For this situation and a few others there is a set of methods we’ve added to ActiveRecord models:
User.on_master.find_by_activation_code(code)
on_slave - this method is used to force a query to be run on a slave even in situations when it’s been previously forced to use the master. If there is more than one slave, one would be selected randomly. Tis method has two forms as well: block and proxy.
on_db(connection) - this method is what makes two previous methods possible. It is used to switch a model’s connection to some db for a short block of code or even for one statement (two forms). It accepts the same range of values as the switch_connection_to method does. Example:
Comment.on_db(:olap).count
Post.on_db(:foo).find(:first)
Upvotes: 5