我已经设置了模式文件,但无法为租户定义种子文件,因此它只能为租户迁移运行.此外,我还尝试在创建用户并创建其租户后创建架构.
    require 'apartment/elevators/subdomain'
    #
    # Apartment Configuration
    #
    Apartment.configure do |config|
      config.excluded_models = ["Admin","Contractor", "ContractorPackage","ContractorTransaction","Country","Currency","Faq","FaqCategory","Language","Package","Page","PaymentType","Setting","TempTransaction","Testimonial","Timezone","Tutorial"]
      # use postgres schemas?
      config.use_schemas = true
       config.tenant_names = lambda{ Contractor.pluck("CONCAT('contractor_',id)") }
    end
    # overriding module schema file here
    module Apartment
      class << self
            def database_schema_file
                @database_schema_file=Rails.root.join('db', 'contractor_schema.rb')
            end
        end
    end
    Rails.application.config.middleware.use 'Apartment::Elevators::Subdomain'
Run Code Online (Sandbox Code Playgroud) 我正在将 Apartment gem 与 Unicorn 和 Nginx 一起使用。我正在使用子域电梯。在initializers/apartment/subdomain_exclusions我有Apartment::Elevators::Subdomain.excluded_subdomains = ['www']
我的理解是公共架构现在应该与公共子域、www 子域或没有子域(即 mydomain.com)一起使用。
但是,它不是这样工作的。当我使用 www 或不使用子域时,它使用上次访问的架构。因此,如果我只是使用另一个子域,它将使用该架构。这是不好的。我什至尝试添加,config.default_schema = "public"但这没有任何作用。
任何想法为什么这不起作用?有没有人以类似的方式设置它并且在没有指定子域时只使用公共模式?它可能是Nginx配置吗?
我需要删除数据库中的所有模式,除了public,information_schema以及那些LIKE 'pg_%'.
这是我发现的:(此变体似乎不起作用)
   CREATE OR REPLACE FUNCTION drop_all () 
   RETURNS VOID  AS
   $$
   DECLARE rec RECORD; 
   BEGIN
       -- Get all the schemas
        FOR rec IN
        SELECT DISTINCT schemaname
         FROM pg_catalog.pg_tables
         -- You can exclude the schema which you don't want to drop by adding another condition here
         WHERE schemaname NOT LIKE 'pg_%' AND schemaname != 'public' 
         AND schemaname != 'information_schema'
           LOOP
             EXECUTE 'DROP SCHEMA ' || rec.schemaname || ' CASCADE'; 
           END LOOP; 
           RETURN; 
   END; …Run Code Online (Sandbox Code Playgroud) 我正在努力将现有的单租户应用程序迁移到使用Apartment和Devise不使用子域的多租户应用程序。
我已经有一个应用程序,该应用程序拥有Company属于该公司的多个用户。
现在我需要扩展该应用程序并添加另一个Company. 每家公司的用户都是独一无二的,并通过电子邮件进行识别。
所以我想要的是 - 当用户尝试登录时 - 我想根据用户电子邮件切换到正确的租户并继续流程。
我想将所有User数据保存在单独的数据库中。在这里我不明白如何查找正确的租户?所有指南和示例都显示了如何处理何时User添加到excluded_models. 但这对我不起作用。
所以看起来,表public所在的地方应该有一些模式UserEmail_Tenant?或者我错过了什么?
我正在使用公寓 gem 和 MySQL 作为数据库在 rails 中创建一个多租户应用程序。我在https://gorails.com/episodes/multitenancy-with-apartment 之后设置了公寓。
在此之后,每当我创建一个新的子域时,都会为每个子域创建新的数据库。现在,如果我执行 rake db:drop,它只会删除主数据库,而所有子域数据库都保持不变。
我的查询是如何删除子域数据库。
我有带有Apartment gem的多租户 Rails 应用程序,我可以使用Apartment-sidekiq成功切换每个工作人员中的数据库租户。但 sidekiq 工作人员为所有租户使用相同的 redis 服务器。我也想在redis上有完全的隔离。有没有一种方法可以让我为每个租户使用单独的 redis 主机,而不会影响使用 sidekiq middeware 进行的大量代码更改。
租户配置示例
 TENANT_POOL = {
  tenant_1: ConnectionPool.new { Redis.new(url: ENV['REDIS_URL1']) },
  tenant_2: ConnectionPool.new { Redis.new(url: ENV['REDIS_URL2']) }  
 }
Run Code Online (Sandbox Code Playgroud)
例如。工人
class SendMailWorker
  include Sidekiq::Worker
  def perform(args)
    puts "In Worker Tenant: #{Apartment::Tenant.current}, #{Sidekiq.redis_pool.inspect}"
  end
end     
Run Code Online (Sandbox Code Playgroud)
客户端中间件
class MyMiddleware::Client::ChangeRedisPool
  def call(worker_class, job, queue, redis_pool)
    #Can I change redis pool/client here to execute job in separate redis 
    yield
  end
end
Run Code Online (Sandbox Code Playgroud)
服务器中间件
class MyMiddleware::Server::ChangeServerRedisPool
  def call(worker, job, queue)
    #Can I …Run Code Online (Sandbox Code Playgroud) 我们是否可以在公寓宝石中重命名租户,或者我们必须放弃租户并创建一个新租户来实现这一目标.
请给我一些建议
我正在用heroku和Rails构建一个SAAS平台。对于这些,我要通过来管理租户apartment gem。
在我的本地开发中,这没有问题,但是当我尝试将其用于heroku时,出现下一个错误:
/app/db/schema.rb尚不存在
运行下一个命令时,将产生此错误:
公寓:: Tenant.create('foo_schema')
我将写主要步骤来设置带有子域+公寓配置的heroku。
postgresql的production和developmentapartment.rb文件设置到initializer目录中。我的 apartment.rb
require 'apartment/elevators/subdomain'
Apartment.configure do |config|
  config.excluded_models = %w{Account Company App Acteco Country Currency Price}
  config.tenant_names = lambda { Company.pluck(:slug) }
  config.use_schemas = true
end
Rails.application.config.middleware.insert_before Warden::Manager, Apartment::Elevators::Subdomain
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
我在设计登录或退出后重定向到子域时遇到问题。我生成了设计控制器未注释的方法,这些方法应该带我去我想要的地方,但子域不起作用。
    def after_sign_in_path_for(resource)
      root_url(subdomain: resource.subdomain)
    end
    def after_sign_out_path_for(resource_or_scope)
      home_url(subdomain: "www")
    end
Run Code Online (Sandbox Code Playgroud)
登录后我收到错误
不安全的重定向到“http://chris.lvh.me:3000/”,无论如何都要传递allow_other_host: true来重定向。
因此,我以几种方式修改了方法,但它给了我更多错误,我尝试过的示例:
root_url(subdomain: resource.subdomain), allow_other_hosts: true
root_url(subdomain: resource.subdomain, allow_other_hosts: true)
redirect_to root_url(subdomain: resource.subdomain),allow_other_hosts: true
redirect_to root_url(subdomain: resource.subdomain, allow_other_hosts: true)
Run Code Online (Sandbox Code Playgroud)
错误是:参数数量错误或意外的“、”预期结束或其他内容。
退出后,我会进入登录页面,而不是 home_url,并且子域不会更改。
我正在使用 devise、rails-on-services/apartment 和 Rails 7
devise ×2
multi-tenant ×2
postgresql ×2
ruby ×2
heroku ×1
localhost ×1
mysql ×1
redis ×1
sidekiq ×1
subdomain ×1