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?
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