3

I have a web application in Rails 4.2.6 that was originally using MongoDB (through Mongoid 5) as its main database. However, now I need to connect to a MySQL database to only read some additional data for my app.

So far, I have required ActiveRecord in my project and was able to establish the connection with the aforementioned database in development environment. My configuration files for this matter are:

mongoid.yml

development:
  clients:
    default:
      database: mongo_development
      hosts:
        - localhost:27017
      options:
test:
  clients:
    default:
      database: mongo_test
      hosts:
        - localhost:27017
      options:
        read:
          mode: :primary
        max_pool_size: 1
  options:
    raise_not_found_error: false

database.yml

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: mysql_development
  pool: 5
  username: user
  password: pass
  socket: /var/run/mysqld/mysqld.sock

The problem arrives when I run my test suite. I am using RSpec and executing bundle exec rspec spec yields the following error message:

 /home/user/.rvm/gems/ruby-2.1.6@global/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_connection': 'test' database is not configured. Available: ["development"] (ActiveRecord::AdapterNotSpecified)

ActiveRecord complains for not having a test database to connect, but the problem is that I don't have a MySQL database for testing purposes: the specs I wrote only interact with Mongoid models.

I was considering creating an empty MySQL database as a hack, but I want to know first if I could configure ActiveRecord not to connect to a test database and use only mongo_test.

Is it possible to do what I want? Does anybody know how to do this?

Daniel Ruiz
  • 600
  • 5
  • 12

2 Answers2

1

Here are two workarounds

Either: Add a sqlite database for your test env.

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

Or: Move your autoload from application.rb to environment specific configs and move the activerecord gem to a development env on your Gemfile.

Gemfile

group :development do
  gem 'activerecord'
end

config/application.rb:

  • First: move this to config/environments/test.rb and config/environments/development.rb (keep in mind which models you want to exclude from the test env, i.e. the activerecord mo

    config.autoload_paths += WHATEVER
    

Remove require 'rails/all' line and require frameworks you want to use specifically in each environment file too.

Oss
  • 4,232
  • 2
  • 20
  • 35
  • Thank you for your suggestion. I'm trying to apply the second workaround you stated, but I encountered some problems with the autoloading of `activerecord` models. It seems like `config.autoload_paths` variable cannot be modified in the environment-specific files, as it's mentioned in http://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoload-paths – Daniel Ruiz Jun 01 '16 at 13:42
  • Check this answer about `config.eager_load_paths` and how it acts different between development and production http://stackoverflow.com/questions/19773266/confusing-about-autoload-paths-vs-eager-load-paths-in-rails-4 , I think it will be useful. – Oss Jun 01 '16 at 13:46
  • Thanks again for your help, Mohamed! I was able to isolate the loading of my `activerecord` models and the require of `activerecord`'s railtie in _config/application.rb_ using conditions like `require 'active_record/railtie' unless Rails.env.test?` and `config.autoload_paths += Dir[Rails.root.join('lib', 'antifraud', '**/')] unless Rails.env.test?`. Currently, when I run my specs they all fail returning the same error: `ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base` Does this error make sense to you? Would you know how to solve it? – Daniel Ruiz Jun 02 '16 at 07:08
1

I decided to answer my own question as the provided answers didn't fit my needs.

After many trials and errors I was able to avoid connecting to a MySQL database in test environment adapting the information given in this question post.

The only thing I needed to do was to add the following gem to my Gemfile:

group :test do
  gem 'activerecord-nulldb-adapter'
end

This gem installs the NullDB database adapter in the project, which enables ActiveRecord to establish a connection without the need to define a real database.

Last but not least, I updated my config/database.yml adding the parameters of a database connection in test environment that uses the new adapter:

test:
  adapter: nulldb
  encoding: utf8
  reconnect: false
  database: mysql_test
  pool: 5
  username: user
  password: pass
  socket: /var/run/mysqld/mysqld.sock
Community
  • 1
  • 1
Daniel Ruiz
  • 600
  • 5
  • 12