active_record_upsert
active_record_upsert copied to clipboard
Support explicitly naming constraints
This PR allows one to pass an explicit Postgres constraint name to upsert_keys
:
class MyClass < ActiveRecord::Base
upsert_keys constraint: 'my_constraint'
end
This generates SQL like
INSERT INTO ... ON CONFLICT ON CONSTRAINT my_constraint DO UPDATE...
My only question is the proper quoting of the constraint name. On one hand, I wouldn't expect a dev to SQL-inject themselves. An attacker already having access to ActiveRecord::Base class methods probably obviates the need to be concerned with security. However, I'm raising it now if anyone else thinks it needs to be addressed.
Tests included (which requires migrating your test database). All are passing ✅
The CI complains in a step of the migrations on Rails 5.0, 5.1:
ArgumentError: Unknown migration version "5.2"; expected one of "4.2", "5.0", "5.1"
Sorry, that was an auto-generated migration file that slipped through.
@andrewclink Perhaps all that remains is a rebase (to get rid of conflicts)?
@andrewclink Perhaps you can rebase and resolve conflicts?
Sure, I actually have a few free hours today! Let me take a look.