From f9950a7e1cac1f3e64e292b9e4f8b0199d901084 Mon Sep 17 00:00:00 2001 From: Johan Brandhorst-Satzkorn Date: Wed, 20 Dec 2023 10:38:44 -0800 Subject: [PATCH] internal/authtoken: sort public_id descending The previous sort would run the risk of missing items which collide on update or create times because of the compound comparison using strictly less than for both the timestamp and the public_id. Sorting both descending avoids thi issue. --- internal/authtoken/query.go | 16 ++++++++-------- .../80/06_auth_token_base_table_updates.up.sql | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/internal/authtoken/query.go b/internal/authtoken/query.go index 1b416b893..6c5d5c351 100644 --- a/internal/authtoken/query.go +++ b/internal/authtoken/query.go @@ -17,7 +17,7 @@ with auth_tokens as ( expiration_time, status from auth_token - order by create_time desc, public_id asc + order by create_time desc, public_id desc limit %d ), auth_accounts as ( @@ -47,7 +47,7 @@ final as ( ) select * from final -order by create_time desc, public_id asc; +order by create_time desc, public_id desc; ` listAuthTokensPageTemplate = ` with auth_tokens as ( @@ -60,7 +60,7 @@ with auth_tokens as ( status from auth_token where (create_time, public_id) < (@last_item_create_time, @last_item_id) - order by create_time desc, public_id asc + order by create_time desc, public_id desc limit %d ), auth_accounts as ( @@ -90,7 +90,7 @@ final as ( ) select * from final -order by create_time desc, public_id asc; +order by create_time desc, public_id desc; ` refreshAuthTokensTemplate = ` with auth_tokens as ( @@ -103,7 +103,7 @@ with auth_tokens as ( status from auth_token where update_time > @updated_after_time - order by update_time desc, public_id asc + order by update_time desc, public_id desc limit %d ), auth_accounts as ( @@ -133,7 +133,7 @@ final as ( ) select * from final -order by update_time desc, public_id asc; +order by update_time desc, public_id desc; ` refreshAuthTokensPageTemplate = ` with auth_tokens as ( @@ -147,7 +147,7 @@ with auth_tokens as ( from auth_token where update_time > @updated_after_time and (update_time, public_id) < (@last_item_update_time, @last_item_id) - order by update_time desc, public_id asc + order by update_time desc, public_id desc limit %d ), auth_accounts as ( @@ -177,6 +177,6 @@ final as ( ) select * from final -order by update_time desc, public_id asc; +order by update_time desc, public_id desc; ` ) diff --git a/internal/db/schema/migrations/oss/postgres/80/06_auth_token_base_table_updates.up.sql b/internal/db/schema/migrations/oss/postgres/80/06_auth_token_base_table_updates.up.sql index 2d52fef59..b088c67f1 100644 --- a/internal/db/schema/migrations/oss/postgres/80/06_auth_token_base_table_updates.up.sql +++ b/internal/db/schema/migrations/oss/postgres/80/06_auth_token_base_table_updates.up.sql @@ -5,10 +5,10 @@ begin; -- Add new indexes for the create time and update time queries. create index auth_token_create_time_public_id_idx - on auth_token (create_time desc, public_id asc); + on auth_token (create_time desc, public_id desc); create index auth_token_update_time_public_id_idx - on auth_token (update_time desc, public_id asc); + on auth_token (update_time desc, public_id desc); analyze auth_token; -commit; \ No newline at end of file +commit;