From fc9b26efa7e02c287c036ec01b6f28417f2d2762 Mon Sep 17 00:00:00 2001 From: Johan Brandhorst-Satzkorn Date: Wed, 20 Dec 2023 10:37:49 -0800 Subject: [PATCH] internal/target: 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. --- .../80/02_target_base_table_updates.up.sql | 4 ++-- internal/target/query.go | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/db/schema/migrations/oss/postgres/80/02_target_base_table_updates.up.sql b/internal/db/schema/migrations/oss/postgres/80/02_target_base_table_updates.up.sql index 30f63b6eb9..0c1a47e67f 100644 --- a/internal/db/schema/migrations/oss/postgres/80/02_target_base_table_updates.up.sql +++ b/internal/db/schema/migrations/oss/postgres/80/02_target_base_table_updates.up.sql @@ -36,9 +36,9 @@ begin; -- Add new indexes for the update time queries. create index target_create_time_public_id_idx - on target (create_time desc, public_id asc); + on target (create_time desc, public_id desc); create index target_update_time_public_id_idx - on target (update_time desc, public_id asc); + on target (update_time desc, public_id desc); analyze target; diff --git a/internal/target/query.go b/internal/target/query.go index 0046119d6c..ea1884b748 100644 --- a/internal/target/query.go +++ b/internal/target/query.go @@ -68,7 +68,7 @@ with targets as ( select public_id from target where %s -- search condition for applying permissions is constructed - order by create_time desc, public_id asc + order by create_time desc, public_id desc limit %d ), tcp_targets as ( @@ -122,7 +122,7 @@ final as ( ) select * from final -order by create_time desc, public_id asc; +order by create_time desc, public_id desc; ` listTargetsPageTemplate = ` @@ -131,7 +131,7 @@ with targets as ( from target where (create_time, public_id) < (@last_item_create_time, @last_item_id) and %s -- search condition for applying permissions is constructed - order by create_time desc, public_id asc + order by create_time desc, public_id desc limit %d ), tcp_targets as ( @@ -185,7 +185,7 @@ final as ( ) select * from final -order by create_time desc, public_id asc; +order by create_time desc, public_id desc; ` refreshTargetsTemplate = ` @@ -194,7 +194,7 @@ with targets as ( from target where update_time > @updated_after_time and %s -- search condition for applying permissions is constructed - order by update_time desc, public_id asc + order by update_time desc, public_id desc limit %d ), tcp_targets as ( @@ -248,7 +248,7 @@ final as ( ) select * from final -order by update_time desc, public_id asc; +order by update_time desc, public_id desc; ` refreshTargetsPageTemplate = ` @@ -258,7 +258,7 @@ with targets as ( where update_time > @updated_after_time and (update_time, public_id) < (@last_item_update_time, @last_item_id) and %s -- search condition for applying permissions is constructed - order by update_time desc, public_id asc + order by update_time desc, public_id desc limit %d ), tcp_targets as ( @@ -312,6 +312,6 @@ final as ( ) select * from final -order by update_time desc, public_id asc; +order by update_time desc, public_id desc; ` )