active_record_upsert icon indicating copy to clipboard operation
active_record_upsert copied to clipboard

Support explicitly naming constraints

Open andrewclink opened this issue 5 years ago • 5 comments

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 ✅

andrewclink avatar Dec 13 '19 01:12 andrewclink

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"

olleolleolle avatar Jun 30 '20 07:06 olleolleolle

Sorry, that was an auto-generated migration file that slipped through.

andrewclink avatar Jun 30 '20 16:06 andrewclink

@andrewclink Perhaps all that remains is a rebase (to get rid of conflicts)?

olleolleolle avatar Jun 30 '20 17:06 olleolleolle

@andrewclink Perhaps you can rebase and resolve conflicts?

olleolleolle avatar Dec 22 '20 09:12 olleolleolle

Sure, I actually have a few free hours today! Let me take a look.

andrewclink avatar Dec 22 '20 17:12 andrewclink