[psql] not cleaning with same database, different schemas
还没有人认领这个 Issue。
评估
调研方向
从 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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
DatabaseCleaner/database_cleaner-active_record 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 62/100
-
难度 5/5 一周以上 新手友好度 30/100
DatabaseCleaner/database_cleaner-active_record#128 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 35/100
DatabaseCleaner/database_cleaner-active_record#127 · 3 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 45/100
-
难度 5/5 一周以上 新手友好度 25/100
查看 DatabaseCleaner/database_cleaner-active_record 的全部 Issue
相似的 Issue
-
P2 testing
难度 1/5 1 小时以内 新手友好度 90/100
维护者通常 1 天内回复
-
performance
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
openSUSE/open-build-service#20338 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
バグ
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复