@@ -103,6 +103,67 @@ def test_establish_connection_using_3_levels_config_with_shards_and_replica
103103 ENV [ "RAILS_ENV" ] = previous_env
104104 end
105105
106+ class IntegerKeysBase < ActiveRecord ::Base
107+ self . abstract_class = true
108+ end
109+
110+ def test_establish_connection_using_3_levels_config_with_integer_keys
111+ previous_env , ENV [ "RAILS_ENV" ] = ENV [ "RAILS_ENV" ] , "default_env"
112+ default_shard_was = ActiveRecord ::Base . default_shard
113+ ActiveRecord ::Base . default_shard = 0
114+
115+ config = {
116+ "default_env" => {
117+ "primary" => { "adapter" => "sqlite3" , "database" => "test/db/primary.sqlite3" } ,
118+ "primary_shard_one" => { "adapter" => "sqlite3" , "database" => "test/db/primary_shard_one.sqlite3" } ,
119+ "primary_shard_one_replica" => { "adapter" => "sqlite3" , "database" => "test/db/primary_shard_one_replica.sqlite3" , "replica" => true }
120+ }
121+ }
122+
123+ @prev_configs , ActiveRecord ::Base . configurations = ActiveRecord ::Base . configurations , config
124+
125+ IntegerKeysBase . connects_to ( shards : {
126+ 0 => { writing : :primary , reading : :primary } ,
127+ 1 => { writing : :primary_shard_one , reading : :primary_shard_one_replica }
128+ } )
129+
130+ connection_description_name = "ActiveRecord::ConnectionAdapters::ConnectionHandlersShardingDbTest::IntegerKeysBase"
131+ base_pool = ActiveRecord ::Base . connection_handler . retrieve_connection_pool ( connection_description_name )
132+ default_pool = ActiveRecord ::Base . connection_handler . retrieve_connection_pool ( connection_description_name , shard : 0 )
133+
134+ assert_equal [ 0 , 1 ] , ActiveRecord ::Base . connection_handler . send ( :get_pool_manager , connection_description_name ) . shard_names
135+ assert_equal base_pool , default_pool
136+ assert_equal "test/db/primary.sqlite3" , default_pool . db_config . database
137+ assert_equal "primary" , default_pool . db_config . name
138+
139+ assert_not_nil pool = ActiveRecord ::Base . connection_handler . retrieve_connection_pool ( connection_description_name , shard : 1 )
140+ assert_equal "test/db/primary_shard_one.sqlite3" , pool . db_config . database
141+ assert_equal "primary_shard_one" , pool . db_config . name
142+
143+ assert_not_nil pool = ActiveRecord ::Base . connection_handler . retrieve_connection_pool ( connection_description_name , role : :reading , shard : 1 )
144+ assert_equal "test/db/primary_shard_one_replica.sqlite3" , pool . db_config . database
145+ assert_equal "primary_shard_one_replica" , pool . db_config . name
146+
147+ IntegerKeysBase . connected_to ( shard : 1 ) do
148+ assert_includes IntegerKeysBase . lease_connection . query_value ( "SELECT file FROM pragma_database_list;" ) , "primary_shard_one.sqlite3"
149+ end
150+
151+ IntegerKeysBase . connected_to ( role : :reading , shard : 1 ) do
152+ assert_includes IntegerKeysBase . lease_connection . query_value ( "SELECT file FROM pragma_database_list;" ) , "primary_shard_one_replica.sqlite3"
153+ end
154+
155+ assert_raises ( ActiveRecord ::ConnectionNotDefined ) do
156+ IntegerKeysBase . connected_to ( shard : 2 ) do
157+ IntegerKeysBase . lease_connection . query_value ( "SELECT file FROM pragma_database_list;" )
158+ end
159+ end
160+ ensure
161+ ActiveRecord ::Base . default_shard = default_shard_was
162+ ActiveRecord ::Base . configurations = @prev_configs
163+ ActiveRecord ::Base . establish_connection ( :arunit )
164+ ENV [ "RAILS_ENV" ] = previous_env
165+ end
166+
106167 def test_switching_connections_via_handler
107168 previous_env , ENV [ "RAILS_ENV" ] = ENV [ "RAILS_ENV" ] , "default_env"
108169
0 commit comments