我需要确保备份服务器上存在不同数据库服务器上的记录。
我正在尝试使用RSpec执行此作为Sinatra项目:
describe 'BACKUP' do
puts 'BACKUP config'
puts DB_CONFIG[:BACKUP].inspect
p "Key.count #{Key.count}"
DB_CONFIG[:databases].each do |server,config|
p "****************************************************************"
p "Server #{server} "
p " Config #{config.inspect}"
p "Service.count #{Service.new(config).count}"
end
end
其中:
class Key < BillingTables
end
class BillingTables < ActiveRecord::Base
self.abstract_class = true
establish_connection DB_CONFIG[:BACKUP]
end
连接到一个数据库。
另一方面,我正在使用此类同时连接到另一个数据库:
class Service < CoreTables
end
class CoreTables < ActiveRecord::Base
self.abstract_class = true
def initialize(params = {})
establish_connection params
end
end
此脚本的输出如下:
=== Comparison Spec ===
backup config
{"adapter"=>"mysql2", "encoding"=>"utf8", "reconnect"=>true, "database"=>"backup", "pool"=>1, "username"=>"backup", "password"=>"password", "host"=>"xxx.xxx"}
"Key.count 3902"
"****************************************************************"
"Server a1 "
" Config {\"adapter\"=>\"mysql2\", \"encoding\"=>\"utf8\", \"reconnect\"=>true, \"database\"=>\"s1\", \"pool\"=>1, \"username\"=>\"s1\", \"password\"=>\"password\", \"host\"=>\"yyy.yyy\"}"
/Users/password123/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:570:in `retrieve_connection': No connection pool for Service (ActiveRecord::ConnectionNotEstablished)
答案 0 :(得分:0)
最后,我放弃了multipe active_records的想法,现在我只是执行mysql连接
begin
client = Mysql2::Client.new DB_CONFIG[:databases][server]
query = 'SELECT * FROM test'
result = client.query(client.escape(query),symbolize_keys: true)
rescue Exception => e
p "query error #{e.message}"
next
end