-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upsert #334
Upsert #334
Conversation
So this does not use on conflict. Is that going to be added in a later PR? |
@wontruefree Yep! Ideally it will use |
aa5a9c8
to
cab822b
Compare
@paulcsmith A idea i have for find_or_create is to maybe add posibility to use a block. as example:
Edit: changed to an more luckilized example instead of ruby on rails |
@confact I like that API, but for a couple of reasons it is tricky to get type-safe and to work with param parsing. But I like the idea. I tried a few APIs before and couldn't get something that worked, but I'll think on it and see if we can do something closer to that style. |
@jwoertink I've merged in I'm happy to take some guidance and attempt to tackle finishing this one up myself, too! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stephendolan Overall this looks good. I left a few suggestions to fix. The only major thing is we don't have the block forms of these methods. So we can either add those in, or roll with this as is and add the block forms as a separate PR. I'm good either way.
@jwoertink I opted not to introduce all the block variants in this PR so that we can get it cleaned out of the queue and at least have the basics available for folks in the next release. If we merge this as-is, I can follow up with an issue to document the need for those additional overloads. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel iffy about this. Here's some things going through my mind, they might or might not be worth anything, just what I'm thinking:
- As I commented,
unique_columns
is weird to me because the call site provides data that isn't used in the fetch. It feels disjointed - It's introducing two methods/macros that have to be used together
- We are depending on the query object that we generated
Because we separate out queries and operations I'm hesitant to believe this is the best way to go so I looked into Elixir's Ecto ORM. They don't have a find_or_create
method or an upsert
. Instead, they provide options on their Repo.insert
function to handle conflicts.
https://hexdocs.pm/ecto/Ecto.Repo.html#c:insert/2
Can we talk before merging this?
Yeah, I'm ok with holding off on this. |
I'm definitely cool with a different approach, as long as this doesn't sit for another year untouched 😆 . Having just implemented this manually in two apps, it feels like an area where it'll be easy for end-users of Lucky to make mistakes until we provide the capability. |
Yea, I think it is good to have something like this. I am doing find and check if nil to create now in many places, so it would surely save a lot of code. But the current flow is confusing to me. I am not sure what it is using to find it and what it will update. Is it some of them or all of them? If we find a good way to handle that in a good way, it would be good. But I understand the issue @paulcsmith discussed with the parameters and block, so a ruby on a rails-like method might be hard. Not sure exactly how to fix it. I think it is a good call to wait out and think it through. |
e6fbdea
to
ad5e537
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking nice! Just a few questions, but I dig the direction.
@@ -57,6 +57,10 @@ private class ParamKeySaveOperation < ValueColumnModel::SaveOperation | |||
param_key :custom_param | |||
end | |||
|
|||
private class UpsertUserOperation < User::SaveOperation | |||
upsert_lookup_columns :name, :nickname |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how this reads.
3c95b73
to
3418b9b
Compare
if operation.save | ||
yield operation, operation.record.not_nil! | ||
else | ||
operation.published_save_failed_event |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was moved to SaveOperation#save
so we don't need to remember to call it in multiple places
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great stuff! Overall, the implementation seems fairly simple which is nice. Just a few small wording suggestions, but if you're cool with it, merge away!
37bb770
to
aa8429f
Compare
Closes #298
Needs
upsert_lookup_columns
upsert
andupsert!