influitive / apartment

Database multi-tenancy for Rack (and Rails) applications
2.67k stars 463 forks source link

No custom database names when with_multi_server_setup is true #583

Closed derosm2 closed 5 years ago

derosm2 commented 5 years ago

I don't see any option to customize the database name to something other than the tenant name (or appending/prepending env). Adding a configuration option would be useful to me, if there's any interest in a PR.

As seen below I'm calling my default partner db just so that I'm able to match our existing databases.

Thanks!

lcjury commented 5 years ago

From the readme

config.with_multi_server_setup = true
config.tenant_names = {
  'tenant1' => {
    adapter: 'postgresql',
    host: 'some_server',
    port: 5555,
    database: 'postgres' # this is not the name of the tenant's db
  }
}

when you use multiserver, your configuration should be a hash (or a lambda who returns a hash) of {tenant_name: configuration} where configuration should be another hash who can have the following keys: adapter, host, port, database.

In database you can specify your database name.

So, your config should be something like:

 config.with_multi_server_setup = true
  config.default_tenant = "db"
  config.tenant_names = {
    "db" => { database: Rails.configuration.database_configuration[Rails.env] },
    "some_tenant" => { database: Rails.configuration.database_configuration["#{Rails.env}_some_tenant"] }
  }

  config.use_schemas = false
  config.prepend_environment = false
  config.append_environment = Rails.env.development? || Rails.env.test?
henriquekraemer commented 4 years ago

The database config only works with pg? If yes, is there a way to have the tenant_name different from the mysql schema name?

I have tried a lot of things, but without success (witch make sense if the database option only works with pg).

My config/database.yml file:

development:
  adapter: mysql2
  encoding: utf8
  host: 127.0.0.1
  pool: 5
  port: 32769
  username: root
  password: 
  database: manusis_client_1

client_1:
  adapter: mysql2
  encoding: utf8
  host: 127.0.0.1
  pool: 5
  port: 32769
  username: root
  password: 
  database: manusis_client_1

client_2:
  adapter: mysql2
  encoding: utf8
  host: 127.0.0.1
  pool: 5
  port: 32771
  username: root
  password: 
  database: manusis_client_2

My config/initializers/apartment.rb file:

require 'apartment/elevators/subdomain'

#
# Apartment Configuration
#
Apartment.configure do |config|
  config.with_multi_server_setup = true
  config.use_schemas = false

  config.tenant_names = {
    "client_1" => { 
      adapter: 'mysql2',
      host: '127.0.0.1',
      port: '32769',
      database: Rails.configuration.database_configuration["client_1"]
    },

    "client_2" => { database: Rails.configuration.database_configuration["client_2"] },

    "client_3" => { 
      "database" => {
        "adapter"=>"mysql2",
        "encoding"=>"utf8",
        "host"=>"127.0.0.1",
        "pool"=>5, 
        "port"=>32769,
        "username"=>"root",
        "password"=>nil,
        "database"=>"manusis_client_1"
      },

      "client_4" => {
        adapter: 'mysql2',
        host: '127.0.0.1',
        port: '32769',
        "database" => {
          "adapter"=>"mysql2",
          "encoding"=>"utf8",
          "host"=>"127.0.0.1",
          "pool"=>5, 
          "port"=>32769,
          "username"=>"root",
          "password"=>nil,
          "database"=>"manusis_client_1"
        }
      }
    }
  }
end

Here are the errors that I have depending on the tenant:

http://client_1.lvh.me:3000/ Error while connecting to tenant client_1: Unknown database 'client_1'

http://client_2.lvh.me:3000/ Error while connecting to tenant client_2: database configuration does not specify adapter

http://client_3.lvh.me:3000/ Error while connecting to tenant client_3: database configuration does not specify adapter

http://client_4.lvh.me:3000/ Error while connecting to tenant client_4: Unknown database 'client_4'

When I dont specify the host, port, apapter it gives me the "database configuration does not specify adapter" error, the attempts with client_3 and client_4 was to see if could work...

I'm running on:

- Ruby: 2.3.8
- Rails: 4.2.11.1
- Apartment: 2.2.1