Topic: Multi-tenancy with the gem Apartment

Good day,

I am new in Ruby On Rails. I am developping an application where I have several databases to manage. Each database has the same structure. I have to separate them for confidentiality purposes. I have only two databases for the beginning of the development, but at the end I will be closer to ten databases.

To manage this, I found the gem Apartment. It seems to fit my needs. I started my new application with this gem.
The different databases have been created in MySQL through the interface phpmyadmin. I have the following databases : "test_db1, development_db1, production_db1, test_db2, development_db2, production_db2".

I have a model "Customer" in my Rails Application. I applied the migration by executing the following commands :

rake db:migrate
rake RAILS_ENV=db2 db:migrate

In my seed.rb, I am trying to use the function Apartment::Database.switch('db_name') to seed the different databases.
When I execute the command "rake db:migrate", I have the following error in the console : "Cannot find database db2"

I don't know if the problem is coming from my database.yml, or the way I am using the gem Apartment, or both, or something else.
Can someone help me on this problem please ?

If some information are missing, please let me know, I will provide with what I can.

Thank you,

Following the different technical information :

I am using Rails v3.2.13.

The database.yml is like this :

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: development_db1
  pool: 5
  username: xxxxxx
  password: xxxxxx

test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: test_db1
  pool: 5
  username: xxxxxx
  password: xxxxxx

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: production_db1
  pool: 5
  username: xxxxxx
  password: xxxxxx

development_db2:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: development_db2
  pool: 5
  username: xxxxxx
  password: xxxxxx

test_db2:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: test_db2
  pool: 5
  username: xxxxxx
  password: xxxxxx

production_db2:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: production_db2
  pool: 5
  username: xxxxxx
  password: xxxxxx

At the moment, I have inside my ApplicationController the creation of the databases :

class ApplicationController < ActionController::Base
  protect_from_forgery

  def initialize
    Apartment::Database.create('db1')
    Apartment::Database.create('db2')
  end
end

The seed.rb is switching between the two databases to add data from a CSV file (The CSV Importer has been tested in another project and I know it is working fine) :

Apartment::Database.switch('db1')
CsvImporter.new('Customer', Compound, [:id, :name, :firstname, :phone, :email, :adress], Encoding::ISO_8859_1).seed

Apartment::Database.switch('db2)
CsvImporter.new('Customer', Compound, [:id, :name, :firstname, :phone, :email, :adress], Encoding::ISO_8859_1).seed