support composite primary keys
The function that was being used to find indexes explicitly ignores primary keys. The change in this PR will manually append the primary key to the list.
This will let us handle duplicate errors for composite primary keys in MySQL.
The code looks ok, however it's been a very long while since I worked with this.
One thing I wonder is that normally you get this library working by setting a validator, e.g:
validates_uniqueness_of :name, scope: :shop_id, rescue_from_duplicate: true
How does that works with composite primary keys? Isn't it a bit weird to set a validator on them? That will generate an extra SELECT on insert, no?
I might be misunderstanding, but won't validates_uniqueness_of do a select before an insert regardless of whether an index exists, or what kind of index might be there?
Are you referring to the extra query that will be run when we call klass.connection.primary_keys? It's true there's a new query there, but only upon hitting the race condition of a matching row being inserted by another thread. So I believe the impact should be minimal.
but won't
validates_uniqueness_ofdo a select before an insert regardless of whether an index exists, or what kind of index might be there?
Yes, I'm questioning wether you already have a uniqueness validator on that composite primary key, or if somehow you expect it to work without?
Yeah, there is a preexisting uniqueness validator that used a composite unique key. We reindexed our table to make the primary key equivalent to that unique key and we saw new ActiveRecord::RecordNotUnique exceptions.