且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails:mysql&在同一应用程序中同时使用postgres?

更新时间:2022-06-09 07:07:01

例如,如果您在您的database.yml中有这样的内容(不要真正记住正确的属性,但我想您会想到的):

If for instance you, in your database.yml have something like this (don't really recall the correct attributes, but I think you get the idea):

postgres:
    adapter: postgres
    database: gis

mysql:
    adapter: mysql
    database: app

然后,您可以添加

establish_connection :postgres 

在应该使用Postgres数据库的模型中

. 当然,创建一个抽象类并让所有模型改为使用该抽象类可能会更容易,因为那样会增加DRYer.

in the models that should use the Postgres database. Of course, it might be easier to create an abstract class and make all the models to use that one instead since that is more DRYer.

class PostgresRecord::Base < ActiveRecord::Base
  self.abstract_class = true
  establish_connection :postgres
end

或者,由于您最终打算迁移到Postgres,因此您可能应该做相反的事情,将Postgres数据库设置为默认数据库并更改MySQL的连接.

Or, since you are planning on migrating to Postgres eventually, you probably should do the opposite, make the Postgres database default and change the connection for the MySQL.