[psql] not cleaning with same database, different schemas
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
Research direction
Start at lookup_from_connection_pool, invoked from connection_class, and compare it with the two ActiveRecord configurations shown in spec_helper. Reproduce cleaning against the same database with different schema_search_path values, then verify that each cleaner selects the matching connection and that the behavior is covered by a regression test.
Written by the indexing model from the issue text.
Description
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
- Dominant language
- Ruby
- Stars
- 76
- Forks
- 76
- PR merge metrics
- No merged PRs in 30d
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from DatabaseCleaner/database_cleaner-active_record
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
DatabaseCleaner/database_cleaner-active_record#128 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
DatabaseCleaner/database_cleaner-active_record#127 · 3 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
All issues in DatabaseCleaner/database_cleaner-active_record
Similar issues
-
security
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
CircuitVerse/CircuitVerse#7967 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
simp/pupmod-simp-stunnel#173 ·
-
Polish QDrantOpen
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
o19s/quepid#1825 · 1 comment ·
Maintainers usually reply within 1 day