简体   繁体   中英

Rails : Multi-tenancy using Apartment and MySQL

I'm creating an application (on rails 6.0.1) using the Apartment gem & Devise, but i can't figure out how to make it work using MySQL..

I've made all my config and stuff, generating my User devise model etc. but when i'm trying to rails db:migrate an error occurs :

Migrating  tenant
Error while connecting to tenant : Mysql2::Error: No database selected

Meaning i havent configured Apartment correctly to work with my Mysql database right? I couldn't find any informations (that i can understand in beginner terms) on how to connect it correctly. My database.yml has the following :

// database.yml

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: myusername
  password: mypassword
  socket: /tmp/mysql.sock

(...)

Note that i have the good username and password in my rails app :) Everything worked fine until i've tried to create the multitenancy stuff. Meaning the issue is maybe coming from my config/initializers/apartment.rb file?

Here is what it looks like right now

// config/initializers/apartment.rb
require 'apartment/elevators/subdomain'
Apartment.configure do |config|
  config.excluded_models = %w[User]
  config.tenant_names = -> { User.pluck :subdomain }
  config.use_schemas = true
end

Rails.application.config.middleware.use Apartment::Elevators::Subdomain

Edit : I'm using the development branch from Apartment. Maybe it's an issue with Rails 6?

// Gemfile

gem 'apartment', github: 'influitive/apartment', branch: 'development'

I guess by now you solved your problem but for those who came here searching to deal with multi tenancy in Rails 6. A couple of things I found by myself:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM