From 5de0c78fa887e3f97ae397e16962354e68b25e97 Mon Sep 17 00:00:00 2001 From: Pierre Schambacher Date: Fri, 6 Oct 2017 15:23:19 -0700 Subject: [PATCH] Raise when calling connection on a sharded model without a shard --- lib/active_record_shards/shard_selection.rb | 2 +- test/connection_switching_test.rb | 24 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/active_record_shards/shard_selection.rb b/lib/active_record_shards/shard_selection.rb index b664ec32..cc1c902c 100644 --- a/lib/active_record_shards/shard_selection.rb +++ b/lib/active_record_shards/shard_selection.rb @@ -16,7 +16,7 @@ def shard(klass = nil) if @shard == NO_SHARD nil else - @shard || self.class.default_shard + @shard || self.class.default_shard || raise('You can not connect a sharded model without calling on_shard.') end end end diff --git a/test/connection_switching_test.rb b/test/connection_switching_test.rb index 8a66f9e3..b341298f 100644 --- a/test/connection_switching_test.rb +++ b/test/connection_switching_test.rb @@ -286,6 +286,30 @@ def clear_connection_pool end end + if ActiveRecord::VERSION::MAJOR > 3 + describe "ActiveRecord::Base.connection.schema_cache.columns_hash" do + before do + ActiveRecord::Base.default_shard = nil + end + + it 'works for non-sharded models' do + Account.connection.schema_cache.columns_hash('accounts') + end + + it 'explodes when a shard has not been specified for sharded model' do + assert_raises('You can not connect a sharded model without calling on_shard.') do + Ticket.connection.schema_cache.columns_hash('tickets') + end + end + + it 'explodes when a shard has been specified for sharded model' do + ActiveRecord::Base.on_first_shard do + Ticket.connection.schema_cache.columns_hash('tickets') + end + end + end + end + describe "ActiveRecord::Base.table_exists?" do before do ActiveRecord::Base.default_shard = nil