Rails - 具有多个数据库的相同模型(只读) [英] Rails - same model with multiple databases (read only)

查看:115
本文介绍了Rails - 具有多个数据库的相同模型(只读)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个数据库,内部具有相同的表和列名称(但不同的唯一ID和行..)。而不是有一个巨大的数据库所有的行,它分为不同的数据库。这是我不能改变的(你可以认为这是收集来自不同国家的相同数据,但每个国家都有自己的数据库)。
这些数据库是只读的 - 这意味着,当我通过Rails使用它们时,它只是显示数据(或保存在本地数据库) - 我不更改任何远程数据库。



我的问题是,我需要在rails中有1个模型,从所有这些数据库收集数据。我们希望能够做以下事情:

  OneModelAllDB.select_where(...)

,而不是将每个搜索拆分为:

  data1 = FirstDBModel.select_where(same_condition)
data2 = SecondDBModel.select_where(same_condition)
...
data = data1 + data2 + ...

此外,如果我想使用线程(并行搜索)创建1个模型,还有一个问题:

  [:db1,:db2] .each do | db | 
threads<< Thread.new do
self.establish_connection(db)
results [db] = self.where(bla_bla_condition)
end
end



因为连接的修改不是线程安全的...



这样做?由于我们不更改任何这些数据库,并且每一行都有唯一的ID,因此从不同的数据库获取数据并将它们加入...应该不会有任何问题...



谢谢!

解决方案

Rails活动记录是从数据库连接抽取的。连接是单独管理的。你不会有像 FirstDB.select_where(...)



try是在 config / database.yml 中定义不同的数据库,例如:

  db1:
adapter:postgresql
encoding:unicode
database:database1
pool:5
用户名:xxx
密码:xxx

db2:
adapter:postgresql
encoding:unicode
database:database2
pool:5
用户名:xxx
密码:xxx

然后在您的代码中,您将使用 ActiveRecord :: Base.establish_connection 在运行查询之前重新建立与所需的数据库的连接。如果您的活动记录模型称为 Foo ,则:

  establish_connection(:db1)
data1 = Foo.where(...)

Foo.establish_connection(:db2)
data2 = Foo.where $ b

我没有尝试过这个细节,但它应该是这样的。


I have multiple databases, with the same tables and columns names inside (but different unique ids and rows..). Instead of having one huge database with all the rows, it is splitted into different databases. This is something I cannot change (you can think of this as collecting the same data from different countries, but each country has its own database). These databases are "read only" - meaning, when I use them via Rails, it is only to display the data (or save it on a local database) - I do not change the data on any of the remote databases.

My problem is, that I need to have 1 model in rails, that collects the data from all these databases. We want to be able to do something like:

OneModelAllDB.select_where(...)

and not to split each search to:

data1 = FirstDBModel.select_where(same_condition)
data2 = SecondDBModel.select_where(same_condition)
...
data = data1 + data2 + ...

Also, if I want to make 1 model with threads (parallel searches), there is a problem:

[:db1, :db2].each do |db|
  threads << Thread.new do
    self.establish_connection(db)
    results[db] = self.where(bla_bla_condition)
  end
end

because the modification of the connection is not threadsafe...

Is there any way to do so? As we do not change any of these databases, and each row has a unique id, there shouldn't be any problem to get the data from different databases and join it together...

Thanks!

解决方案

Rails active records are abstracted from the database connection. The connection is managed separately. You won't have something like, FirstDB.select_where(...).

However, what you can try is define your different databases in your config/database.yml, for example:

db1:
  adapter: postgresql
  encoding: unicode
  database: database1
  pool: 5
  username: xxx
  password: xxx

db2:
  adapter: postgresql
  encoding: unicode
  database: database2
  pool: 5
  username: xxx
  password: xxx

Then in your code, you would use ActiveRecord::Base.establish_connection to re-establish the connection to the database you want before running the query. If your active record model is called Foo, then:

Foo.establish_connection(:db1)
data1 = Foo.where(...)

Foo.establish_connection(:db2)
data2 = Foo.where(...)

I have not tried this in detail myself, but it should work something like that.

这篇关于Rails - 具有多个数据库的相同模型(只读)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