Skip to content

Use SQL for Destination{Create,Update,Delete}#3356

Merged
dnephin merged 5 commits intomainfrom
dnephin/sql-for-destinations
Oct 4, 2022
Merged

Use SQL for Destination{Create,Update,Delete}#3356
dnephin merged 5 commits intomainfrom
dnephin/sql-for-destinations

Conversation

@dnephin
Copy link
Contributor

@dnephin dnephin commented Sep 30, 2022

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 for Columns and Values. 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

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
Copy link
Contributor Author

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
Copy link
Collaborator

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

Copy link
Contributor Author

@dnephin dnephin Oct 3, 2022

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:

  1. We could select created_at and set the value on the struct, before the UPDATE
  2. We could not use insert() and copy out the implementation into CreateDestination.

Maybe option 2 is better, I'll see what it looks like

Copy link
Contributor Author

@dnephin dnephin Oct 3, 2022

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.

@dnephin dnephin merged commit 29d54ca into main Oct 4, 2022
@dnephin dnephin deleted the dnephin/sql-for-destinations branch October 4, 2022 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants