Skip to content

Commit 5de0c78

Browse files
committed
Raise when calling connection on a sharded model without a shard
1 parent fd6de0f commit 5de0c78

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/active_record_shards/shard_selection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def shard(klass = nil)
1616
if @shard == NO_SHARD
1717
nil
1818
else
19-
@shard || self.class.default_shard
19+
@shard || self.class.default_shard || raise('You can not connect a sharded model without calling on_shard.')
2020
end
2121
end
2222
end

test/connection_switching_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,30 @@ def clear_connection_pool
286286
end
287287
end
288288

289+
if ActiveRecord::VERSION::MAJOR > 3
290+
describe "ActiveRecord::Base.connection.schema_cache.columns_hash" do
291+
before do
292+
ActiveRecord::Base.default_shard = nil
293+
end
294+
295+
it 'works for non-sharded models' do
296+
Account.connection.schema_cache.columns_hash('accounts')
297+
end
298+
299+
it 'explodes when a shard has not been specified for sharded model' do
300+
assert_raises('You can not connect a sharded model without calling on_shard.') do
301+
Ticket.connection.schema_cache.columns_hash('tickets')
302+
end
303+
end
304+
305+
it 'explodes when a shard has been specified for sharded model' do
306+
ActiveRecord::Base.on_first_shard do
307+
Ticket.connection.schema_cache.columns_hash('tickets')
308+
end
309+
end
310+
end
311+
end
312+
289313
describe "ActiveRecord::Base.table_exists?" do
290314
before do
291315
ActiveRecord::Base.default_shard = nil

0 commit comments

Comments
 (0)