Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database cleaner with multiple DBs #3

Open
ijunaid8989 opened this issue Jan 8, 2017 · 3 comments
Open

Database cleaner with multiple DBs #3

ijunaid8989 opened this issue Jan 8, 2017 · 3 comments

Comments

@ijunaid8989
Copy link

I have been using DB cleaner with my only db but now I have 2 of them, and I am getting this error in testing mode as

RuntimeError:
  As you have more than one active sequel database you have to specify the one to use manually!

At first my whole sequel models were using only one db but now i have connected my one model with a separate Db as

class CameraActivity < Sequel::Model(Sequel.connect(ENV['SNAPSHOT_DATABASE_URL'])[:camera_activities])

and in spec helper I have these configurations

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] = 'test'
ENV["DATABASE_URL"] = "postgres://localhost/evercam_tst"
ENV["RACK_ENV"]  = ENV["RAILS_ENV"]
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'webmock/rspec'
require 'factory_girl'
require 'rack_session_access/capybara'
require "simplecov"
SimpleCov.start

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

require 'database_cleaner'
require 'capybara/poltergeist'
require 'simplecov'

SimpleCov.start 'rails'

#Capybara.server_port = 3001
#Capybara.app_host = "http://local.evercam.io:3001"
Capybara.javascript_driver = :poltergeist

RSpec.configure do |config|
  config.infer_spec_type_from_file_location!

  config.filter_run :focus => true
  config.run_all_when_everything_filtered = true

  # Configure factory girl stuff.
  config.include FactoryGirl::Syntax::Methods

  # Configure database cleaner.
  config.before(:suite) do
    WebMock.allow_net_connect!
    DatabaseCleaner[:sequel].strategy = :truncation, {except: %w[spatial_ref_sys]}
    DatabaseCleaner[:sequel].clean_with(:truncation, except: %w[spatial_ref_sys])
  end

  config.before(:each) do
    DatabaseCleaner[:sequel].start
  end

  config.after(:each) do
    DatabaseCleaner[:sequel].clean
  end
end

# Load up factories.
FactoryGirl.find_definitions
```


my other DB is `ENV['SNAPSHOT_DATABASE_URL']` But for testing purposes both DB URLs are directed to one database. How is that possible to tell DB Cleaner to handle that case?
@baschtl
Copy link

baschtl commented Feb 17, 2017

I just had the same issue. I solved it by setting the database instance manually:

  db = Sequel.connect(ENV.fetch('DATABASE_URL'))
  DatabaseCleaner[:sequel].db = db

@rastating
Copy link

I just had the same issue. I solved it by setting the database instance manually:

db = Sequel.connect(ENV.fetch('DATABASE_URL'))
DatabaseCleaner[:sequel].db = db

Thanks, this just resolved the same issue for me 👍

@okuramasafumi
Copy link

For ActiveRecord users who have models with connections to different DB and config is located other than config/database.yml, the config below worked perfectly.

DatabaseCleaner[:active_record, {model: ModelWithDifferentConnection}].clean_with(:truncation)

BTW, this is documented in README. Cannot this issue be closed?

@botandrose botandrose transferred this issue from DatabaseCleaner/database_cleaner Feb 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants