Skip to content

Commit 0f952de

Browse files
committed
Improve error messages from UntenantedConnectionPool
1 parent da22dc9 commit 0f952de

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

lib/active_record/tenanted/tenant.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def connection_pool # :nodoc:
7878

7979
pool
8080
else
81-
Tenanted::UntenantedConnectionPool.new(tenanted_root_config)
81+
Tenanted::UntenantedConnectionPool.new(tenanted_root_config, self)
8282
end
8383
end
8484

lib/active_record/tenanted/untenanted_connection_pool.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ module Tenanted
1212
class UntenantedConnectionPool < ActiveRecord::ConnectionAdapters::NullPool # :nodoc:
1313
attr_reader :db_config
1414

15-
def initialize(db_config)
15+
def initialize(db_config, model)
1616
super()
1717

1818
@db_config = db_config
19+
@model = model
1920
end
2021

2122
def schema_cache
@@ -25,19 +26,19 @@ def schema_cache
2526
end
2627

2728
def lease_connection(...)
28-
raise Tenanted::NoTenantError, "Cannot connect to a tenanted database while untenanted."
29+
raise Tenanted::NoTenantError, "Cannot connect to a tenanted database while untenanted (#{@model})."
2930
end
3031

3132
def checkout(...)
32-
raise Tenanted::NoTenantError, "Cannot connect to a tenanted database while untenanted."
33+
raise Tenanted::NoTenantError, "Cannot connect to a tenanted database while untenanted (#{@model})."
3334
end
3435

3536
def with_connection(...)
36-
raise Tenanted::NoTenantError, "Cannot connect to a tenanted database while untenanted."
37+
raise Tenanted::NoTenantError, "Cannot connect to a tenanted database while untenanted (#{@model})."
3738
end
3839

3940
def new_connection(...)
40-
raise Tenanted::NoTenantError, "Cannot connect to a tenanted database while untenanted."
41+
raise Tenanted::NoTenantError, "Cannot connect to a tenanted database while untenanted (#{@model})."
4142
end
4243
end
4344
end

test/unit/untenanted_connection_pool_test.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
require "test_helper"
44

55
describe ActiveRecord::Tenanted::UntenantedConnectionPool do
6-
let(:config) { Object.new }
7-
let(:subject) { ActiveRecord::Tenanted::UntenantedConnectionPool.new(config) }
6+
with_scenario(:primary_db, :primary_record) do
7+
let(:config) { Object.new }
8+
let(:subject) { ActiveRecord::Tenanted::UntenantedConnectionPool.new(config, User) }
89

9-
[ :lease_connection,
10-
:checkout,
11-
:with_connection,
12-
:new_connection
13-
].each do |method|
14-
test "#{method} raises NoTenantError" do
15-
assert_raises(ActiveRecord::Tenanted::NoTenantError) do
16-
subject.send(method)
10+
[ :lease_connection,
11+
:checkout,
12+
:with_connection,
13+
:new_connection
14+
].each do |method|
15+
test "#{method} raises NoTenantError" do
16+
e = assert_raises(ActiveRecord::Tenanted::NoTenantError) do
17+
subject.send(method)
18+
end
19+
assert_equal("Cannot connect to a tenanted database while untenanted (User).", e.message)
1720
end
1821
end
1922
end

0 commit comments

Comments
 (0)