【问题标题】:Schema selection in Apartment gem not working as expectedApartment gem 中的模式选择未按预期工作
【发布时间】:2019-08-05 12:18:37
【问题描述】:

在一个演示应用程序中,我们希望处理不同模式中的租户(Postgres 的默认公寓配置)。这些将从应用程序 url 的子域中选择。

我让它工作了,但后来又坏了(它开始使用公共而不是选定的架构)。

我可以将问题缩小到这一点:如果我使用以下代码,一切都会按预期工作:

require 'apartment/elevators/first_subdomain'
Rails.application.config.middleware.use Apartment::Elevators::FirstSubdomain
Apartment::Elevators::FirstSubdomain.excluded_subdomains = ['demo', 'localhost']

如果我自己这样做(将丢失的租户行为扩展为返回 404),它就不再起作用了:

class DemoUserSelectionMiddleware < Apartment::Elevators::Generic
  def parse_tenant_name(request)
    subdomain = request.host.split('.')[0]
    unless Rails.application.config.is_demo && subdomain.start_with?('demo_')
      'public'
    else
      subdomain
    end
  end

  def call(*args)
    begin
      super
    rescue Apartment::TenantNotFound
      Rails.logger.error "ERROR: Apartment Tenant not found: #{Apartment::Tenant.current.inspect}"
      return [404, {"Content-Type" => "application/json"}, ["Error" => "Tenant not found"]]
    end
  end
end

没有错误,没有日志条目,什么都没有。它只是决定使用“公共”。 使用 byebug 我看到正确找到了子域。

我在这里遗漏了什么明显的东西吗?

【问题讨论】:

    标签: ruby-on-rails apartment-gem


    【解决方案1】:

    在第一个 sn-p 中,您将演示子域添加到 exclude_subdomains。换句话说,公共租户应该在演示子域中处于活动状态。在其他子域中,相应的租户将处于活动状态。

    在您编写的那段代码中,公共租户不使用演示子域。相反,公共租户在所有子域中都很活跃。

    .
    .
    unless Rails.application.config.is_demo && subdomain.start_with?('demo_')
    .
    .
    

    这里似乎存在逻辑错误。我认为你应该使用if 而不是unless

    【讨论】:

    • 逻辑是正确的,它将是公开的除非我们在演示并且de子域是demo_.*。正如我所说,我确实调试了它,所以我知道这两段代码返回了我期望的结果。但正如我所说,Apartment 正在回归“公共”(在某些情况下哪个公寓会这样做,但我不明白为什么在这里)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多