From b3207d8e1c81f13238e4e8d7115a8ea5009c2039 Mon Sep 17 00:00:00 2001 From: Johan Brandhorst-Satzkorn Date: Wed, 20 Dec 2023 11:50:33 -0800 Subject: [PATCH] internal/host: 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 this issue. --- .../postgres/80/10_host_set_table_updates.up.sql | 8 ++++---- internal/host/plugin/query.go | 16 ++++++++-------- internal/host/static/repository_host_set.go | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/internal/db/schema/migrations/oss/postgres/80/10_host_set_table_updates.up.sql b/internal/db/schema/migrations/oss/postgres/80/10_host_set_table_updates.up.sql index ca8f8eabb..0849201ed 100644 --- a/internal/db/schema/migrations/oss/postgres/80/10_host_set_table_updates.up.sql +++ b/internal/db/schema/migrations/oss/postgres/80/10_host_set_table_updates.up.sql @@ -5,17 +5,17 @@ begin; -- Add new indexes for the create time and update time queries. create index host_plugin_set_create_time_public_id_idx - on host_plugin_set (create_time desc, public_id asc); + on host_plugin_set (create_time desc, public_id desc); create index host_plugin_set_update_time_public_id_idx - on host_plugin_set (update_time desc, public_id asc); + on host_plugin_set (update_time desc, public_id desc); analyze host_plugin_set; -- Add new indexes for the create time and update time queries. create index static_host_set_create_time_public_id_idx - on static_host_set (create_time desc, public_id asc); + on static_host_set (create_time desc, public_id desc); create index static_host_set_update_time_public_id_idx - on static_host_set (update_time desc, public_id asc); + on static_host_set (update_time desc, public_id desc); analyze static_host_set; diff --git a/internal/host/plugin/query.go b/internal/host/plugin/query.go index c1a27b4bf..bcdf7ea31 100644 --- a/internal/host/plugin/query.go +++ b/internal/host/plugin/query.go @@ -362,7 +362,7 @@ with host_sets as ( attributes from host_plugin_set where catalog_id = @catalog_id - order by create_time desc, public_id asc + order by create_time desc, public_id desc limit %d ), host_catalog as ( @@ -407,7 +407,7 @@ final as ( ) select * from final -order by create_time desc, public_id asc; +order by create_time desc, public_id desc; ` listSetsPageTemplate = ` @@ -426,7 +426,7 @@ with host_sets as ( from host_plugin_set where catalog_id = @catalog_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 ), host_catalog as ( @@ -471,7 +471,7 @@ final as ( ) select * from final -order by create_time desc, public_id asc; +order by create_time desc, public_id desc; ` listSetsRefreshTemplate = ` @@ -490,7 +490,7 @@ with host_sets as ( from host_plugin_set where catalog_id = @catalog_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 ), host_catalog as ( @@ -535,7 +535,7 @@ final as ( ) select * from final -order by update_time desc, public_id asc; +order by update_time desc, public_id desc; ` listSetsRefreshPageTemplate = ` @@ -555,7 +555,7 @@ with host_sets as ( where catalog_id = @catalog_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 ), host_catalog as ( @@ -600,6 +600,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/host/static/repository_host_set.go b/internal/host/static/repository_host_set.go index 185805b6a..ff1c2690d 100644 --- a/internal/host/static/repository_host_set.go +++ b/internal/host/static/repository_host_set.go @@ -267,7 +267,7 @@ func (r *Repository) listSets(ctx context.Context, catalogId string, opt ...Opti } whereClause := "catalog_id = @catalog_id" args := []any{sql.Named("catalog_id", catalogId)} - order := "create_time desc, public_id asc" + order := "create_time desc, public_id desc" if opts.withStartPageAfterItem != nil { args = append(args, sql.Named("last_item_create_time", opts.withStartPageAfterItem.GetCreateTime()), @@ -301,7 +301,7 @@ func (r *Repository) listSetsRefresh(ctx context.Context, catalogId string, upda sql.Named("catalog_id", catalogId), sql.Named("updated_after_time", updatedAfter), } - order := "update_time desc, public_id asc" + order := "update_time desc, public_id desc" if opts.withStartPageAfterItem != nil { args = append(args, sql.Named("last_item_update_time", opts.withStartPageAfterItem.GetUpdateTime()),