Skip to content

Instantly share code, notes, and snippets.

@jkraemer
Created August 1, 2018 03:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Rake task to create Trigram indizes for a Redmine database (PostgreSQL only)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment