Rails:mysql &postgres 同时在同一个应用程序中? [英] Rails: mysql & postgres at the same time in the same app?
问题描述
你为什么会问?因为我已经在 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 数据库的模型中.当然,创建一个抽象类并让所有模型都使用该抽象类可能会更容易,因为这样更干燥.
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屋!