rails/rails — pull request #16130 by felixbuenemann (2014-07-11 02:18:16+00:00)
Title: Add support for PostgreSQL index operator classes

This adds support for setting and dumping the index operator class when using the PostgreSQL adapter.
This is especially useful for search, because it is needed to make use of the operators provided by the pg_trgm extension and also the builtin pattern_ops classes that enable LIKE/REGEX queries on non-C-locale columns.

The current code checks that the operator class is a valid opclass provides by the current database, but it does not yet check wether the combination of index type and operator class is valid. This would be possible by using a slightly more comlicated query for determining the valid opclasses:

```SQL
SELECT am.amname AS index_method,
opc.opcname AS opclass_name
FROM pg_am am, pg_opclass opc
WHERE opc.opcmethod = am.oid
ORDER BY index_method, opclass_name;
```

The code is very loosely based on a previous implementation in the postgres_ext 1.0 gem which provides an index_opclass option when working with Rails 3.2.x.

**Usage Examples:**
```ruby
add_index :people, :last_name, opclass: :varchar_pattern_ops
add_index :people, :last_name, using: :gist, opclass: :gist_trgm_ops
```

[Relevant Postgres Docs: Operator Classes and Operator Families](http://www.postgresql.org/docs/9.3/static/indexes-opclass.html)