简体   繁体   English

关闭Postgre DB连接,并在轨道上使用其他DB rub​​y​​打开新连接

[英]Close the Postgre DB connection and open a new connection with different DB ruby on rails

I'm migrating the previous database to my new application I have a dump file imported from heroku and there are some tables missing in the dump file as well in almost every table there are some attributes missing so for this I'm creating a new database and imported the dump in that database, and when I imported that from that DB and close the connection so that I can create a new connection with the production database where I need to migrate the data the connection is closed but the instance variables which I stored the data from the previous database is also reset so can anybody tell me how to close the Postgre DB connection and open a new connection to a different PG DB without loosing the saved data fetched from previous DB to populate the second DB. 我正在将以前的数据库迁移到我的新应用程序中,我有一个从heroku导入的转储文件,并且转储文件中还缺少一些表,几乎每个表中都缺少一些属性,因此为此我创建了一个新数据库然后将转储导入到该数据库中,然后当我从该数据库中导入转储并关闭连接,以便可以与需要迁移数据的生产数据库创建新连接时,该连接已关闭,但是我存储的实例变量来自先前数据库的数据也将被重置,因此任何人都可以告诉我如何关闭Postgre DB连接并打开与其他PG DB的新连接,而不会丢失从先前DB获取的已保存数据来填充第二DB。

I am using this to establish the connection 我正在用它来建立连接

 ActiveRecord::Base.establish_connection('DB')

and I'm using to close the DB connection 我正在用来关闭数据库连接

 ActiveRecord::Base.clear_active_connections!

Please answer my question? 请回答我的问题?

I solved this by creating setup for two databases in the database.yml and added two methods for database connections one for one database connection using the 我通过在database.yml中为两个数据库创建安装程序解决了这一问题,并添加了两种数据库连接方法,一种是使用

def xyz_connection
  ActiveRecord::Base.establish_connection('DB')
end

and second for second DB connection like 第二个用于第二个数据库连接

def abc_connection
  ActiveRecord::Base.establish_connection('DB')
end

and called them both where required and I did my insertions and DB manipulations and after that I suspended the connection from required DB. 并在需要的地方调用它们,然后进行插入和数据库操作,然后中止与所需数据库的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM