ruby-on-rails - 公寓和设计不创建租户

标签 ruby-on-rails ruby postgresql devise apartment-gem

嗨,我是第一次和公寓一起工作,然后是 https://gorails.com/episodes/multitenancy-with-apartment?autoplay=1但我正在使用设计进行身份验证。

当我去创建一个新的用户和租户时,我得到以下错误

    ActiveRecord::StatementInvalid in Devise::RegistrationsController#create

PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block : SET search_path TO "public"

  def create_tenant
    Apartment::Tenant.create('tenant_name')
  end
end

我有一个设计用户模型,我在 user.account 上创建一个租户

模型/用户.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  after_create :create_tenant

  private

  def create_tenant
    Apartment::Tenant.create('tenant_name')
  end
end

设计/注册/新

<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :firstName %><br />
    <%= f.text_field :firstName %>
  </div>

  <div class="field">
    <%= f.label :lastName %><br/>
    <%= f.text_field :lastName %>
  </div>

  <div class="field">
    <%= f.label :account %><br/>
    <%= f.text_field :account%>
  </div>

  <div class="field">
    <%= f.label :email %><br/>
    <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

Controller /application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:firstName, :lastName, :account])
    devise_parameter_sanitizer.permit(:sign_in, keys: [:firstName, :lastName, :account])
  end
end

配置/应用程序.rb

require_relative 'boot'

require 'rails/all'
require  'csv'
require 'apartment/elevators/subdomain'

Bundler.require(*Rails.groups)

module AssetCatcher
  class Application < Rails::Application
    config.load_defaults 5.1
    config.generators do |g|
      g.orm :active_record, primary_key_type: :uuid
    end
    config.middleware.use Apartment::Elevators::Subdomain
  end
end

config/initializers/apartment.rb

# You can have Apartment route to the appropriate Tenant by adding some Rack middleware.
# Apartment can support many different "Elevators" that can take care of this routing to your data.
# Require whichever Elevator you're using below or none if you have a custom one.
#
# require 'apartment/elevators/generic'
# require 'apartment/elevators/domain'
require 'apartment/elevators/subdomain'
# require 'apartment/elevators/first_subdomain'
# require 'apartment/elevators/host'

#
# Apartment Configuration
#
Apartment.configure do |config|

  # Add any models that you do not want to be multi-tenanted, but remain in the global (public) namespace.
  # A typical example would be a Customer or Tenant model that stores each Tenant's information.
  #
  # config.excluded_models = %w{ Tenant }

  # In order to migrate all of your Tenants you need to provide a list of Tenant names to Apartment.
  # You can make this dynamic by providing a Proc object to be called on migrations.
  # This object should yield either:
  # - an array of strings representing each Tenant name.
  # - a hash which keys are tenant names, and values custom db config (must contain all key/values required in database.yml)
  # config.tenant_names = lambda { User.pluck :account }
  # config.tenant_names = lambda{ Customer.pluck(:tenant_name) }
  # config.tenant_names = ['tenant1', 'tenant2']
  # config.tenant_names = {
  #   'tenant1' => {
  #     adapter: 'postgresql',
  #     host: 'some_server',
  #     port: 5555,
  #     database: 'postgres' # this is not the name of the tenant's db
  #                          # but the name of the database to connect to before creating the tenant's db
  #                          # mandatory in postgresql
  #   },
  #   'tenant2' => {
  #     adapter:  'postgresql',
  #     database: 'postgres' # this is not the name of the tenant's db
  #                          # but the name of the database to connect to before creating the tenant's db
  #                          # mandatory in postgresql
  #   }
  # }
  # config.tenant_names = lambda do
  #   Tenant.all.each_with_object({}) do |tenant, hash|
  #     hash[tenant.name] = tenant.db_configuration
  #   end
  # end
  #
  config.tenant_names = lambda { User.pluck :account }
  # PostgreSQL:
  #   Specifies whether to use PostgreSQL schemas or create a new database per Tenant.
  #
  # MySQL:
  #   Specifies whether to switch databases by using `use` statement or re-establish connection.
  #
  # The default behaviour is true.
  #
  # config.use_schemas = true

  #
  # ==> PostgreSQL only options

  # Apartment can be forced to use raw SQL dumps instead of schema.rb for creating new schemas.
  # Use this when you are using some extra features in PostgreSQL that can't be represented in
  # schema.rb, like materialized views etc. (only applies with use_schemas set to true).
  # (Note: this option doesn't use db/structure.sql, it creates SQL dump by executing pg_dump)
  #
  # config.use_sql = false

  # There are cases where you might want some schemas to always be in your search_path
  # e.g when using a PostgreSQL extension like hstore.
  # Any schemas added here will be available along with your selected Tenant.
  #
  # config.persistent_schemas = %w{ hstore }

  # <== PostgreSQL only options
  #

  # By default, and only when not using PostgreSQL schemas, Apartment will prepend the environment
  # to the tenant name to ensure there is no conflict between your environments.
  # This is mainly for the benefit of your development and test environments.
  # Uncomment the line below if you want to disable this behaviour in production.
  #
  # config.prepend_environment = !Rails.env.production?
end

# Setup a custom Tenant switching middleware. The Proc should return the name of the Tenant that
# you want to switch to.
# Rails.application.config.middleware.use Apartment::Elevators::Generic, lambda { |request|
#   request.host.split('.').first
# }

