Skip to content

Allow to specify custom attributes to update on conflict for upsert_all method - #35631

Closed
simi wants to merge 2 commits into
rails:masterfrom
simi:upsert_all_updatable_columns
Closed

simi wants to merge 2 commits into
rails:masterfrom
simi:upsert_all_updatable_columns

Conversation

@simi

@simi simi commented Mar 15, 2019 •

Copy link
Copy Markdown
Contributor

This allows to specify attributes to update on conflict. It opens door to handling timestamps updates manually in reasonable transparent way.

now = Time.now
row = { slug: 'new-title', post: 'New post', created_at: now, updated_at: now }
Post.upsert(row1, unique_by: { columns: [:slug]}, update: [:post, :updated_at])

In combination with defaults (I'll try to add this in different PR) mentioned at #35493 (comment) it can provide easy and transparent way to handle timestamps. Anyway I think it should not be default behaviour since methods like update_all doesn't handle this on its own as well.

WDYT @boblail @dhh @kaspth ❓

references #35493 #35494

I'll update documentation once approved.

@simi
simi force-pushed the upsert_all_updatable_columns branch from 5ddb666 to ef250ed Compare March 15, 2019 21:43
@simi
simi force-pushed the upsert_all_updatable_columns branch 2 times, most recently from 5edfee7 to c529390 Compare March 15, 2019 23:06
@simi

simi commented Mar 16, 2019

Copy link
Copy Markdown
Contributor Author

@boblail ping

@simi

simi commented Mar 16, 2019

Copy link
Copy Markdown
Contributor Author

@boblail 👀 requested changes addressed.

@boblail

boblail commented Mar 16, 2019

Copy link
Copy Markdown
Contributor

LGTM!

I'm curious what Rails Members think of the feature and of unifying with #35636!

@simi

simi commented Mar 16, 2019

Copy link
Copy Markdown
Contributor Author

I think update option can work with array of columns or with SQL string (similar to joins for example).

@simi
simi force-pushed the upsert_all_updatable_columns branch from 52ec6f3 to 2c131e9 Compare March 16, 2019 15:39
@kaspth

kaspth commented Mar 31, 2019 •

Copy link
Copy Markdown
Contributor

I'm not liking the extra API exposed here, doesn't match Rails' aesthetic. Let's see where #35636 goes.

@kaspth kaspth closed this Mar 31, 2019
@simi

simi commented Apr 1, 2019

Copy link
Copy Markdown
Contributor Author

@kaspth is it only about the API or doesn't you like option to specify which columns to use on update without need of using SQL fragment? In my eyes that is actually really handy and powerful. I can wait until #35636 is resolved and I'll try to rethink this agian. Anyway thanks for your time you spend on this already.

@kaspth

kaspth commented Apr 1, 2019

Copy link
Copy Markdown
Contributor

I want to do something closer to only/with that I alluded to in #35686.

@simi

simi commented Apr 1, 2019

Copy link
Copy Markdown
Contributor Author

It is hard to follow for me @kaspth since #35686 was closed.

@kaspth

kaspth commented Apr 1, 2019

Copy link
Copy Markdown
Contributor

I meant this part from the description:

Book.upsert_all rows, only: %i( id name author_id )
Book.upsert_all rows, with: "status = GREATEST(books.status, excluded.status)"

Though I'm thinking of putting a halt on more changes for insert_all for Rails 6. I want the relation support, but that's probably it for now for me.

@simi

simi commented Apr 1, 2019 •

Copy link
Copy Markdown
Contributor Author

👍 Ahh, ok. I'll focus on relation support for now. We can reopen and rethink specifying custom attributes to update on conflict once 6 is released and get more time unless next release.

@shaicoleman

Copy link
Copy Markdown

@simi , @kaspth , can you look into reopening this?

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