20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/minitest-instrument-db.rb', line 20
def self.configuration_at(database_yml, options = {})
options[:project] ||= "Unknown"
options[:machine] ||= begin
require 'socket'
Socket.gethostname
end
connection = ActiveRecord::Base.connection rescue nil
ActiveRecord::Base.configurations.merge! YAML::load( IO.read( database_yml ) )
ActiveRecord::Base.establish_connection("minitest-instrument-db")
ActiveRecord::Migrator.migrate( File.join( File.dirname(__FILE__), 'migrations' ) )
MinitestTestMethod.establish_connection("minitest-instrument-db")
ActiveSupport::Notifications.subscribe "test.finished" do |name, start, finish, id, payload|
MinitestTestMethod.create!(
project: options[:project],
machine: options[:machine],
test_id: id,
klass: payload[:klass],
method: payload[:test],
exception: !!payload[:exception],
execution_time: finish - start
)
end
ensure
if connection
config = ActiveRecord::Base.connection.instance_variable_get(:@config).stringify_keys
name, configuration = ActiveRecord::Base.configurations.find{|k,v| v == config}
ActiveRecord::Base.establish_connection(name)
end
end
|