ruby-on-rails - 在 Rails 2.3 中从一个数据库读取并写入另一个数据库

标签 ruby-on-rails database sharding ruby-on-rails-2

Heroku 的 database followers很酷。它们允许您创建一个单独的数据库,自动与您的主数据库保持同步。

我想使用这项技术访问我的追随者数据库而不是我的主数据库以进行昂贵的读取操作(但显然我总是想写入我的主数据库)

当我在生产环境中时,是否可以同时访问两个数据库(用于写入的 master 和用于读取的 follower)?我理想的 API 类似于 Post.use_db(:follower).find(1)Post.find(1, :use_db => :follower)

最佳答案

DbCharmer 已暂停

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 可能是替代方案:github.com/taskrabbit/makara

原始答案

据我所知,Rails 并未提供开箱即用的此功能。但是我发现了一个名为 db-charmer 的 gem ,这似乎与您要查找的内容非常接近。

这是最好的部分(每查询连接管理):

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)

关于ruby-on-rails - 在 Rails 2.3 中从一个数据库读取并写入另一个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8900570/

相关文章:

c++ - 基于服务器的嵌入式数据库

elasticsearch - 如何通过Logstash控制Elastic Index中的分片数量?

database - 对 uuid 进行模运算以确定 shard_id

javascript - Ruby on Rails - 无法使用 Java 脚本将第二个应用程序作为弹出窗口打开

ruby-on-rails - sqlite3 1.3.6从heroku推送拒绝

mysql - 用于在第二个数据库表中搜索不明确字段的 SQL

java - 即使数据库不存在,也可以使用 Hibernate 即时创建 PostgreSQL 数据库

mongodb - 如何以编程方式使用MongoDB预拆分基于GUID的分片 key

ruby-on-rails - rails 4 : ActiveRecord-style I18n on a non-ActiveRecord object?

ruby-on-rails - 在选择 Rails 中设置提示值