Skip to content

Commit

Permalink
DEBUG
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobat committed Oct 12, 2017
1 parent 5cab63a commit 4915dc1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/active_record_shards/connection_switcher-5-0.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
1 change: 1 addition & 0 deletions lib/active_record_shards/connection_switcher-5-1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions lib/active_record_shards/connection_switcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions test/connection_switching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4915dc1

Please sign in to comment.