Skip to content

Instantly share code, notes, and snippets.

@jkraemer
Created August 1, 2018 03:07
Show Gist options
  • Save jkraemer/a812cbbd711f98a995009d82c9e6f95b to your computer and use it in GitHub Desktop.
Save jkraemer/a812cbbd711f98a995009d82c9e6f95b to your computer and use it in GitHub Desktop.
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