-
Notifications
You must be signed in to change notification settings - Fork 369
/
multi_db_spec.rb
241 lines (204 loc) · 6.99 KB
/
multi_db_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
require 'ddtrace/contrib/support/spec_helper'
require 'ddtrace'
require 'ddtrace/contrib/integration_examples'
require 'active_record'
if PlatformHelpers.jruby?
require 'activerecord-jdbc-adapter'
else
require 'mysql2'
require 'sqlite3'
end
RSpec.describe 'ActiveRecord multi-database implementation' do
let(:configuration_options) { { service_name: default_db_service_name } }
let(:default_db_service_name) { 'default-db' }
let(:mysql) do
{
database: ENV.fetch('TEST_MYSQL_DB', 'mysql'),
host: ENV.fetch('TEST_MYSQL_HOST', '127.0.0.1'),
password: ENV.fetch('TEST_MYSQL_ROOT_PASSWORD', 'root'),
port: ENV.fetch('TEST_MYSQL_PORT', '3306')
}
end
def mysql_connection_string
"mysql2://root:#{mysql[:password]}@#{mysql[:host]}:#{mysql[:port]}/#{mysql[:database]}"
end
let(:application_record) do
stub_const('ApplicationRecord', Class.new(ActiveRecord::Base) do
self.abstract_class = true
end)
end
let!(:gadget_class) do
stub_const('Gadget', Class.new(application_record)).tap do |klass|
# Connect to the default database
ActiveRecord::Base.establish_connection(mysql_connection_string)
begin
klass.count
rescue ActiveRecord::StatementInvalid
ActiveRecord::Schema.define(version: 20180101000000) do
create_table 'gadgets', force: :cascade do |t|
t.string 'title'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
end
end
# Prevent extraneous spans from showing up
klass.count
end
end
end
let!(:widget_class) do
stub_const('Widget', Class.new(application_record)).tap do |klass|
# Connect the Widget database
klass.establish_connection(adapter: 'sqlite3', database: ':memory:')
begin
klass.count
rescue ActiveRecord::StatementInvalid
klass.connection.create_table 'widgets', force: :cascade do |t|
t.string 'title'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
end
# Prevent extraneous spans from showing up
klass.count
end
end
end
subject(:count) do
gadget_class.count
widget_class.count
end
let(:gadget_span) { spans[0] }
let(:widget_span) { spans[1] }
before(:each) do
Datadog.registry[:active_record].reset_configuration!
Datadog.configure do |c|
c.use :active_record, configuration_options
end
end
after(:each) do
Datadog.registry[:active_record].reset_configuration!
end
context 'when databases are configured with' do
let(:gadget_db_service_name) { 'gadget-db' }
let(:widget_db_service_name) { 'widget-db' }
context 'a Symbol that matches a configuration' do
context 'when ActiveRecord has configurations' do
let(:database_configuration) do
{
'gadget' => {
'encoding' => 'utf8',
'adapter' => 'mysql2',
'username' => 'root',
'host' => mysql[:host],
'port' => mysql[:port].to_i,
'password' => mysql[:password],
'database' => mysql[:database]
},
'widget' => {
'adapter' => 'sqlite3',
'pool' => 5,
'timeout' => 5000,
'database' => ':memory:'
}
}
end
let(:database_configuration_object) do
if defined?(ActiveRecord::DatabaseConfigurations)
ActiveRecord::DatabaseConfigurations.new(database_configuration)
else
# Before Rails 6, database configuration was a plain Hash
database_configuration
end
end
before(:each) do
# Stub ActiveRecord::Base, to pretend its been configured
allow(ActiveRecord::Base).to receive(:configurations).and_return(database_configuration_object)
Datadog.configure do |c|
c.use :active_record, describes: :gadget do |gadget_db|
gadget_db.service_name = gadget_db_service_name
end
c.use :active_record, describes: :widget do |widget_db|
widget_db.service_name = widget_db_service_name
end
end
end
it do
count
# Gadget is configured to show up as its own database service
expect(gadget_span.service).to eq(gadget_db_service_name)
# Widget is configured to show up as its own database service
expect(widget_span.service).to eq(widget_db_service_name)
end
end
end
context 'a String that\'s a URL' do
context 'for a typical server' do
before(:each) do
Datadog.configure do |c|
c.use :active_record, describes: mysql_connection_string do |gadget_db|
gadget_db.service_name = gadget_db_service_name
end
end
end
it do
count
# Gadget is configured to show up as its own database service
expect(gadget_span.service).to eq(gadget_db_service_name)
# Widget isn't, ends up assigned to the default database service
expect(widget_span.service).to eq(default_db_service_name)
end
it_behaves_like 'a peer service span' do
let(:span) { gadget_span }
end
it_behaves_like 'a peer service span' do
let(:span) { widget_span }
end
end
context 'for an in-memory database' do
before(:each) do
Datadog.configure do |c|
c.use :active_record, describes: 'sqlite3::memory:' do |widget_db|
widget_db.service_name = widget_db_service_name
end
end
end
it do
count
# Gadget belongs to the default database
expect(gadget_span.service).to eq(default_db_service_name)
# Widget belongs to its own database
expect(widget_span.service).to eq(widget_db_service_name)
end
it_behaves_like 'a peer service span' do
let(:span) { gadget_span }
end
it_behaves_like 'a peer service span' do
let(:span) { widget_span }
end
end
end
context 'a Hash that describes a connection' do
before(:each) do
widget_db_connection_hash = { adapter: 'sqlite3', database: ':memory:' }
Datadog.configure do |c|
c.use :active_record, describes: widget_db_connection_hash do |widget_db|
widget_db.service_name = widget_db_service_name
end
end
end
it do
count
# Gadget belongs to the default database
expect(gadget_span.service).to eq(default_db_service_name)
# Widget belongs to its own database
expect(widget_span.service).to eq(widget_db_service_name)
end
it_behaves_like 'a peer service span' do
let(:span) { gadget_span }
end
it_behaves_like 'a peer service span' do
let(:span) { widget_span }
end
end
end
end