Skip to content

Deprecate mismatched collation comparison for uniquness validator#35350

Merged
kamipo merged 1 commit intorails:masterfrom
kamipo:deprecate_validate_uniqueness_mismatched_collation
Mar 4, 2019
Merged

Deprecate mismatched collation comparison for uniquness validator#35350
kamipo merged 1 commit intorails:masterfrom
kamipo:deprecate_validate_uniqueness_mismatched_collation

Conversation

@kamipo
Copy link
Member

@kamipo kamipo commented Feb 21, 2019

In MySQL, the default collation is case insensitive. Since the
uniqueness validator enforces case sensitive comparison by default, it
frequently causes mismatched collation issues (performance, weird
behavior, etc) to MySQL users.

https://grosser.it/2009/12/11/validates_uniqness_of-mysql-slow/
#1399
#13465
gitlabhq/gitlabhq@c1dddf8
huginn/huginn#1330 (comment)

I'd like to deprecate the implicit default enforcing since I frequently
experienced the problems in code reviews.

e.g.

Note that this change has no effect to sqlite3, postgresql, and
oracle-enhanced adapters which are implemented as case sensitive by
default, only affect to mysql2 adapter (I can take a work if sqlserver
adapter will support Rails 6.0).

As far as I can see, Basecamp, Shopify, and GitHub are using MySQL (or MariaDB).
Can we make this deprecated in Rails 6.0?

cc @jeremy @rafaelfranca @tenderlove

In MySQL, the default collation is case insensitive. Since the
uniqueness validator enforces case sensitive comparison by default, it
frequently causes mismatched collation issues (performance, weird
behavior, etc) to MySQL users.

https://grosser.it/2009/12/11/validates_uniqness_of-mysql-slow/
rails#1399
rails#13465
gitlabhq/gitlabhq@c1dddf8
huginn/huginn#1330 (comment)

I'd like to deprecate the implicit default enforcing since I frequently
experienced the problems in code reviews.

Note that this change has no effect to sqlite3, postgresql, and
oracle-enhanced adapters which are implemented as case sensitive by
default, only affect to mysql2 adapter (I can take a work if sqlserver
adapter will support Rails 6.0).
@kamipo kamipo force-pushed the deprecate_validate_uniqueness_mismatched_collation branch from 4c5a18d to 9def053 Compare March 4, 2019 12:28

if current_adapter?(:Mysql2Adapter)
assert_equal 2, Topic.where(author_email_address: "david@loudthinking.com").count
assert_equal 2, Topic.where(author_email_address: "David@loudthinking.com").count
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That happening this (break the uniqueness contract) easily without race condition is what I want to prevent, which frequently experienced to me as MySQL DBA.

@kamipo
Copy link
Member Author

kamipo commented Mar 4, 2019

I'm going to merge this, since the case sensitive comparison on the case insensitive column is potentially problematic, the enforcing implicitly would causes any problems silently without being noticed by MySQL users.

If the deprecation message would be seen on the apps using MySQL, I strongly recommend to confirm the query execution plan and data integrity on the database, and to reconsider that the case sensitive comparison on the case insensitive column is whether or not what you want.

@kamipo kamipo merged commit 467ae9c into rails:master Mar 4, 2019
@kamipo kamipo deleted the deprecate_validate_uniqueness_mismatched_collation branch March 4, 2019 13:40
suketa added a commit to suketa/rails_sandbox that referenced this pull request Sep 1, 2019
Deprecate mismatched collation comparison for uniquness validator
rails/rails#35350
suketa added a commit to suketa/rails_sandbox that referenced this pull request Sep 1, 2019
Deprecate mismatched collation comparison for uniquness validator
rails/rails#35350
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants