DEV: Remove need for reloading cached summary thanks to Range#max (#23106)

This commit is contained in:
Roman Rizzi 2023-08-15 15:03:42 -03:00 committed by GitHub
parent 5683c90917
commit 7251d89919
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 8 deletions

View File

@ -9,10 +9,14 @@ class TopicSummarySerializer < ApplicationSerializer
def new_posts_since_summary def new_posts_since_summary
# Postgres uses discrete range types for int4range, which means # Postgres uses discrete range types for int4range, which means
# an inclusive [1,2] ranges is stored as [1,3). To work around this # (1..2) is stored as (1...3).
# an provide an accurate count, we do the following: #
range_end = [object.content_range&.end.to_i - 1, 0].max # We use Range#max to work around this, which in the case above always returns 2.
# Be careful with using Range#end here, it could lead to unexpected results as:
#
# (1..2).end => 2
# (1...3).end => 3
object.target.highest_post_number.to_i - range_end object.target.highest_post_number.to_i - object.content_range&.max.to_i
end end
end end

View File

@ -116,10 +116,7 @@ class TopicSummarization
) )
end end
# Calling reload here ensures Postgres' discrete range type is applied. main_summary
# an inclusive [1,2] ranges is stored as [1,3).
# Read more about this here: https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-DISCRETE
main_summary.reload
end end
def build_sha(ids) def build_sha(ids)