ruby - Switching databases in Rails/Postgres is causing error -
i'm trying setup testing environment can simulate writing master database , reading slave database. have parent model before_save
, after_save
callback switches slave , master.
i'm running error though activerecord::statementinvalid: pg::connectionbad: connection closed: rollback
application_record.rb
class applicationrecord < activerecord::base self.abstract_class = true before_save :switch_to_master after_save :switch_to_slave def switch_to_slave config = yaml.load_file(file.join(rails.root, "config", "database.yml"))[rails.env.to_s] applicationrecord.establish_connection(config) end def switch_to_master config = yaml.load_file(file.join(rails.root, "config", "database_master.yml"))[rails.env.to_s] applicationrecord.establish_connection(config) end end
master_slave_test.rb
require 'test_helper' class masterslavetest < actioncontroller::testcase def test_one @user = users(:john) @user.update(last_name: 'change') @user.update(last_name: 'change') assert "change", @user.reload.last_name end end
database.yml
test: adapter: postgresql database: autobrain_test_slave host: localhost
database_master.yml
test: adapter: postgresql database: autobrain_test host: localhost
Comments
Post a Comment