From 447a8ab90834b63d8f2f8cb324a03d5d22d1e716 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 22 Apr 2021 10:31:05 -0400 Subject: [PATCH] Add tsvector index to comments.body_markdown (#13469) * Add tsvector index to comments.body_markdown * Users --> Comments --- ...ody_markdown_tsvector_index_to_comments.rb | 27 +++++++++++++++++++ db/schema.rb | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20210421190438_add_body_markdown_tsvector_index_to_comments.rb diff --git a/db/migrate/20210421190438_add_body_markdown_tsvector_index_to_comments.rb b/db/migrate/20210421190438_add_body_markdown_tsvector_index_to_comments.rb new file mode 100644 index 000000000..aabf38bb1 --- /dev/null +++ b/db/migrate/20210421190438_add_body_markdown_tsvector_index_to_comments.rb @@ -0,0 +1,27 @@ +class AddBodyMarkdownTsvectorIndexToComments < ActiveRecord::Migration[6.1] + disable_ddl_transaction! + + INDEX = "to_tsvector('simple'::regconfig, COALESCE((body_markdown)::text, ''::text))" + private_constant :INDEX + + INDEX_NAME = "index_comments_on_body_markdown_as_tsvector" + private_constant :INDEX_NAME + + def up + return if index_name_exists?(:comments, INDEX_NAME) + + add_index :comments, + INDEX, + using: :gin, + name: INDEX_NAME, + algorithm: :concurrently + end + + def down + return unless index_name_exists?(:comments, INDEX_NAME) + + remove_index :comments, + name: INDEX_NAME, + algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index 265d36d48..d1b7e8c57 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_04_20_135208) do +ActiveRecord::Schema.define(version: 2021_04_21_190438) do # These are extensions that must be enabled in order to support this database enable_extension "citext" @@ -388,6 +388,7 @@ ActiveRecord::Schema.define(version: 2021_04_20_135208) do t.datetime "updated_at", null: false t.bigint "user_id" t.index "digest(body_markdown, 'sha512'::text), user_id, ancestry, commentable_id, commentable_type", name: "index_comments_on_body_markdown_user_ancestry_commentable", unique: true + t.index "to_tsvector('simple'::regconfig, COALESCE(body_markdown, ''::text))", name: "index_comments_on_body_markdown_as_tsvector", using: :gin t.index ["ancestry"], name: "index_comments_on_ancestry" t.index ["ancestry"], name: "index_comments_on_ancestry_trgm", opclass: :gin_trgm_ops, using: :gin t.index ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type"