This gem adds trigram index support to Rails' SchemaDumper.
This repo gemifies code from GitLab EE, specifically, this commit.
Currently supports Rails 4.1 and 4.2. Note that Rails 5 now has native support.
Add this line to your application's Gemfile:
gem 'postgram_raidexes', git: 'https://github.com/nulogy/postgram_raidexes'And then execute:
$ bundle
- In a migration, enable the
pg_trgmextension in postgres:
class EnableTrigramExtension < ActiveRecord::Migration
def up
enable_extension :pg_trgm
end
...
end- Add a trigram index to a text field:
def up
add_index "users", ["email"], name: "index_users_on_email_trigram", using: :gin, opclasses: {"email"=>"gin_trgm_ops"}
end
def down
remove_index "users", name: "index_users_on_email_trigram"
end- Run the migration:
$ rake db:migrate
- And you should the
add_indexstatement from Step 2 in yourschema.rb:
add_index "users", ["email"], name: "index_users_on_email_trigram", using: :gin, opclasses: {"email"=>"gin_trgm_ops"}- add to RubyGems
- setup TravisCI
- add note on
DROP EXTENSION pg_trgm CASCADEwhen removing trigram support - support
gistindexes - investigate extending
SchemaDumper(Module.prepend or a Refinement) rather than monkeypatching
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
There is a test application in spec/support/test_app (Rails 4.2) that the current tests run against. It contains a simple User model with a couple simple trigram indexes. The specs migrate this application, then check its db/schema.rb file for the expected add_index statements.
Bug reports and pull requests are welcome on GitHub at https://github.com/nulogy/postgram_raidexes.
The gem is available as open source under the terms of the MIT License.