3

Hi I am working with apartment for the first time following along with https://gorails.com/episodes/multitenancy-with-apartment?autoplay=1 but I am using devise for authentication.

When I go to create a new user and tenant I get the following error

    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

I have a devise User model where I am creating a tenant on the user.account

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_create :create_tenant

  private

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

devise/registrations/new

<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" %>

controllers/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

config/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

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']

I am sure I have missed something simple, abd appreciate your time

UPDATE:

So I updated the 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

You will notice two changes after_create became after_commit as per the advice given by. I then found the https://github.com/influitive/apartment/issues/342 on the apartment gem. It is labelled as closed, I added the following.

config/initializers/apartment.rb

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

Once apartment was able to commit I found the error is created by UUID. My subdomain is created but I hit the below error:

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)

My Schema is as follows:

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
Jeremy Bray
  • 444
  • 5
  • 18

1 Answers1

3

The problem is that Apartment::Tenant.create('tenant_name') is raising some error at database level, thus it effecting the future transactions.

PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block : SET search_path TO "public", this is the not the actual error. Postgres raise this exception after the Apartment raised some error.

To find out what is the error change after_create :create_tenant with after_commit :create_tenant

ayush lodhi
  • 367
  • 1
  • 4
  • 16
  • You are awesome, it's apartment and uuid. Will post an update shortly – Jeremy Bray May 08 '18 at 10:43
  • happy to help :) – ayush lodhi May 08 '18 at 12:02
  • @JeremyBray can you try once with uuid_generate_v4(), i mean replace gen_random_uuid() with uuid_generate_v4() – ayush lodhi May 10 '18 at 10:03
  • @AyushIodhi How would i change that? this is created by enable_extension 'uuid-ossp', enable_extension 'pgcrypto' not a choice I made – Jeremy Bray May 11 '18 at 03:08
  • @JeremyBray did you manage to make it work ? I'm stuch by the same issue `PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist ` – Dorian Jan 19 '20 at 07:34
  • @Dorian, I don't think I did to be honest. But I think if you share your code with me in a question I can probably talk you through it. If you are not to invested in your apartment I could suggest a couple of other gems. – Jeremy Bray Jan 19 '20 at 07:41
  • Actually I've just managed to make it work :) (but I'm interested to other gem suggestions !) Thank you! – Dorian Jan 19 '20 at 08:00