# Rails.application.config.middleware.use Apartment::Elevators::Domain
Rails.application.config.middleware.use Apartment::Elevators::Subdomain
# Rails.application.config.middleware.use Apartment::Elevators::FirstSubdomain
# Rails.application.config.middleware.use Apartment::Elevators::Host

Apartment::Elevators::Subdomain.excluded_subdomains = ['www']

我确定我错过了一些简单的事情,感谢您的宝贵时间

更新:

所以我更新了 models/user.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  after_commit :create_tenant

  private

  def create_tenant
    Apartment::Tenant.create(account)
  end
end

根据 的建议,您会注意到 after_create 变成了 after_commit 的两个变化。然后我找到了 https://github.com/influitive/apartment/issues/342在公寓 gem 上。它被标记为关闭,我添加了以下内容。

config/initializers/apartment.rb

Apartment.configure do |config|
  config.persistent_schemas = %w{uuid}
end

一旦公寓能够提交,我发现错误是由 UUID 创建的。我的子域已创建,但出现以下错误:

ActiveRecord::StatementInvalid in Devise::RegistrationsController#create
PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist HINT: No function matches the given name and argument types. You might need to add explicit type casts. : CREATE TABLE "asset_types" ("id" uuid DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY, "name" character varying, "monthly" decimal, "quarterly" decimal, "halfyearly" decimal, "annual" decimal, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)

我的架构如下:

ActiveRecord::Schema.define(version: 20180507085312) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"
  enable_extension "uuid-ossp"
  enable_extension "pgcrypto"

  create_table "asset_types", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.decimal "monthly"
    t.decimal "quarterly"
    t.decimal "halfyearly"
    t.decimal "annual"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "asset_types_maintenance_specifications", id: false, force: :cascade do |t|
    t.uuid "asset_type_id", null: false
    t.uuid "maintenance_specification_id", null: false
    t.uuid "[:asset_type_id, :maintenance_specification_id]"
  end

  create_table "frequencies", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.uuid "maintenance_specification_id"
    t.uuid "asset_type_id"
    t.boolean "monthly"
    t.boolean "quarterly"
    t.boolean "halfyearly"
    t.boolean "annual"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["asset_type_id"], name: "index_frequencies_on_asset_type_id"
    t.index ["maintenance_specification_id"], name: "index_frequencies_on_maintenance_specification_id"
  end

  create_table "locations", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.uuid "site_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["site_id"], name: "index_locations_on_site_id"
  end

  create_table "maintenance_quotes", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "section"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.uuid "site_id"
    t.uuid "maintenance_specification_id"
    t.index ["maintenance_specification_id"], name: "index_maintenance_quotes_on_maintenance_specification_id"
    t.index ["site_id"], name: "index_maintenance_quotes_on_site_id"
  end

  create_table "maintenance_quotes_sections", id: false, force: :cascade do |t|
    t.uuid "maintenance_quote_id", null: false
    t.uuid "section_id", null: false
    t.index ["maintenance_quote_id", "section_id"], name: "maintenance_quote_sections"
  end

  create_table "maintenance_specifications", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.uuid "asset_type_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["asset_type_id"], name: "index_maintenance_specifications_on_asset_type_id"
  end

  create_table "manufacturers", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.integer "qualityScore"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "sections", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "title"
    t.text "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "site_assets", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.string "model"
    t.string "serial"
    t.date "installed"
    t.uuid "site_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "condition"
    t.uuid "asset_type_id"
    t.uuid "location_id"
    t.uuid "manufacturer_id"
    t.string "refrigerant"
    t.string "description"
    t.index ["asset_type_id"], name: "index_site_assets_on_asset_type_id"
    t.index ["location_id"], name: "index_site_assets_on_location_id"
    t.index ["manufacturer_id"], name: "index_site_assets_on_manufacturer_id"
    t.index ["site_id"], name: "index_site_assets_on_site_id"
  end

  create_table "sites", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "streetAddress"
    t.string "suburb"
    t.string "postcode"
    t.string "state"
    t.string "country"
    t.integer "environment"
    t.integer "market"
  end

  create_table "users", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet "current_sign_in_ip"
    t.inet "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "firstName"
    t.string "lastName"
    t.string "account"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end

最佳答案

问题是 Apartment::Tenant.create('tenant_name') 在数据库级别引发了一些错误,从而影响了 future 的交易。

PG::InFailedSqlTransaction:错误:当前事务中止,命令被忽略,直到事务 block 结束:SET search_path TO "public",这不是实际错误。 Postgres 在公寓引发一些错误后引发此异常。

要找出错误是什么,请将 after_create :create_tenant 更改为 after_commit :create_tenant

关于ruby-on-rails - 公寓和设计不创建租户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50224380/

相关文章:

ruby - 在 Linux 上安装 newgem 时出错

arrays - 在postgresql中提取数组的一部分

sql - 删除重复记录,保留一条

ruby-on-rails - Rails 3 - Devise Gem - 如何通过 CRUD 界面管理用户

ruby-on-rails - Bundler 找不到 gem "activerecord"的兼容版本

ruby-on-rails - 如何使用带有 Rails 4 的 rspec 检查记录是否保存在数据库中?

ruby-on-rails - 如何找到 Rails 中链接的正确路径

ruby-on-rails - 关闭 Rails 中 Stripe 订阅按比例分配以全额退款

ruby - minitest断言自定义断言失败

PostgreSQL 8.3 权限未更新 - 用法错误?