djezzzl / database_consistency

The tool to avoid various issues due to inconsistencies and inefficiencies between a database schema and application models.
MIT License
1.05k stars 46 forks source link

`MissingUniqueIndexChecker` autocorrect is wrong when column is a function #224

Closed stevegeek closed 1 month ago

stevegeek commented 7 months ago

Imagine a model with validates :name, uniqueness: {case_sensitive: false} (with PG this will use lower(name)).

This will make database_consistency report:

MissingUniqueIndexChecker fail MyModel lower(name) model should have proper unique index in the database

Generating a migration 'autocorrect' creates:

def change
    add_index :my_models, %w[lower(name)], name: :index_my_models_lower_name_, unique: true
end

Running this migration gives:

PG::UndefinedColumn: ERROR: column "lower(name)" does not exist ..._add_my_models_lower_name__index.rb:3:in 'change'

The correct migration is:

add_index :my_models, "lower(name)", name: :index_my_models_lower_name_, unique: true

database_consistency version: 1.7.23 Rails 7.1

Thanks for all your work on this gem!

djezzzl commented 1 month ago

Hi @stevegeek,

Thank you for noticing this! It should be fixed in 1.7.24. Please try it out and let me know if it isn't working.

https://github.com/djezzzl/database_consistency/pull/230

stevegeek commented 1 month ago

@djezzzl thank you!