Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[psql] not cleaning with same database, different schemas

未关闭
#18 4 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
postgresql, ruby
领域
databases

调研方向

从 lookup_from_connection_pool 开始,它由 connection_class 调用,并将其与 spec_helper 中显示的两个 ActiveRecord 配置进行比较。使用不同的 schema_search_path 值在同一个数据库上重现清理操作,然后验证每个 cleaner 是否选择匹配的连接,并确认该行为已由回归测试覆盖。

由索引模型根据 Issue 内容生成。

描述

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

主要语言
Ruby
星标
76
派生
76
PR 合并指标
30 天内没有已合并 PR

环境准备

我们还没有检查这个项目的环境配置文件。先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

DatabaseCleaner/database_cleaner-active_record 的其他 Issue

查看 DatabaseCleaner/database_cleaner-active_record 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。