Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mark
Rails/UniqBeforePluck
cops as unsafe
`Rails/UniqBeforePluck` is unsafe depending on the database collation. Here is an example of an unsafe case for the three databases that Rails supports(MySQL, SQLite, PostgreSQL). ### In the case of MySQL The default collation is `utf8mb4_general_ci`(in Rails), so it is unsafe. An example is as follows. - Create table ``` class CreateUsers < ActiveRecord::Migration[6.0] def change create_table :users do |t| t.string :name end end end ``` - Create data ``` User.create!(name: 'A') User.create!(name: 'a') ``` before autocorrect ``` User.pluck(:name).uniq #=> ["A", "a"] ``` after autocorrect ``` User.distinct.pluck(:name) #=> ["A"] ``` ### In the case of SQLite The default collation is `BINARY`, so it is safe. If you change the collation to `NOCASE`, the collation is unsafe. The following is an example. - Create table ``` class CreateUsers < ActiveRecord::Migration[6.0] def change create_table :users do |t| t.string :name, collation: 'NOCASE' end end end ``` Other than the above, it is the same as MySQL. ### In the case of PostgreSQL The default collation is safe. If you create a custom collation and specify it, the collation will be unsafe. A concrete example is as follows. - Create custom collation ``` CREATE COLLATION case_insensitive ( provider = icu, locale = 'und-u-ks-level2', deterministic = false ); ``` - Create table ``` class CreateUsers < ActiveRecord::Migration[6.0] def change create_table :users do |t| t.string :name, collation: 'case_insensitive' end end end ``` Other than the above, it is the same as MySQL.
- Loading branch information