SchemaPlus / schema_plus_pg_indexes

Adds support in ActiveRecord for PostgreSQL index expressions and operator classes, as well as a shorthand for case-insensitive indexes
MIT License
30 stars 13 forks source link

Feature request: support GIN indexes keys of JSONB columns #2

Closed fj closed 9 years ago

fj commented 9 years ago

Postgresql permits indexes on specific keys in JSONB fields. SchemaPlus can handle indices on the whole JSONB column, like any other column, but it doesn't seem to have a syntax (that I could find) for doing so on a specific key.

In Postgresql, this would look like:

CREATE INDEX idx_blog_posts_tags ON blog_posts USING gin ((metadata -> 'tags'));

for a specific-key index, or

CREATE INDEX idx_blog_posts_metadata ON blog_posts USING gin (metadata);

for an index on the whole column.

rhymes commented 9 years ago

@fj this works:

class AddIndexes < ActiveRecord::Migration
  def up
    add_index :blog_posts, using: :gin, expression: "(metadata -> 'tags')", name: 'idx_blog_posts_tags'
  end

  def down
    remove_index :blog_posts, name: :idx_blog_posts_tags
  end
end

You can already achieve the GIN index on the whole column with plain AR, just use using: :gin in the index

fj commented 9 years ago

Agreed, this is now handled already by schema_plus. Closing.