DatabaseCleaner/database_cleaner-active_record

Rails 6 Multiple Databases Clarification

scottrobertson opened this issue ยท 4 comments

Hey

We have been using the multiverse gem for a long time to manage multiple databases, but with an upgrade to Rails 6, we are moving to use their built-in setup.

Previously, we had the following:

DatabaseCleaner[:active_record].strategy = :deletion
DatabaseCleaner[:active_record, { connection: :provider_data_test }].strategy = :deletion

With the following in the Model's

establish_connection :"provider_data_#{Rails.env}"

The new way of doing it is by having the following in database.yml:

test:
  primary:
    <<: *default
  provider_data:
    <<: *provider_data_default

And this in the models:

connects_to database: { writing: :provider_data, reading: :provider_data }

How should Database cleaner be setup for this? It's clearing the primary db, but it's not clearing the secondary dbs. I have tried this for example:

DatabaseCleaner[:active_record, { connection: :provider_data }].strategy = :deletion

Will ask this in the AR repo actually.

@scottrobertson Thanks for opening this issue! I went ahead and transferred it to the right repository.

I believe you can do it like this:

DatabaseCleaner[:active_record, { connection: :primary }].strategy = :deletion
DatabaseCleaner[:active_record, { connection: :provider_data }].strategy = :deletion

More here: https://github.com/DatabaseCleaner/database_cleaner/blob/c542fee62a1c97766ee979920a398eabfaeb3a77/spec/database_cleaner/cleaners_spec.rb#L36-L43

If that doesn't work, let us know!

@etagwerker thank you ๐Ÿ‘

Yeah that worked :) figured it out between closing the old issue, and when i was supposed to open the new one ha.

@scottrobertson Great! I realize that the documentation could be better (https://github.com/DatabaseCleaner/database_cleaner-active_record#adapter-configuration-options), so if you think of an improvement for our README, I'd be happy to review it ๐Ÿ˜„