Skip to content

Commit

Permalink
Add a few warnings to the connection switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
bquorning committed Oct 25, 2017
1 parent e32f84d commit 2a1721d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 32 deletions.
5 changes: 5 additions & 0 deletions lib/active_record_shards/configuration_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ def explode(conf)

conf.to_a.each do |env_name, env_config|
next unless shards = env_config.delete('shards')

unless shards.keys.all? { |shard_name| shard_name.is_a?(Integer) }
raise "All shard names must be integers: #{shards.keys.inspect}."
end

env_config['shard_names'] = shards.keys
shards.each do |shard_name, shard_conf|
expand_child!(env_config, shard_conf)
Expand Down
10 changes: 8 additions & 2 deletions lib/active_record_shards/connection_switcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def connection_specification_name
name = current_shard_selection.resolve_connection_name(sharded: is_sharded?, configurations: configurations)

unless configurations[name] || name == "primary"
raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in database.yml"
raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.inspect})"
end

name
Expand All @@ -144,7 +144,10 @@ def current_shard_id

def shard_names
unless config = configurations[shard_env]
raise "Did not find #{shard_env} in configurations, did you forget to add it to your database.yml ? (configurations: #{configurations.inspect})"
raise "Did not find #{shard_env} in configurations, did you forget to add it to your database config? (configurations: #{configurations.inspect})"
end
unless config.fetch(SHARD_NAMES_CONFIG_KEY, []).all? { |shard_name| shard_name.is_a?(Integer) }
raise "All shard names must be integers: #{config[SHARD_NAMES_CONFIG_KEY].inspect}."
end
config[SHARD_NAMES_CONFIG_KEY] || []
end
Expand All @@ -158,6 +161,9 @@ def switch_connection(options)
end

if options.key?(:shard)
unless config = configurations[shard_env]
raise "Did not find #{shard_env} in configurations, did you forget to add it to your database config? (configurations: #{configurations.inspect})"
end
current_shard_selection.shard = options[:shard]
end

Expand Down
34 changes: 17 additions & 17 deletions test/configuration_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,78 +19,78 @@
"username" => "root",
"password" => nil,
"host" => "main_slave_host",
"shard_names" => ["a", "b"].to_set
"shard_names" => [500, 501].to_set
}, @conf)
end
end

describe "shard a" do
describe "master" do
before { @conf = @exploded_conf["test_shard_a"] }
before { @conf = @exploded_conf["test_shard_500"] }
it "be exploded" do
@conf["shard_names"] = @conf["shard_names"].to_set
assert_equal({
"adapter" => "mysql",
"encoding" => "utf8",
"database" => "ars_test_shard_a",
"database" => "ars_test_shard_500",
"port" => 123,
"username" => "root",
"password" => nil,
"host" => "shard_a_host",
"shard_names" => ["a", "b"].to_set
"host" => "shard_500_host",
"shard_names" => [500, 501].to_set
}, @conf)
end
end

describe "slave" do
before { @conf = @exploded_conf["test_shard_a_slave"] }
before { @conf = @exploded_conf["test_shard_500_slave"] }
it "be exploded" do
@conf["shard_names"] = @conf["shard_names"].to_set
assert_equal({
"adapter" => "mysql",
"encoding" => "utf8",
"database" => "ars_test_shard_a",
"database" => "ars_test_shard_500",
"port" => 123,
"username" => "root",
"password" => nil,
"host" => "shard_a_slave_host",
"shard_names" => ["a", "b"].to_set
"host" => "shard_500_slave_host",
"shard_names" => [500, 501].to_set
}, @conf)
end
end
end

describe "shard b" do
describe "master" do
before { @conf = @exploded_conf["test_shard_b"] }
before { @conf = @exploded_conf["test_shard_501"] }
it "be exploded" do
@conf["shard_names"] = @conf["shard_names"].to_set
assert_equal({
"adapter" => "mysql",
"encoding" => "utf8",
"database" => "ars_test_shard_b",
"database" => "ars_test_shard_501",
"port" => 123,
"username" => "root",
"password" => nil,
"host" => "shard_b_host",
"shard_names" => ["a", "b"].to_set
"host" => "shard_501_host",
"shard_names" => [500, 501].to_set
}, @conf)
end
end

describe "slave" do
before { @conf = @exploded_conf["test_shard_b_slave"] }
before { @conf = @exploded_conf["test_shard_501_slave"] }
it "be exploded" do
@conf["shard_names"] = @conf["shard_names"].to_set
assert_equal({
"adapter" => "mysql",
"encoding" => "utf8",
"database" => "ars_test_shard_b_slave",
"database" => "ars_test_shard_501_slave",
"port" => 123,
"username" => "root",
"password" => nil,
"host" => "shard_b_host",
"shard_names" => ["a", "b"].to_set
"host" => "shard_501_host",
"shard_names" => [500, 501].to_set
}, @conf)
end
end
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 @@ -75,8 +75,8 @@
assert_includes(database_names, @shard_1_master.select_value("SELECT DATABASE()"))

assert_equal(2, database_shards.size)
assert_includes(database_shards, "0")
assert_includes(database_shards, "1")
assert_includes(database_shards, 0)
assert_includes(database_shards, 1)
end

it "execute the block unsharded" do
Expand Down
2 changes: 1 addition & 1 deletion test/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mysql: &MYSQL
test:
<<: *MYSQL
database: ars_test
shard_names: ['0', '1']
shard_names: [0, 1]

test_slave:
<<: *MYSQL
Expand Down
16 changes: 8 additions & 8 deletions test/database_parse_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ test:
slave:
host: main_slave_host
shards:
a:
database: ars_test_shard_a
host: shard_a_host
500:
database: ars_test_shard_500
host: shard_500_host
slave:
host: shard_a_slave_host
b:
database: ars_test_shard_b
host: shard_b_host
host: shard_500_slave_host
501:
database: ars_test_shard_501
host: shard_501_host
slave:
database: ars_test_shard_b_slave
database: ars_test_shard_501_slave
4 changes: 2 additions & 2 deletions test/database_tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test:
slave:
database: ars_tasks_test_slave
shards:
a:
0:
database: ars_tasks_test_shard_a
b:
1:
database: ars_tasks_test_shard_b

0 comments on commit 2a1721d

Please sign in to comment.