Schema_plus_index adds various convenient capabilities to ActiveRecord
's index handling:
-
Adds shorthands to the
:index
option in migrationscreate_table :parts do |t| t.string :role, index: true # shorthand for index: {} t.string :product_code, index: :unique # shorthand for index: { unique: true } t.string :first_name t.string :last_name, index: { with: :first_name } # multi-column index t.string :country_code t.string :area_code t.string :local_number, index: { with: [:country_code, :area_code] } # multi-column index end
Of course options can be combined, such as
index: { with: :first_name, unique: true, name: "my_index"}
-
Ensures that the
:index
option is respected byMigration.add_column
and inMigration.change_table
-
Adds
:if_exists
option toActiveRecord::Migration.remove_index
-
Provides consistent behavior regarding attempted duplicate index creation: Ignore and log a warning. Different versions of Rails with different db adapters otherwise behave inconsistently: some ignore the attempt, some raise an error.
-
Model.indexes
returns the indexes defined for theActiveRecord
model. Shorthand forconnection.indexes(Model.table_name)
; the value is cached until the next timeModel.reset_column_information
is called -
In the schema dump
schema.rb
, index definitions are included within thecreate_table
statements rather than added afterwards -
When using SQLite3, makes sure that the definitions returned by
connection.indexes
properly include the column orders (:asc
or:desc
)
schema_plus_indexes is part of the SchemaPlus family of Ruby on Rails extension gems.
In your application's Gemfile
gem "schema_plus_indexes"
schema_plus_indexes is tested on
- ruby 1.9.3 with rails 4.2, using mysql2, sqlite3 or postgresql
- ruby 2.1.5 with rails 4.2, using mysql2, sqlite3 or postgresql
- Initial release
Are you interested in contributing to schema_plus_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: schema_plus_indexes uses schema_dev to facilitate running rspec tests on the matrix of ruby, rails, 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_monkey: schema_plus_indexes extends ActiveRecord using schema_monkey's extension API and protocols -- see its README for details. If your contribution needs any additional monkey patching that isn't already supported by schema_monkey, please head over there and submit a PR.