ActiveRecord::SimpleIndexName

Shorten index name

Requirements

  • Ruby 2.3+

  • activerecord 5.0+

Supported frameworks

Installation

Add this line to your application’s Gemfile:

gem 'activerecord-simple_index_name'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord-simple_index_name

Usage

When run rake db:migrate (or rake ar:migrate), index name will be automatically short.

Example

create_table :user_stocks do |t|
  t.integer :user_id,    null: false
  t.integer :article_id, null: false
  t.timestamps null: false
end

add_index :user_stocks, [:user_id, :article_id]
  • Index name without activerecord-simple_index_name : index_user_stocks_on_user_id_and_article_id

  • Index name with activerecord-simple_index_name : user_id_and_article_id

Configurations

Usage

ActiveRecord::SimpleIndexName.config.auto_shorten = true

or

ActiveRecord::SimpleIndexName.configure do |config|
  config.auto_shorten = true
end
  • auto_shorten : Whether shorten index name

  • true (default) : all index names are shorten (e.g. user_id_and_article_id)

  • false : all index names are not shorten (e.g. index_user_stocks_on_user_id_and_article_id)

Opt-in/Opt-out

When auto_shorten is enabled, we can disable simple index name in only specified migration. (or when auto_shorten is disabled, we can enable simple index name in only specified migration)

  • ActiveRecord::SimpleIndexName::EnableShorten

  • When include ActiveRecord::SimpleIndexName::EnableShorten in any migration file, enable simple index name

  • ActiveRecord::SimpleIndexName::DisableShorten

  • When include ActiveRecord::SimpleIndexName::DisableShorten in any migration file, disable simple index name

Opt-in Example

class AddCategoryIdToArticles < ActiveRecord::Migration
  include Activerecord::SimpleIndexName::EnableShorten

  def change
    add_column :articles, :category_id, :integer
    add_index :articles, :category_id
  end
end

include ActiveRecord::SimpleIndexName::EnableShorten, use simple index name regardless of whether auto_shorten is true or false

Opt-out Example

class AddArticleIndexToUserStocks < ActiveRecord::Migration
  include ActiveRecord::SimpleIndexName::DisableShorten

  def change
    add_index :user_stocks, :article_id
  end
end

include ActiveRecord::SimpleIndexName::DisableShorten, don’t use simple index name regardless of whether auto_shorten is true or false

Development

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.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at github.com/sue445/activerecord-simple_index_name.

License

The gem is available as open source under the terms of the MIT License.

Special thanks

@kamipo :bow:

Original idea is qiita.com/kamipo/items/6e5a1e238d7cc0611ade (ja)