Usage of SQL and NoSQL Databases in single rails application(MySQL, PostgreSQL and MongoDB)

There are distinctive reasons why you should think about having various databases in your Ruby on Rails application. In my situation, I expected to store large quantities of data.

Consider default database is MySQL. In our application database.yml file write connections for MySQL in normal way. After that, for connecting postgresql in the same application we need to create custom files.

Create the custom database files to connect postgresql

We’re going to set up a second database called “Stats”

First of all, create the file config/database_stats.yml and populate it as you do with the primary database’s config file.

Your file will look something like this:

development:
adapter: postgresql
encoding: utf8
reconnect: false
database: db_info_development
pool: 5
host: localhost
username: postgres
password:

We’re now going to create a directory that will hold the schema and all the migrations of the Stats database.

Create directory with name db_stats in the rails root and copy the structure as mentioned below

–db
–migrate
schema.rb
seeds.rb
–db_stats
–migrate
schema.rb
seeds.rb

The created files should be empty.

Add Rake Tasks

For handling stats database, we need to write custom tasks for creation, migrations and other functionalities.

Create a file lib/tasks/db_stats.rake with the below content

namespace :stats do
namespace :db do |ns|
task :drop do
Rake::Task[“db:drop”].invoke
end
task :create do
Rake::Task[“db:create”].invoke
end
task :setup do
Rake::Task[“db:setup”].invoke
end
task :migrate do
Rake::Task[“db:migrate”].invoke
end
task :rollback do
Rake::Task[“db:rollback”].invoke
end
task :seed do
Rake::Task[“db:seed”].invoke
end
task :version do
Rake::Task[“db:version”].invoke
end
namespace :schema do
task :load do
Rake::Task[“db:schema:load”].invoke
end
task :dump do
Rake::Task[“db:schema:dump”].invoke
end
end
namespace :test do
task :prepare do
Rake::Task[“db:test:prepare”].invoke
end
end

# append and prepend proper tasks to all the tasks as defined above

ns.tasks.each do |task|
task.enhance [“stats:set_custom_config”] do
Rake::Task[“stats:revert_to_original_config”].invoke
end
end
end
task :set_custom_config do

# save current vars

@original_config = {
env_schema: ENV[‘SCHEMA’],
config: Rails.application.config.dup
}

# set config variables for custom database

ENV[‘SCHEMA’] = “db_stats/schema.rb”
Rails.application.config.paths[‘db’] = [“db_stats”]
Rails.application.config.paths[‘db/migrate’] = [“db_stats/migrate”]
Rails.application.config.paths[‘db/seeds’] = [“db_stats/seeds.rb”]
Rails.application.config.paths[‘config/database’] = [“config/database_stats.yml”]
end
task :revert_to_original_config do
# reset config variables to original values
ENV[‘SCHEMA’] = @original_config[:env_schema]
Rails.application.config = @original_config[:config]
end
end

Read full article on : Usage of SQL and NoSQL Databases in single rails application(MySQL, PostgreSQL and MongoDB)

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.