简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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