4

I am creating application in Ruby on Rails which is having many engines(for modularity).

I want different databases for each engine. How to configure this?

Database - MYSQL

2 Answers 2

3

There is a good explanation by the link http://www.blrice.net/blog/2016/04/09/one-rails-app-with-many-databases/

Sign up to request clarification or add additional context in comments.

1 Comment

I know this answer may be old-business to you, but it would be best if you resummarize major points about the article you cite. That article can go away, and this answer would then be meaningless.
1

General approach is to take a look at the framework sources and decide can it be reused.

Let's take a look at activerecord/lib/active_record/railties/databases.rake (v5.0.7) first. For example on how db:create implemented.

We will see ActiveRecord::Tasks::DatabaseTasks.create_current.

Let's open ActiveRecord::Tasks::DatabaseTasks and take a look at

    # The possible config values are:
    #
    # * +env+: current environment (like Rails.env).
    # * +database_configuration+: configuration of your databases (as in +config/database.yml+).
    # * +db_dir+: your +db+ directory.
    # * +fixtures_path+: a path to fixtures directory.
    # * +migrations_paths+: a list of paths to directories with migrations.
    # * +seed_loader+: an object which will load seeds, it needs to respond to the +load_seed+ method.
    # * +root+: a path to the root of the application.
    #
    # Example usage of DatabaseTasks outside Rails could look as such:
    #
    #   include ActiveRecord::Tasks
    #   DatabaseTasks.database_configuration = YAML.load_file('my_database_config.yml')
    #   DatabaseTasks.db_dir = 'db'
    #   # other settings...

This way we are getting to following solution:

namespace :your_engine do
  namespace :db do

    task :load_config do
      ActiveRecord::Tasks::DatabaseTasks.database_configuration = YAML.load_file("config/database_your_engine.yml")
      ActiveRecord::Tasks::DatabaseTasks.db_dir = "db_your_engine"
      ActiveRecord::Tasks::DatabaseTasks.migrations_paths = [ "components/your_engine/db/migrate" ]

      ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration
      ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths

      # You can observe following values to see how settings applied.
      # puts ActiveRecord::Base.configurations
      # puts ActiveRecord::Migrator.migrations_paths
      # puts ActiveRecord::Tasks::DatabaseTasks.database_configuration
      # puts ActiveRecord::Tasks::DatabaseTasks.migrations_paths
    end

    desc "Create Your DB"
    task create: :load_config do
      ActiveRecord::Tasks::DatabaseTasks.create_current
    end
  end
end

The same approach for drop/migrate and other needed tasks.

It is good general rule - know you stack at least one level lower than your work with. Sometimes reading underlying sources much more helpful than direct answer.

I will update this answer while going forward with my solution...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.