diff --git a/lib/active_record_shards/connection_switcher-5-0.rb b/lib/active_record_shards/connection_switcher-5-0.rb index ab328467..f9dc56d5 100644 --- a/lib/active_record_shards/connection_switcher-5-0.rb +++ b/lib/active_record_shards/connection_switcher-5-0.rb @@ -1,6 +1,7 @@ module ActiveRecordShards module ConnectionSwitcher def connection_specification_name + byebug name = current_shard_selection.resolve_connection_name(sharded: is_sharded?, configurations: configurations) unless configurations[name] || name == "primary" diff --git a/lib/active_record_shards/connection_switcher-5-1.rb b/lib/active_record_shards/connection_switcher-5-1.rb index b00107a0..e629394c 100644 --- a/lib/active_record_shards/connection_switcher-5-1.rb +++ b/lib/active_record_shards/connection_switcher-5-1.rb @@ -2,6 +2,7 @@ module ActiveRecordShards module ConnectionSwitcher def connection_specification_name name = current_shard_selection.resolve_connection_name(sharded: is_sharded?, configurations: configurations) + puts "ARS::ConnectionSwitcher resolved #{name} for #{is_sharded?} #{self}" unless configurations[name] || name == "primary" raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in database.yml" diff --git a/lib/active_record_shards/connection_switcher.rb b/lib/active_record_shards/connection_switcher.rb index ad58a09a..6201769b 100644 --- a/lib/active_record_shards/connection_switcher.rb +++ b/lib/active_record_shards/connection_switcher.rb @@ -152,6 +152,7 @@ def shard_names private def switch_connection(options) + puts "Switching to #{options}" if options.any? if options.key?(:shard) unless config = configurations[shard_env] diff --git a/test/connection_switching_test.rb b/test/connection_switching_test.rb index 8a66f9e3..ff785555 100644 --- a/test/connection_switching_test.rb +++ b/test/connection_switching_test.rb @@ -19,10 +19,10 @@ def clear_connection_pool describe "shard switching" do it "only switch connection on sharded models" do - assert_using_database('ars_test', Ticket) + assert_using_database('ars_test_shard1_slave', Ticket) assert_using_database('ars_test', Account) - ActiveRecord::Base.on_shard(0) do + Ticket.on_shard(0) do assert_using_database('ars_test_shard0', Ticket) assert_using_database('ars_test', Account) end diff --git a/test/helper.rb b/test/helper.rb index f6ba10e4..44bbd08a 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -102,10 +102,26 @@ def self.with_phenix %w[test_shard_0 test_shard_0_slave test_shard_1 test_shard_1_slave].include?(name) end end + Phenix.load_database_config + require 'models' + # Ticket.establish_connection(:test_shard_0) + # ActiveRecord::Base.establish_connection(:test) + ActiveRecord::Base.send(:sharded=, false) + # require 'models' + # byebug + # Account.primary_key + # Ticket.on_first_shard { Ticket.primary_key } Phenix.rise!(with_schema: true) + puts "*" * 80 + puts "Done with unsharded schema" + puts "*" * 80 + + ActiveRecord::Base.send(:sharded=, true) + ActiveRecord::Base.establish_connection(:test_shard_0) # Populate sharded databases Phenix.configure do |config| + config.active_record_connection_class = Ticket config.schema_path = File.join(Dir.pwd, 'test', 'sharded_schema.rb') config.skip_database = lambda do |name, _| %w[test test_slave test2 test2_slave].include?(name)