-
Notifications
You must be signed in to change notification settings - Fork 56
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
Use SQL for Destination{Create,Update,Delete} #3356
Conversation
We can get the count directly without the sub-query. This does change the order of results but the only caller of this function does not care about the order because they are emitted as independent metrics.
Previously we were returning ErrNotFound if the ID did not match any destination, but we don't do that for any other entity, and no tests appear to expect that behaviour. Removing that for now for consistency, but we can re-introduce it by checking the result for the number of updated rows if we want to restore it.
return deleteAll[models.Destination](db, ByIDs(ids)) | ||
} | ||
|
||
return internal.ErrNotFound |
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.
Previously we were returning ErrNotFound
if the ID was not found, but we don't do that for any other types. I suspect we should be consistent with the behaviour.
If we want to return a not found on a missing delete then we can check result.RowsAffected() > 0
from the result returned by tx.Exec
.
// destinationsUpdateTable is used to update the destination. It excludes | ||
// the CreatedAt field, because that field is not part of the input to | ||
// UpdateDestination. | ||
type destinationsUpdateTable models.Destination |
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 feels a bit off, but I can't come up with a better way to do updates either in our current model
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.
It is unfortunate, there are some other options:
- We could
select created_at
and set the value on the struct, before theUPDATE
- We could not use
insert()
and copy out the implementation intoCreateDestination
.
Maybe option 2 is better, I'll see what it looks like
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 guess the problem with 2 is that we have to add CreatedAt
back in for both Get
and List
, which means we end up having to deal with the special case in 3 places, instead of only Update
being a special case.
Let's try it with the current implementation, and see if we run into this problem with any other models.
Summary
This PR converts the three destination write queries to use SQL.
DestinationUpdate
is a bit of a special case. We don't receive the created_at time, so we have to exclude it from the query. I did that by creating a second set of methods forColumns
andValues
. We could also select the row first, but it seems like we should be able to do an update and omit the rows that we don't care to update. If this becomes a more common pattern then maybe we can support that difference in the Table interface somehow.Also updated
CountDestinationsByConnectedVersion
to remove a sub-select.Related Issues
Related to #2415