3

I'd like to use zeus (0.13.3) to preload my rails environment for an rails (3.2.11) app on ruby 2.0.0 and use database_cleaner (0.9.1) to clean the databases. This is working fine if as long as I use only one mysql database. In the app we have to use two different mysql databases. Edit: What needs to be mentioned is, that I use the shared connection hack, which is described here.

Running my specs without using zeus is working like expected. But if I use it, every test is failing in the moment when a record is created with the error:

undefined method `query_options' for nil:NilClass

How can this be solved?

The setup is like that:

config/database.yml

test:
  database: app_test
  ...

test_second_db:
  database: other_test
  ...

In an abstract base class for all the models using the second connection we do

models/second_db/base.rb

module SecondDB

  class Base < ActiveRecord::Base

    establish_connection "#{Rails.env}_second_db"
    self.abstract_class = true

  end

end

In my spec_helper.rb file I setup the database_cleaner:

spec/spec_helper.rb

require 'database_cleaner'

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner[:active_record,{:connection => :test_second_db}].strategy = :transaction
  end

  config.before(:each) do
    DatabaseCleaner.start
    DatabaseCleaner[:active_record,{:connection => :test_second_db}].start
  end

  config.after(:each) do
    DatabaseCleaner.clean
    DatabaseCleaner[:active_record,{:connection => :test_second_db}].clean
  end

end

edit:

spec/support/shared_connection.rb

class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

spec/support/shared_connection.rb

Community
  • 1
  • 1
Lars Schirrmeister
  • 2,215
  • 1
  • 22
  • 23
  • Can you run the test with `--trace`, so I can see an extended backtrace? This might require digging into some code. – Benjamin Manns Apr 23 '13 at 18:00
  • I was able to get this to work by modifying the @@shared_connection || retrieve_connection line to use only one connection in the pool: ```@@shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection }``` – RubyRedGrapefruit Jan 04 '14 at 20:27

1 Answers1

2

Not sure if this will fix your issue as I am not able to test it but you may want to try adding this to your spec_helper file.

ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
        def current_connection_id
          # Thread.current.object_id
          Thread.main.object_id
        end
end

This is used for Selenium when running the browser in tests. Tests use transactions and the browser is a seperate process so it cant see the DB. This may not be the problem/solution but its worth a try.

Kieran Andrews
  • 5,845
  • 2
  • 33
  • 57