database_consistency icon indicating copy to clipboard operation
database_consistency copied to clipboard

Does this gem recognize a find on a column without index?

Open mak-dunkelziffer opened this issue 2 years ago • 4 comments

From having a brief glance at the code, it seems like the gem doesn't analyze calls to #find and #find_by_<attribute>, but I might have missed something.

It would be really valuable to check, whether all those calls have an appropriate DB index on the corresponding column.

mak-dunkelziffer avatar Jan 02 '23 15:01 mak-dunkelziffer

Hi @mak-dunkelziffer,

This would indeed be a great feature! Please point me in the direction of how you think this should be implemented. Is it using some AST parser?

djezzzl avatar Jan 03 '23 07:01 djezzzl

Yes, I guess you would need to parse the whole source code for that. Before you start implementing that yourself, it might be worth to investigate, whether this would be easier to implement as an additional cop for Rubocop. Rubocop already parses everything, but I don't know, whether it is able to then compare the currently parsed file against the indexes from the schema.rb.

mak-dunkelziffer avatar Jan 09 '23 11:01 mak-dunkelziffer

Thank you for the suggestion, @mak-dunkelziffer! I will look into that.

BTW, would you like to contribute?

djezzzl avatar Jan 12 '23 09:01 djezzzl

Not related to the topic, but since we are talking about AST here. It would be nice to have it as a part of DatabaseConsistency because we would finally be able to write autocorrections for checkers that requires model code changes.

Also, some cool GitHub Action would be possible that can point you to the line where you missed validation.

toydestroyer avatar Jan 15 '23 07:01 toydestroyer