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

Fail to setup database_cleaner for dual database #419

Open
leomao10 opened this issue Jan 14, 2016 · 2 comments
Open

Fail to setup database_cleaner for dual database #419

leomao10 opened this issue Jan 14, 2016 · 2 comments

Comments

@leomao10
Copy link

I have two database, one with a model call Device, and the other has a model call User.

When I set up database_cleaner according to readme describe:

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

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

I found that in my test, device model's connection will point to secondary_test database. And I will have following error as my secondary_test database don't have a table call devices:

ActiveRecord::StatementInvalid:
  PG::UndefinedTable: ERROR:  relation "devices" does not exist
  LINE 5:                WHERE a.attrelid = '"devices"'::regclass
                                            ^
  :               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                       pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                  FROM pg_attribute a LEFT JOIN pg_attrdef d
                    ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                 WHERE a.attrelid = '"devices"'::regclass
                   AND a.attnum > 0 AND NOT a.attisdropped
                 ORDER BY a.attnum

I dig in the code, and found that it is cause by #connection_class

      def connection_class
        @connection_class ||= if db && !db.is_a?(Symbol)
                                db
                              elsif connection_hash
                                lookup_from_connection_pool || establish_connection
                              else
                                ::ActiveRecord::Base
                              end
      end

      private

      def lookup_from_connection_pool
        if ::ActiveRecord::Base.respond_to?(:descendants)
          database_name = connection_hash["database"] || connection_hash[:database]
          models        = ::ActiveRecord::Base.descendants
          models.detect { |m| m.connection_pool.spec.config[:database] == database_name }
        end
      end

      def establish_connection
        ::ActiveRecord::Base.establish_connection(connection_hash)
      end

However, since ::ActiveRecord::Base.descendants is blank when lookup_from_connection_pool is called, lookup_from_connection_pool would return nil, and establish_connection will be called. Unfortunately, ActiveRecord::Base.establish_connection(connection_hash) has a huge side effect in Rails 4+. It will set connection to specify one for ALL models that inherit from ActiveRecord::Base class. This can be reproduced by following code:

ActiveRecord::Base.establish_connection('test')
Device.connection

In my case, following code:

DatabaseCleaner[:active_record,{:connection => :test}].start
DatabaseCleaner[:active_record,{:connection => :secondary_test}].start

will first set ALL ActiveRecord::Base descendants' connections to test db, then it will set ALL their connection to secondary_test. Therefore, when I try to use Device in my spec, I will have PG::UndefinedTable: ERROR

Here is the similar issues that cause by the side effect for establish_connection:

@etagwerker
Copy link
Member

@leomao10 Could you try this workaround and report back? https://gist.github.com/mgreenly/1109325

Thanks!

@fmnoise
Copy link

fmnoise commented Mar 26, 2016

I've fixed this by adding Rails.application.eager_load! to spec helper

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

  Rails.application.eager_load!

  %i(db_test1 db_test2 db_test3).each do |connection|
    config.before :suite do
      # transaction strategy is not working with multiple databases
      DatabaseCleaner[:active_record, { connection: connection }].strategy = :deletion
      DatabaseCleaner[:active_record, { connection: connection }].clean_with :truncation
    end

    config.around :each do |example|
      DatabaseCleaner[:active_record, { connection: connection }].cleaning &example
    end
  end
end

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

3 participants