influitive / apartment

Database multi-tenancy for Rack (and Rails) applications
2.68k stars 460 forks source link

ActiveRecord::StatementInvalid: PG::UndefinedObject: ERROR: operator class "gin_trgm_ops" does not exist for access method "gin" #657

Open CR1AT0RS opened 4 years ago

CR1AT0RS commented 4 years ago

Steps to reproduce

Rails 6 
ruby '~> 2.7.1'

Rails console

Apartment::Tenant.create("demo")
--OUTPUT--
[8] pry(main)> Apartment::Tenant.create("demo")
   (6.4ms)  CREATE SCHEMA "demo"
  SQL (0.3ms)  CREATE EXTENSION IF NOT EXISTS "pg_trgm"
  SQL (0.3ms)  CREATE EXTENSION IF NOT EXISTS "plpgsql"
   (0.2ms)  DROP TABLE IF EXISTS "active_storage_attachments" CASCADE
   (4.3ms)  CREATE TABLE "active_storage_attachments" ("id" bigserial primary key, "name" character varying NOT NULL, "record_type" character varying NOT NULL, "record_id" bigint NOT NULL, "blob_id" bigint NOT NULL, "created_at" timestamp NOT NULL)
   (1.4ms)  CREATE  INDEX  "index_active_storage_attachments_on_blob_id" ON "active_storage_attachments"  ("blob_id")
   (1.7ms)  CREATE UNIQUE INDEX  "index_active_storage_attachments_uniqueness" ON "active_storage_attachments"  ("record_type", "record_id", "name", "blob_id")
   (0.2ms)  DROP TABLE IF EXISTS "active_storage_blobs" CASCADE
   (3.1ms)  CREATE TABLE "active_storage_blobs" ("id" bigserial primary key, "key" character varying NOT NULL, "filename" character varying NOT NULL, "content_type" character varying, "metadata" text, "byte_size" bigint NOT NULL, "checksum" character varying NOT NULL, "created_at" timestamp NOT NULL)
   (1.1ms)  CREATE UNIQUE INDEX  "index_active_storage_blobs_on_key" ON "active_storage_blobs"  ("key")
   (0.2ms)  DROP TABLE IF EXISTS "admin_users" CASCADE
   (3.4ms)  CREATE TABLE "admin_users" ("id" serial NOT NULL PRIMARY KEY, "email" character varying DEFAULT '' NOT NULL, "encrypted_password" character varying DEFAULT '' NOT NULL, "reset_password_token" character varying, "reset_password_sent_at" timestamp, "remember_created_at" timestamp, "sign_in_count" integer DEFAULT 0 NOT NULL, "current_sign_in_at" timestamp, "last_sign_in_at" timestamp, "current_sign_in_ip" inet, "last_sign_in_ip" inet, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
   (1.1ms)  CREATE UNIQUE INDEX  "index_admin_users_on_email" ON "admin_users"  ("email")
   (1.0ms)  CREATE UNIQUE INDEX  "index_admin_users_on_reset_password_token" ON "admin_users"  ("reset_password_token")
   (0.2ms)  DROP TABLE IF EXISTS "delayed_jobs" CASCADE
   (3.1ms)  CREATE TABLE "delayed_jobs" ("id" serial NOT NULL PRIMARY KEY, "priority" integer DEFAULT 0 NOT NULL, "attempts" integer DEFAULT 0 NOT NULL, "handler" text NOT NULL, "last_error" text, "run_at" timestamp, "locked_at" timestamp, "failed_at" timestamp, "locked_by" character varying, "queue" character varying, "created_at" timestamp, "updated_at" timestamp)
   (1.4ms)  CREATE  INDEX  "delayed_jobs_priority" ON "delayed_jobs"  ("priority", "run_at")
   (0.2ms)  DROP TABLE IF EXISTS "exception_hunter_error_groups" CASCADE
   (3.2ms)  CREATE TABLE "exception_hunter_error_groups" ("id" bigserial primary key, "error_class_name" character varying NOT NULL, "message" character varying, "status" integer DEFAULT 0, "tags" text[] DEFAULT '{}', "created_at" timestamp(6) NOT NULL, "updated_at" timestamp(6) NOT NULL)
   (0.7ms)  CREATE  INDEX  "index_exception_hunter_error_groups_on_message" ON "exception_hunter_error_groups" USING gin ("message" gin_trgm_ops)
ActiveRecord::StatementInvalid: PG::UndefinedObject: ERROR:  operator class "gin_trgm_ops" does not exist for access method "gin"

from /Users/xxxxx/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `async_exec'
Caused by PG::UndefinedObject: ERROR:  operator class "gin_trgm_ops" does not exist for access method "gin"

from /Users/xxxxx/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `async_exec'

Expected behavior

Create Tenant named "demo"

Actual behavior


ActiveRecord::StatementInvalid: PG::UndefinedObject: ERROR:  operator class "gin_trgm_ops" does not exist for access method "gin"

from /Users/xxxxx/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `async_exec'
Caused by PG::UndefinedObject: ERROR:  operator class "gin_trgm_ops" does not exist for access method "gin"

from /Users/xxxxx/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `async_exec'

System configuration

#

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{ Landlord }

config.tenant_names = lambda { User.pluck :subdomain }

end

Rails.application.config.middleware.use Apartment::Elevators::Subdomain

Rails.application.config.middleware.use Apartment::Elevators::FirstSubdomain

Rails.application.config.middleware.use Apartment::Elevators::Host


  * `use_schemas`: (`true` or `false`)

use_schemas: false


* Rails (or ActiveRecord) version:

6.0

* Ruby version:

2.7.1

CR1AT0RS commented 4 years ago

I also tried this within the database that I am using for this rails app:

CREATE EXTENSION pg_trgm; ERROR: extension 'pg_trgm' already exists

No luck same error.

bglimepoint commented 2 weeks ago

From our experience, adding gin_trgm_ops to pg_excluded_names in our config/initializers/apartment.rb fixed a very similar looking issue :-)