Repository: rails/rails
File: activerecord/test/cases/connection_adapters/connection_handlers_sharding_db_test.rb (Ruby)

Code before:
          ActiveRecord::Base.establish_connection(:arunit)
          ENV["RAILS_ENV"] = previous_env
        end

        class IntegerKeysBase < ActiveRecord::Base
          self.abstract_class = true
        end

        def test_establish_connection_using_3_levels_config_with_integer_keys
          previous_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "default_env"

          config = {
            "default_env" => {
              "primary" => { "adapter" => "sqlite3", "database" => "test/db/primary.sqlite3" },
              "primary_shard_one" => { "adapter" => "sqlite3", "database" => "test/db/primary_shard_one.sqlite3" },
              "primary_shard_one_replica" => { "adapter" => "sqlite3", "database" => "test/db/primary_shard_one_replica.sqlite3", "replica" => true }
            }
          }

          @prev_configs, ActiveRecord::Base.configurations = ActiveRecord::Base.configurations, config

          IntegerKeysBase.connects_to(shards: {
            default: { writing: :primary, reading: :primary },
            1 => { writing: :primary_shard_one, reading: :primary_shard_one_replica }
          })

          connection_description_name = "ActiveRecord::ConnectionAdapters::ConnectionHandlersShardingDbTest::IntegerKeysBase"
          base_pool = ActiveRecord::Base.connection_handler.retrieve_connection_pool(connection_description_name)
          default_pool = ActiveRecord::Base.connection_handler.retrieve_connection_pool(connection_description_name, shard: :default)

          assert_equal [:default, 1], ActiveRecord::Base.connection_handler.send(:get_pool_manager, connection_description_name).shard_names
          assert_equal base_pool, default_pool
          assert_equal "test/db/primary.sqlite3", default_pool.db_config.database
          assert_equal "primary", default_pool.db_config.name

          assert_not_nil pool = ActiveRecord::Base.connection_handler.retrieve_connection_pool(connection_description_name, shard: 1)
          assert_equal "test/db/primary_shard_one.sqlite3", pool.db_config.database
          assert_equal "primary_shard_one", pool.db_config.name

          assert_not_nil pool = ActiveRecord::Base.connection_handler.retrieve_connection_pool(connection_description_name, role: :reading, shard: 1)
          assert_equal "test/db/primary_shard_one_replica.sqlite3", pool.db_config.database
          assert_equal "primary_shard_one_replica", pool.db_config.name

          IntegerKeysBase.connected_to(shard: 1) do
            assert_includes IntegerKeysBase.lease_connection.query_value("SELECT file FROM pragma_database_list;"), "primary_shard_one.sqlite3"
          end

          IntegerKeysBase.connected_to(role: :reading, shard: 1) do
            assert_includes IntegerKeysBase.lease_connection.query_value("SELECT file FROM pragma_database_list;"), "primary_shard_one_replica.sqlite3"
          end


Review comment (suggestion):
I don't have a problem with this fundamentally however...this is going to sound picky and but seeing it written out it's kind of ugly, especially mixing symbols and integers like `default:` with `1 =>`.

Here's my thought process:

So should `0` be default also so we're not mixing symbols and integers? Or should we not accept this apps generate a key name like `shard_1` and then in the connects to it's `connected_to(shard: "shard_#{account.shard}")`? My issue is how with this change we're encouraging the mixing of symbols and integers and making the API potentially very ugly.

Code after:
          ActiveRecord::Base.establish_connection(:arunit)
          ENV["RAILS_ENV"] = previous_env
        end

        class IntegerKeysBase < ActiveRecord::Base
          self.abstract_class = true
        end

        def test_establish_connection_using_3_levels_config_with_integer_keys
          previous_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "default_env"
          default_shard_was = ActiveRecord::Base.default_shard
          ActiveRecord::Base.default_shard = 0

          config = {
            "default_env" => {
              "primary" => { "adapter" => "sqlite3", "database" => "test/db/primary.sqlite3" },
              "primary_shard_one" => { "adapter" => "sqlite3", "database" => "test/db/primary_shard_one.sqlite3" },
              "primary_shard_one_replica" => { "adapter" => "sqlite3", "database" => "test/db/primary_shard_one_replica.sqlite3", "replica" => true }
            }
          }

          @prev_configs, ActiveRecord::Base.configurations = ActiveRecord::Base.configurations, config

          IntegerKeysBase.connects_to(shards: {
            0 => { writing: :primary, reading: :primary },
            1 => { writing: :primary_shard_one, reading: :primary_shard_one_replica }
          })

          connection_description_name = "ActiveRecord::ConnectionAdapters::ConnectionHandlersShardingDbTest::IntegerKeysBase"
          base_pool = ActiveRecord::Base.connection_handler.retrieve_connection_pool(connection_description_name)
          default_pool = ActiveRecord::Base.connection_handler.retrieve_connection_pool(connection_description_name, shard: 0)

          assert_equal [0, 1], ActiveRecord::Base.connection_handler.send(:get_pool_manager, connection_description_name).shard_names
          assert_equal base_pool, default_pool
          assert_equal "test/db/primary.sqlite3", default_pool.db_config.database
          assert_equal "primary", default_pool.db_config.name

          assert_not_nil pool = ActiveRecord::Base.connection_handler.retrieve_connection_pool(connection_description_name, shard: 1)
          assert_equal "test/db/primary_shard_one.sqlite3", pool.db_config.database
          assert_equal "primary_shard_one", pool.db_config.name

          assert_not_nil pool = ActiveRecord::Base.connection_handler.retrieve_connection_pool(connection_description_name, role: :reading, shard: 1)
          assert_equal "test/db/primary_shard_one_replica.sqlite3", pool.db_config.database
          assert_equal "primary_shard_one_replica", pool.db_config.name

          IntegerKeysBase.connected_to(shard: 1) do
            assert_includes IntegerKeysBase.lease_connection.query_value("SELECT file FROM pragma_database_list;"), "primary_shard_one.sqlite3"
          end

          IntegerKeysBase.connected_to(role: :reading, shard: 1) do
            assert_includes IntegerKeysBase.lease_connection.query_value("SELECT file FROM pragma_database_list;"), "primary_shard_one_replica.sqlite3"