Квартира и Дизайн не создают арендатора - PullRequest
0 голосов
/ 08 мая 2018

Привет, я впервые работаю с квартирой, а также 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 Design, в которой я создаю арендатора для user.account

Модели / 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_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" %>

Контроллеры / 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

конфиг / application.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

конфиг / Инициализаторы / 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']

Я уверен, что упустил что-то простое, и я ценю ваше время

UPDATE:

Итак, я обновил модели / 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 на жемчужине квартиры. Это помечено как закрытое, я добавил следующее.

конфиг / Инициализаторы / 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

1 Ответ

0 голосов
/ 08 мая 2018

Проблема в том, что Apartment::Tenant.create('tenant_name') вызывает некоторую ошибку на уровне базы данных, что влияет на будущие транзакции.

PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block : SET search_path TO "public", это не фактическая ошибка. Постгрес поднял это исключение после того, как в квартире возникла ошибка.

Чтобы узнать, что за ошибка меняется after_create :create_tenant с after_commit :create_tenant

...