Rails:mysql&在同一应用程序中同时使用postgres? [英] Rails: mysql & postgres at the same time in the same app?

查看:71
本文介绍了Rails:mysql&在同一应用程序中同时使用postgres?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您为什么会问?因为我已经在mysql上构建了该应用程序,并且需要开始仅将postgres用于我的应用程序的GIS组件.最终,我将完全迁移到postgres,但与此同时我想知道是否有可能

Why you may ask? Because i have built the app on mysql and need to start using postgres for GIS component of my app only. Eventually i will migrate to postgres completely but in the mean time would like to know if this is possible

推荐答案

例如,如果您在您的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.

这篇关于Rails:mysql&amp;在同一应用程序中同时使用postgres?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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