Created
August 1, 2018 03:07
-
-
Save jkraemer/a812cbbd711f98a995009d82c9e6f95b to your computer and use it in GitHub Desktop.
Revisions
-
jkraemer revised this gist
Aug 1, 2018 . No changes.There are no files selected for viewing
-
jkraemer created this gist
Aug 1, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ class CreatePostgresqlTrgmIndex def self.call(model, column, concurrently: true) table = model.table_name column = column.sub(/.*\./, "") puts sql = "CREATE INDEX #{"CONCURRENTLY" if concurrently} index_#{table}_on_#{column}_trgm ON #{table} USING gin (#{column} gin_trgm_ops)" model.connection.execute sql rescue ActiveRecord::StatementInvalid raise $! unless $!.message =~ /PG::Duplicate/ end end namespace :redmine do namespace :pg_trgm do task setup: :environment do begin Project.connection.execute "create extension pg_trgm" rescue ActiveRecord::StatementInvalid # ignore the error if the extension is already set up raise $! unless $!.message =~ /PG::Duplicate/ end [ Changeset, Document, Issue, Project, Message, News, ].each do |model| model.searchable_options[:columns].each do |column| CreatePostgresqlTrgmIndex.(model, column) end end CreatePostgresqlTrgmIndex.(Attachment, "filename") CreatePostgresqlTrgmIndex.(Attachment, "description") CreatePostgresqlTrgmIndex.(WikiPage, "title") CreatePostgresqlTrgmIndex.(WikiContent, "text") end end end