Search code examples
ruby-on-railsrubycucumberdatabase-cleaner

database_cleaner without database.yml and multiple databases


I have a cucumber-tests of non-rails application.

I do not have database.yml in my project, and for some reasons dont want to have it (connection setting are stored in a bit different format).

I have several database models, defined like

class MyBase < ActiveRecord::Base
  self.abstract_class = true

  def self.some_extra_methods
  end 
end

class DatabaseA < MyBase
  self.abstract_class = true
  establish_connection ($configA)
end

class DatabaseB < MyBase
  self.abstract_class = true
  establish_connection ($configB)
end

# if i will not connect ActiveRecord::Base to something i get ActiveRecord::ConnectionNotEstablished errors 
# is there a better solution?
ActiveRecord::Base.establish_connection $configC;

class TableA < DatabaseA
  self.table_name =  :table_a
  self.primary_key = :id
end
#... and other tables

class TableB < DatabaseB
  self.table_name =  :table_b
  self.primary_key = :id
end
#... and other tables

Now to clean that databases i'm trying to use the following code. I do not have database.yml so i'm just passing models.

DatabaseCleaner[:active_record, {:model => DatabaseA} ].strategy = :truncation
DatabaseCleaner[:active_record, {:model => DatabaseB} ].strategy = :truncation

Before do
  DatabaseCleaner.start
end

After do
  DatabaseCleaner.clean
end 

But it cleans only database connected by ActiveRecord::Base, not DatabaseA & DatabaseB What is wrong?


Solution

  • I think, this gist is exactly what you need: https://gist.github.com/1109325 Code looks weird, but it works.