Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opclass is not parsed whenever the indexed column in the indexdef is wrapped by quotes #34493

Closed
ThomBian opened this issue Nov 20, 2018 · 0 comments

Comments

@ThomBian
Copy link
Contributor

ThomBian commented Nov 20, 2018

Steps to reproduce

Add a gin index on a colum with a name position.

add_index :players, :position, using: :gin, opclass: :gin_trgm_ops, name: 'gin_index_position_on_players'
add_index :players, :name, using: :gin, opclass: :gin_trgm_ops, name: 'gin_index_name_on_players'

At the moment, I have only encountered the problem on column with position as name.

Expected behavior

This should dump properly the index the schema.rb.
Something like
t.index ["position"], name: "gin_index_position_on_players", opclass: :gin_trgm_ops, using: :gin

Actual behavior

It creates an index wihtout an opclass in the db/schema.rb. Here is what is created:
t.index ["position"], name: "gin_index_position_on_positions", using: :gin

Investigations done on my own

Creating indexes seems fine.
However, while checking the created indexes in db using

select *
from pg_indexes
where tablename = 'players';

The indexdef of the index related to the position column is different. The colum name is wrapped with double quotes.
indexdef:CREATE INDEX gin_index_position_on_players ON public.players USING gin ("position" gin_trgm_ops)

I did not find out why this particular word is wrapped up (other postgres reserved words are not wrapped up, other built-in functions are not wrapped up).

The issue occurs while parsing the indexdef in activerecord-5.2.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:127.

It could be fixed by adding this edge case to the regexp.

Why does this occurs
After deeper explanations, I land on https://www.postgresql.org/docs/current/sql-keywords-appendix.html and it seems that this behavior happens on the non reserved (cannot be function or type) keyword.

System configuration

Rails version: 5.2.1

Ruby version: 2.5.3

@ThomBian ThomBian changed the title opclass is not parsed whenever the indexed column in the indexdef is wrapped by quotes opclass is not parsed whenever the indexed column in the indexdef is wrapped by quotes Nov 20, 2018
ThomBian pushed a commit to ThomBian/rails that referenced this issue Nov 21, 2018
in indexdef to be wrapped up by double quotes

    Fixes rails#34493.

    *Thomas Bianchini*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants