Skip to content
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

feat: Handle pagination cases where after_id does not exist #1947

Merged
merged 6 commits into from
Jun 2, 2022

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jun 1, 2022

Throw an error to the user in these cases

  • Templateversions
  • Workspacebuilds

User pagination does not need it as suspended users still
have rows in the database


I added unit tests to confirm status code Bad Request for these cases. And confirmed the users pagination is ok and works in both the db fake + postgres.

Throw an error to the user in these cases
- Templateversions
- Workspacebuilds

User pagination does not need it as suspended users still
have rows in the database
@Emyrk Emyrk requested a review from dwahler June 1, 2022 15:04
Comment on lines +234 to +236
if len(params.Status) == 0 {
params.Status = []database.UserStatus{database.UserStatusActive}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postgres defaults to an "active" filter.

if paginationParams.AfterID != uuid.Nil {
// See if the record exists first. If the record does not exist, the pagination
// query will not work.
_, err := api.Database.GetTemplateVersionByID(r.Context(), paginationParams.AfterID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this almost entirely solves the problem, but not 100%. It handles the case where the AfterID is bogus (never existed), and where the row was deleted before the current request started, but there's still a possibility that the row is deleted between GetTemplateVersionByID and GetTemplateVersionsByTemplateID.

(Or alternatively, if the DB is using something like RDS with read-replicas, the row could appear to be deleted if the two queries hit different replicas, which would break the assumption of sequential consistency. Not sure if that's something we plan to support.)

I guess even if that race happened, the consequence would just be falling back to the current behavior of returning an empty resultset, so this is still a big improvement.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I was trying to do it in postgres to prevent that tiny window @dwahler, but I don't think you can raise an exception in a query. It has to be a postgres procedure, which is a headache.

I think this is good enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am dumb, @dwahler I'll put both db calls in a transaction.

coderd/workspacebuilds.go Outdated Show resolved Hide resolved
coderd/templateversions.go Outdated Show resolved Hide resolved
coderd/templateversions.go Outdated Show resolved Hide resolved
coderd/templateversions.go Outdated Show resolved Hide resolved
coderd/workspacebuilds.go Outdated Show resolved Hide resolved
@Emyrk
Copy link
Member Author

Emyrk commented Jun 2, 2022

@dwahler I am going to move it to another PR since it was acting strange in CI. I'll put it back in

@Emyrk Emyrk merged commit b9983e4 into main Jun 2, 2022
@Emyrk Emyrk deleted the stevenmasley/pagination_after_id branch June 2, 2022 14:01
kylecarbs pushed a commit that referenced this pull request Jun 10, 2022
* feat: Handle pagination cases where after_id does not exist

Throw an error to the user in these cases
- Templateversions
- Workspacebuilds

User pagination does not need it as suspended users still
have rows in the database
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.

None yet

2 participants