From 6238a533b00e019af1a5cdb7ffb7d631b0e37373 Mon Sep 17 00:00:00 2001 From: Johan Brandhorst-Satzkorn Date: Wed, 20 Dec 2023 10:38:20 -0800 Subject: [PATCH] internal/credential: 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/credential/query.go | 16 ++++++++-------- internal/credential/static/query.go | 16 ++++++++-------- internal/credential/vault/query.go | 16 ++++++++-------- ...3_credential_static_base_table_updates.up.sql | 4 ++-- ..._credential_library_base_table_updates.up.sql | 4 ++-- ...07_credential_store_base_table_updates.up.sql | 4 ++-- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/internal/credential/query.go b/internal/credential/query.go index 9aa590e9b..9d92ca754 100644 --- a/internal/credential/query.go +++ b/internal/credential/query.go @@ -26,7 +26,7 @@ with stores as ( select public_id from credential_store where %s -- search condition for project IDs is constructed - order by create_time desc, public_id asc + order by create_time desc, public_id desc limit %d ), vault_stores as ( @@ -102,7 +102,7 @@ final as ( ) select * from final -order by create_time desc, public_id asc; +order by create_time desc, public_id desc; ` listStoresPageTemplate = ` @@ -111,7 +111,7 @@ with stores as ( from credential_store where (create_time, public_id) < (@last_item_create_time, @last_item_id) and %s -- search condition for project IDs is constructed - order by create_time desc, public_id asc + order by create_time desc, public_id desc limit %d ), vault_stores as ( @@ -187,7 +187,7 @@ final as ( ) select * from final -order by create_time desc, public_id asc; +order by create_time desc, public_id desc; ` listStoresRefreshTemplate = ` @@ -196,7 +196,7 @@ with stores as ( from credential_store where update_time > @updated_after_time and %s -- search condition for project IDs is constructed - order by update_time desc, public_id asc + order by update_time desc, public_id desc limit %d ), vault_stores as ( @@ -272,7 +272,7 @@ final as ( ) select * from final -order by update_time desc, public_id asc; +order by update_time desc, public_id desc; ` listStoresRefreshPageTemplate = ` @@ -282,7 +282,7 @@ with stores as ( where update_time > @updated_after_time and (update_time, public_id) < (@last_item_update_time, @last_item_id) and %s -- search condition for project IDs is constructed - order by update_time desc, public_id asc + order by update_time desc, public_id desc limit %d ), vault_stores as ( @@ -358,6 +358,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/credential/static/query.go b/internal/credential/static/query.go index 4a6a53b03..787872b86 100644 --- a/internal/credential/static/query.go +++ b/internal/credential/static/query.go @@ -56,7 +56,7 @@ with credentials as ( select public_id from credential_static where store_id = @store_id - order by create_time desc, public_id asc + order by create_time desc, public_id desc limit %d ), json_creds 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; ` listCredentialsPageTemplate = ` @@ -131,7 +131,7 @@ with credentials as ( from credential_static where store_id = @store_id and (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 ), json_creds as ( @@ -197,7 +197,7 @@ final as ( ) select * from final -order by create_time desc, public_id asc; +order by create_time desc, public_id desc; ` listCredentialsRefreshTemplate = ` @@ -206,7 +206,7 @@ with credentials as ( from credential_static where store_id = @store_id and update_time > @updated_after_time - order by update_time desc, public_id asc + order by update_time desc, public_id desc limit %d ), json_creds as ( @@ -272,7 +272,7 @@ final as ( ) select * from final -order by update_time desc, public_id asc; +order by update_time desc, public_id desc; ` listCredentialsRefreshPageTemplate = ` @@ -282,7 +282,7 @@ with credentials as ( where store_id = @store_id and 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 ), json_creds as ( @@ -348,6 +348,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/credential/vault/query.go b/internal/credential/vault/query.go index 5b9801ca7..b65a1c40c 100644 --- a/internal/credential/vault/query.go +++ b/internal/credential/vault/query.go @@ -204,7 +204,7 @@ with libraries as ( select public_id from credential_library where store_id = @store_id - order by create_time desc, public_id asc + order by create_time desc, public_id desc limit %d ), generic_libs as ( @@ -266,7 +266,7 @@ final as ( ) select * from final -order by create_time desc, public_id asc; +order by create_time desc, public_id desc; ` listLibrariesPageTemplate = ` @@ -275,7 +275,7 @@ with libraries as ( from credential_library where store_id = @store_id and (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 ), generic_libs as ( @@ -337,7 +337,7 @@ final as ( ) select * from final -order by create_time desc, public_id asc; +order by create_time desc, public_id desc; ` listLibrariesRefreshTemplate = ` @@ -346,7 +346,7 @@ with libraries as ( from credential_library where store_id = @store_id and update_time > @updated_after_time - order by update_time desc, public_id asc + order by update_time desc, public_id desc limit %d ), generic_libs as ( @@ -408,7 +408,7 @@ final as ( ) select * from final -order by update_time desc, public_id asc; +order by update_time desc, public_id desc; ` listLibrariesRefreshPageTemplate = ` @@ -418,7 +418,7 @@ with libraries as ( where store_id = @store_id and 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 ), generic_libs as ( @@ -480,6 +480,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/03_credential_static_base_table_updates.up.sql b/internal/db/schema/migrations/oss/postgres/80/03_credential_static_base_table_updates.up.sql index 146199139..73fe34c36 100644 --- a/internal/db/schema/migrations/oss/postgres/80/03_credential_static_base_table_updates.up.sql +++ b/internal/db/schema/migrations/oss/postgres/80/03_credential_static_base_table_updates.up.sql @@ -60,9 +60,9 @@ begin; -- Add new indexes for the create and update time queries. create index credential_static_create_time_public_id_idx - on credential_static (create_time desc, public_id asc); + on credential_static (create_time desc, public_id desc); create index credential_static_update_time_public_id_idx - on credential_static (update_time desc, public_id asc); + on credential_static (update_time desc, public_id desc); analyze credential_static; diff --git a/internal/db/schema/migrations/oss/postgres/80/04_credential_library_base_table_updates.up.sql b/internal/db/schema/migrations/oss/postgres/80/04_credential_library_base_table_updates.up.sql index 20f8621ef..b7923cdce 100644 --- a/internal/db/schema/migrations/oss/postgres/80/04_credential_library_base_table_updates.up.sql +++ b/internal/db/schema/migrations/oss/postgres/80/04_credential_library_base_table_updates.up.sql @@ -54,9 +54,9 @@ begin; -- Add new indexes for the create and update time queries. create index credential_library_create_time_public_id_idx - on credential_library (create_time desc, public_id asc); + on credential_library (create_time desc, public_id desc); create index credential_library_update_time_public_id_idx - on credential_library (update_time desc, public_id asc); + on credential_library (update_time desc, public_id desc); analyze credential_library; diff --git a/internal/db/schema/migrations/oss/postgres/80/07_credential_store_base_table_updates.up.sql b/internal/db/schema/migrations/oss/postgres/80/07_credential_store_base_table_updates.up.sql index a963ebcd9..3726588bc 100644 --- a/internal/db/schema/migrations/oss/postgres/80/07_credential_store_base_table_updates.up.sql +++ b/internal/db/schema/migrations/oss/postgres/80/07_credential_store_base_table_updates.up.sql @@ -50,9 +50,9 @@ begin; -- Add new indexes for the create and update time queries. create index credential_store_create_time_public_id_idx - on credential_store (create_time desc, public_id asc); + on credential_store (create_time desc, public_id desc); create index credential_store_update_time_public_id_idx - on credential_store (update_time desc, public_id asc); + on credential_store (update_time desc, public_id desc); analyze credential_store;