ActiveRecord Truncation does not work when using multiple schemas
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
Research direction
Start with lib/database_cleaner/active_record/truncation.rb, especially the table-list query at lines 191–196, and reproduce it with the public, earth, and mars schemas described in the issue. Confirm that truncation cleans tenant tables while still excluding schema_migrations, ar_internal_metadata, pg_catalog, and information_schema.
Written by the indexing model from the issue text.
Description
Hello - thanks for putting together database_cleaner! I've been using the gem for a while and I know maintenance takes effort, so appreciate those who contribute to this project.
I'm using the following setup -
- Rails 5.1.0
- ActiveRecord 3.0.0
- Postgres 9.6
Like many multi-tenant applications, my app utilizes postgres schemas which store parrallel, independent copies of my database tables.
So if I have two tenants (earth, mars) and two tables (users, companies) I would have the following tables in my DB -
schema_migrations
ar_internal_metadata
public.users
public.companies
earth.users
earth.companies
mars.users
mars.companies
When using DatabaseCleaner.strategy = :truncation, it looks for a list of tables in my DB to truncate (see here).
When I run that query against my local test database -
SELECT schemaname || '.' || tablename
FROM pg_tables
WHERE
tablename !~ '_prt_' AND
tablename <> 'schema_migrations' AND tablename <> 'ar_internal_metadata'
AND schemaname = ANY (current_schemas(false))
;
?column?
-------------------------------------------
public.users
public.companies
(2 rows)
As you can see, it only returns the public schemas, because of the schemaname = criteria. This effectively doesn't wipe my non-public schema tables, causing run-over between tests.
I understand that there are options to use a transaction instead, or even the only: [..] parameter to list all my tables. But it seems like this should work as is without having to use one of those as a workaround (and if my DB is quite large, a whitelist can get messy and inefficient across thousands of tests).
Would it make sense to update the query logic to something like
SELECT schemaname || '.' || tablename
FROM pg_tables
WHERE
tablename !~ '_prt_' AND
tablename <> 'schema_migrations' AND tablename <> 'ar_internal_metadata'
AND schemaname NOT IN ('pg_catalog', 'information_schema')
;
Thanks!
Related to: https://github.com/DatabaseCleaner/database_cleaner/issues/225
- 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
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
TheOdinProject/curriculum#31444 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
security
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
ds-drift
Difficulty 1/5 Under an hour Newbie friendliness 88/100
we-promise/sure#3934 ·
Maintainers usually reply within 1 day