Adding exception hunter breaks the apartment gem
Bug report:
- Expected Behavior:
- Actual Behavior:
pry(main)> Apartment::Tenant.create('tesla')
(1.0ms) CREATE SCHEMA "tesla" []
SQL (0.2ms) CREATE EXTENSION IF NOT EXISTS "pg_trgm" []
SQL (0.1ms) CREATE EXTENSION IF NOT EXISTS "plpgsql" []
(0.2ms) DROP TABLE IF EXISTS "active_storage_attachments" CASCADE []
(7.2ms) 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.3ms) 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 []
(4.5ms) 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.4ms) CREATE UNIQUE INDEX "index_active_storage_blobs_on_key" ON "active_storage_blobs" ("key") []
(0.3ms) DROP TABLE IF EXISTS "admin_users" CASCADE []
(4.3ms) 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.4ms) CREATE UNIQUE INDEX "index_admin_users_on_email" ON "admin_users" ("email") []
(1.6ms) CREATE UNIQUE INDEX "index_admin_users_on_reset_password_token" ON "admin_users" ("reset_password_token") []
(0.4ms) DROP TABLE IF EXISTS "delayed_jobs" CASCADE []
(6.5ms) 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.7ms) CREATE INDEX "delayed_jobs_priority" ON "delayed_jobs" ("priority", "run_at") []
(0.5ms) DROP TABLE IF EXISTS "exception_hunter_error_groups" CASCADE []
(5.3ms) 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.8ms) 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/narya/.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/narya/.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'
-
Steps to Reproduce:
- install apartment gem: https://github.com/rails-on-services/apartment
- bundle exec rails generate apartment:install
- Bind to user model etc.
- Apartment::Tenant.create('atlas')
-
Version of the repo: latest
-
Ruby and Rails Version: 2.7.1 and 6.0.3
-
Rails Stacktrace: this can be found in the
log/development.logorlog/test.log, if this is applicable.
The problem is that its not able to copy the schema from the exception_hunter gem
I have tried testing this with different rails api projects and all gems except the "exception_hunter_error" table. It breaks on that. I have postgres with "gin_trgm_ops"
Commenting out these in schema.rb resolves the issue:
# create_table "exception_hunter_error_groups", force: :cascade do |t|
# t.string "error_class_name", null: false
# t.string "message"
# t.integer "status", default: 0
# t.text "tags", default: [], array: true
# t.datetime "created_at", precision: 6, null: false
# t.datetime "updated_at", precision: 6, null: false
# t.index ["message"], name: "index_exception_hunter_error_groups_on_message", opclass: :gin_trgm_ops, using: :gin
# t.index ["status"], name: "index_exception_hunter_error_groups_on_status"
# end
# create_table "exception_hunter_errors", force: :cascade do |t|
# t.string "class_name", null: false
# t.string "message"
# t.datetime "occurred_at", null: false
# t.json "environment_data"
# t.json "custom_data"
# t.json "user_data"
# t.string "backtrace", default: [], array: true
# t.bigint "error_group_id"
# t.datetime "created_at", precision: 6, null: false
# t.datetime "updated_at", precision: 6, null: false
# t.index ["error_group_id"], name: "index_exception_hunter_errors_on_error_group_id"
# end
# add_foreign_key "exception_hunter_errors", "exception_hunter_error_groups", column: "error_group_id"
Hey @CR1AT0RS, thanks for reporting this issue!. It doesn't seem to be related to the gem itself but rather to Postgres. Exception Hunter uses the pg_trgm module from Postgres to quickly index error messages and search them by similarity (kind of like Elasticsearch does).
This module should be installed by default but it may not be present on your system, could you tell me which operating system are you using, Postgres' version, and how did you install it?
You can also check for instructions to enable the module in this issue, it seems you only need to run CREATE EXTENSION pg_trgm; inside a psql session.
@brunvez thanks for responding.
I have the extensions enabled. I am using OSX postgres.app which comes precompiled with the extensions. I am on rails 6.0.3 and ruby 2.7.1. Interestingly if I run the migrations manually to create a new schema it works but when I use the default "apartment" based migration creation it faiils.
This works:
t.index :message, using: :gin
This doesn't work:
t.index :message, opclass: :gin_trgm_ops, using: :gin
Migration name: 20200716171440_create_exception_hunter_error_groups.rb
class CreateExceptionHunterErrorGroups < ActiveRecord::Migration[6.0]
def change
enable_extension :pg_trgm
create_table :exception_hunter_error_groups do |t|
t.string :error_class_name, null: false
t.string :message
t.integer :status, default: 0
t.text :tags, array: true, default: []
t.timestamps
t.index :message, opclass: :gin_trgm_ops, using: :gin
t.index :status
end
end
end