/schema_plus_pg_indexes

Adds support in ActiveRecord for PostgreSQL index expressions and operator classes, as well as a shorthand for case-insensitive indexes

Primary LanguageRubyMIT LicenseMIT

Gem Version Build Status Coverage Status Dependency Status

Deprecated for Rails 5.2+

As of ActiveRecord 5.2 all of the functionality provided by this gem is supported in the core ActiveRecord.

This gem can still be added to a Rails 5.2 project to maintain backward compatibility while migrating. However it will not be supported in future Rails versions. The schema_plus_indexes gem can still be used to support the shortcut index syntax.

Migrations can be written as

# instead of expression: 'upper(last_name)'
t.string :last_name
t.index 'upper(last_name)', name: 'index_my_table_on_last_name'

# instead of operator_class: 'varchar_pattern_ops'
t.string :last_name,  index: { opclass: 'varchar_pattern_ops' }

# instead of operator_class {last_name: 'varchar_pattern_ops', address: 'text_pattern_ops' }
t.string :last_name,  index: { with: :address, opclass: {last_name: 'varchar_pattern_ops', address: 'text_pattern_ops' } }

# instead of case_sensitive: false
t.string :last_name
t.index 'lower(last_name)', name: 'index_my_table_on_last_name'

After updating all of your migrations you can replace schema_plus_pg_indexes with schema_plus_indexes to continue to use the shortcut syntax.

schema_plus_pg_indexes

Schema_plus_pg_indexes adds into ActiveRecord support for some additional PostgreSQL index features: expressions, operator classes, and case-insensitive indexes:

t.string :last_name,  index: { expression: 'upper(last_name)' }
t.string :last_name,  index: { operator_class: 'varchar_pattern_ops' }
t.string :last_name,  index: { with: :address, operator_class: {last_name: 'varchar_pattern_ops', address: 'text_pattern_ops' } }
t.string :last_name,  index: { case_sensitive: false }

t.index expression: 'upper(last_name)', name: 'my_index' # no column given, must give a name

Case insensitivity is a shorthand for the expression lower(last_name)

The ActiveRecord::ConnectionAdapters::IndexDefinition object has the corresponding methods defined on it: #expression, #operator_classes and #case_sensitive?

Schema_plus_pg_indexes is part of the SchemaPlus family of Ruby on Rails extension gems.

Installation

As usual:

gem "schema_plus_pg_indexes"                # in a Gemfile
gem.add_dependency "schema_plus_pg_indexes" # in a .gemspec

Deprecations

SchemaPlus 1.8.x provided some options and accessors that are now available in rails 4.2, in slightly different form. SchemaPlusPgIndexes supports the SchemaPlus 1.8.x form but issues deprecation warnings in favor of the rails form:

  • Index definition deprecates these options:

    • :conditions => :where
    • :kind => :using
  • IndexDefinition deprecates accessors:

    • #conditions in favor of #where
    • #kind in favor of #using.to_s

Compatibility

schema_plus_pg_indexes is tested on

  • ruby 2.3.1 with activerecord 5.0, using postgresql
  • ruby 2.3.1 with activerecord 5.1, using postgresql
  • ruby 2.3.1 with activerecord 5.2, using postgresql

Release Notes

  • v0.3.2 - Add Rails 5.2 support and deprecate gem.
  • v0.3.1 - Bug fix: schema dump for complex order clause (#19). Thanks to @joxxoxo.
  • v0.3.0 - Added Rails 5.1.0 support
  • v0.2.1 - Added Rails 5.0.1 support (Removed Rails 5.0.0 support)
  • v0.2.0 - Added Rails 5 support (Removed Rails 4.2 support)
  • v0.1.12 - Missing require
  • v0.1.11 - Explicit gem dependencies
  • v0.1.10 - Upgrade to schmea_plus_core 1.0
  • v0.1.9 - Bug fix: multiple expression indexes (#8)
  • v0.1.8 - Bug fix: expression with operator class (#7)
  • v0.1.7 - Bug fix: mix of columns & expressions (#5)
  • v0.1.6 - Bug fix: operator class & multiple columns (#4). Thanks to @nbudin
  • v0.1.5 - Bug fix: t.index without column in change_table
  • v0.1.1 through v0.1.4 - Conform to schema_dev updates
  • v0.1.0 - Initial release

Development & Testing

Are you interested in contributing to schema_plus_pg_indexes? Thanks! Please follow the standard protocol: fork, feature branch, develop, push, and issue pull request.

Some things to know about to help you develop and test:

  • schema_dev: SchemaPlus::PgIndexes uses schema_dev to facilitate running rspec tests on the matrix of ruby, activerecord, and database versions that the gem supports, both locally and on travis-ci

    To to run rspec locally on the full matrix, do:

      $ schema_dev bundle install
      $ schema_dev rspec
    

    You can also run on just one configuration at a time; For info, see schema_dev --help or the schema_dev README.

    The matrix of configurations is specified in schema_dev.yml in the project root.

  • schema_plus_core: SchemaPlus::PgIndexes uses the SchemaPlus::Core API that provides middleware callback stacks to make it easy to extend ActiveRecord's behavior. If that API is missing something you need for your contribution, please head over to schema_plus_core and open an issue or pull request.
  • schema_monkey: SchemaPlus::PgIndexes is implemented as a schema_monkey client, using schema_monkey's convention-based protocols for extending ActiveRecord and using middleware stacks.