Repository: rails/rails
File: activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb (Ruby)

Code before:
            connection = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new(configuration)
            connection.exec_query("SELECT 1")
          end
          assert_not_nil connection
          assert_equal connection.pool, error.connection_pool
        end
      end

      def test_reconnect_after_bad_connection_on_check_version
        db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
        connection = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new(db_config.configuration_hash)
        connection.connect!

        # mimic a connection that hasn't checked and cached the server version yet i.e. without a raw_connection
        connection.pool.instance_variable_set(:@server_version, nil)
        connection.raw_connection.stub(:server_version, 0) do
          error = assert_raises ActiveRecord::ConnectionFailed do
            connection.reconnect!
          end
          assert_equal "Could not determine PostgreSQL version", error.message
        end

        # can reconnect after a bad connection
        assert_nothing_raised do
          connection.reconnect!
        end
      end

      def test_database_exists_returns_false_when_the_database_does_not_exist
        config = { database: "non_extant_database", adapter: "postgresql" }
        assert_not ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.database_exists?(config),
          "expected database #{config[:database]} to not exist"
      end

      def test_database_exists_returns_true_when_the_database_exists
        db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
        assert ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.database_exists?(db_config.configuration_hash),
          "expected database #{db_config.database} to exist"
      end

      def test_primary_key
        with_example_table do
          assert_equal "id", @connection.primary_key("ex")
        end
      end

      def test_primary_key_works_tables_containing_capital_letters
        assert_equal "id", @connection.primary_key("CamelCase")
      end

      def test_non_standard_primary_key

Review comment (bug):
Without the patch, if I update the assertion above to expect [this](https://github.com/rails/rails/blob/049d1af38d19968fc8e8648989b0fb8aae2caa71/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L665-L669) `RuntimeError` and the respective message to pass, this assertion fails with:

![Screenshot 2025-03-08 at 12 08 10 am](https://github.com/user-attachments/assets/7b08baf8-5019-42f0-8abf-3ec0c0045e9b)

i.e. it cannot successfully reconnect and the same error is raised due to 0 being cached.

Code after:
            connection = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new(configuration)
            connection.exec_query("SELECT 1")
          end
          assert_not_nil connection
          assert_equal connection.pool, error.connection_pool
        end
      end

      def test_reconnect_after_bad_connection_on_check_version
        db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
        connection = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new(db_config.configuration_hash.merge(connection_retries: 0))
        connection.connect!

        # mimic a connection that hasn't checked and cached the server version yet i.e. without a raw_connection
        connection.pool.instance_variable_set(:@server_version, nil)
        connection.raw_connection.stub(:server_version, 0) do
          error = assert_raises ActiveRecord::ConnectionFailed do
            connection.reconnect!
          end
          assert_equal "Could not determine PostgreSQL version", error.message
        end

        # can reconnect after a bad connection
        assert_nothing_raised do
          connection.reconnect!
        end
      end

      def test_database_exists_returns_false_when_the_database_does_not_exist
        config = { database: "non_extant_database", adapter: "postgresql" }
        assert_not ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.database_exists?(config),
          "expected database #{config[:database]} to not exist"
      end

      def test_database_exists_returns_true_when_the_database_exists
        db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
        assert ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.database_exists?(db_config.configuration_hash),
          "expected database #{db_config.database} to exist"
      end

      def test_primary_key
        with_example_table do
          assert_equal "id", @connection.primary_key("ex")
        end
      end

      def test_primary_key_works_tables_containing_capital_letters
        assert_equal "id", @connection.primary_key("CamelCase")
      end

      def test_non_standard_primary_key