Skip to content

Ordering personal tokens both by expires_at and id

What does this MR do and why?

This MR resolves Issue - #366928 (closed). When Personal Access Tokens are only ordered by expires_at column, the query does not give expected results with limits and offset since expires_at is a non-uniques and nullable field. Hence adding an additional ordering column to have expected results.

Screenshots or screen recordings

In the attached screenshots we can see that the results for two pages are sorted on id since expires_at value is null in all cases Screenshot_2022-07-15_at_3.24.58_PM

Screenshot_2022-07-15_at_3.25.14_PM

How to set up and validate locally

One can check per page results by following the steps below :-

  1. Enable the feature-flag access_token_pagination from rails console
bin/rails console
Feature.enable(:access_token_pagination)
  1. Generate 1000 personal access tokens for the root user (user_id: 1):
gdk psql
DO $$
  BEGIN 
    FOR X IN 1..1000 LOOP
      INSERT INTO "personal_access_tokens" (name, created_at, updated_at, user_id) VALUES (X, '2022-06-02', '2022-06-02', 1);
    END LOOP;
  END;
$$;
  1. Access the api end-point for personal access tokens with page parameter
http://localhost:3000/-/profile/personal_access_tokens.json?page=2

OR the specs that are added are good enough to validate changes are working fine -->

Database

SELECT name FROM personal_access_tokens
WHERE user_id = 1
ORDER BY expires_at ASC, id ASC
LIMIT 10 OFFSET 0;

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Max Orefice

Merge request reports