Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

[psql] not cleaning with same database, different schemas

Đang mở
#18 4 bình luận 2 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
postgresql, ruby
Lĩnh vực
databases

Hướng nghiên cứu

Bắt đầu từ lookup_from_connection_pool, được gọi từ connection_class, và so sánh nó với hai cấu hình ActiveRecord được hiển thị trong spec_helper. Tái hiện việc dọn dẹp trên cùng một cơ sở dữ liệu với các giá trị schema_search_path khác nhau, sau đó xác minh rằng mỗi cleaner chọn kết nối tương ứng và hành vi này được kiểm thử bằng một regression test.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Hi. I've been having trouble with making database cleaner work when trying to delete data from different psql schemas, but in the same database.

database.yml:

test: &test
  adapter: postgresql
  database: test_project
  host: localhost
  port: 6432
  user: postgres

test_non_public:
  <<: *test
  schema_search_path: non_public

What I did in spec_helper:

config.before(:suite) do
  DatabaseCleaner.clean_with :deletion
  DatabaseCleaner[:active_record, connection: :test_non_public].clean_with :deletion
end

From what I understand it happens because of this code:

      def lookup_from_connection_pool
        if ::ActiveRecord::Base.respond_to?(:descendants)
          database_name = connection_hash["database"] || connection_hash[:database]
          models        = ::ActiveRecord::Base.descendants
          models.detect { |m| m.connection_pool.spec.config[:database] == database_name }
        end
      end

This method is invoked from connection_class, and because both connection "test" and "test_non_public" are configured to connect to the same database it ends up returning a model with an incorrect connection. This could be solved (I think) by adding one more condition, so that besides checking for the database_name it also checks for the schema_search_path.

While writing this issue I ended up editing the code as follows and it worked:

      def lookup_from_connection_pool
        if ::ActiveRecord::Base.respond_to?(:descendants)
          database_name      = connection_hash["database"] || connection_hash[:database]
          schema_search_path = connection_hash["schema_search_path"] || connection_hash[:schema_search_path]
          models             = ::ActiveRecord::Base.descendants
          models.detect do |m|
            m.connection_pool.spec.config[:database] == database_name &&
            m.connection_pool.spec.config[:schema_search_path] == schema_search_path
          end
        end
      end

Does it look like a good fix? If not how should I approach this problem so that I can solve this issue?

Thanks

Ngôn ngữ chính
Ruby
Star
76
Fork
76
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của DatabaseCleaner/database_cleaner-active_record

Tất cả issue của DatabaseCleaner/database_cleaner-active_record

Issue tương tự

Thêm issue về Ruby

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.