Skip to content

Commit

Permalink
Explicitly truncate updated_at and created_at (#132)
Browse files Browse the repository at this point in the history
* Explicitly truncate updated_at and created_at
* Add truncate to where clause
* Add truncate to checksum_calculated_at
  • Loading branch information
ericaporter authored Apr 22, 2024
1 parent 3dd4d79 commit 7af1a0a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
10 changes: 8 additions & 2 deletions lib/dfe/analytics/services/generic_checksum_calculator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require_relative '../shared/service_pattern'
require_relative '../shared/checksum_query_components'

module DfE
module Analytics
Expand All @@ -8,7 +7,8 @@ module Services
# and order column in a generic database
class GenericChecksumCalculator
include ServicePattern
include ChecksumQueryComponents

WHERE_CLAUSE_ORDER_COLUMNS = %w[CREATED_AT UPDATED_AT].freeze

def initialize(entity, order_column, checksum_calculated_at)
@entity = entity
Expand Down Expand Up @@ -58,6 +58,12 @@ def build_select_and_order_clause(order_column, table_name_sanitized)

[select_clause, order_by_clause]
end

def build_where_clause(order_column, table_name_sanitized, checksum_calculated_at_sanitized)
return '' unless WHERE_CLAUSE_ORDER_COLUMNS.map(&:downcase).include?(order_column.downcase)

"WHERE #{table_name_sanitized}.#{order_column.downcase} < #{checksum_calculated_at_sanitized}"
end
end
end
end
Expand Down
12 changes: 9 additions & 3 deletions lib/dfe/analytics/services/postgres_checksum_calculator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require_relative '../shared/service_pattern'
require_relative '../shared/checksum_query_components'

module DfE
module Analytics
Expand All @@ -8,7 +7,8 @@ module Services
# and order column in a PostgreSQL database
class PostgresChecksumCalculator
include ServicePattern
include ChecksumQueryComponents

WHERE_CLAUSE_ORDER_COLUMNS = %w[CREATED_AT UPDATED_AT].freeze

def initialize(entity, order_column, checksum_calculated_at)
@entity = entity
Expand Down Expand Up @@ -55,12 +55,18 @@ def build_select_and_order_clause(order_column, table_name_sanitized)
end
select_clause = case order_column
when 'UPDATED_AT', 'CREATED_AT'
"#{table_name_sanitized}.#{order_column.downcase} AS \"#{order_alias}\""
"DATE_TRUNC('milliseconds', #{table_name_sanitized}.#{order_column.downcase}) AS \"#{order_alias}\""
else
"#{table_name_sanitized}.id::TEXT AS \"#{order_alias}\""
end
[select_clause, order_alias]
end

def build_where_clause(order_column, table_name_sanitized, checksum_calculated_at_sanitized)
return '' unless WHERE_CLAUSE_ORDER_COLUMNS.map(&:downcase).include?(order_column.downcase)

"WHERE DATE_TRUNC('milliseconds', #{table_name_sanitized}.#{order_column.downcase}) < DATE_TRUNC('milliseconds', '#{checksum_calculated_at_sanitized}')"
end
end
end
end
Expand Down
10 changes: 0 additions & 10 deletions lib/dfe/analytics/shared/checksum_query_components.rb

This file was deleted.

12 changes: 12 additions & 0 deletions spec/dfe/analytics/services/entity_table_checks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,17 @@
})])
end
end

it 'orders records by updated_at truncated to milliseconds' do
time_base = Time.zone.now.beginning_of_minute
Candidate.create(email_address: 'first@example.com', updated_at: time_base + 0.001.seconds)
Candidate.create(email_address: 'second@example.com', updated_at: time_base + 0.005.seconds)

described_class.call(entity_name: candidate_entity, entity_type: entity_type, entity_tag: nil)

ordered_candidates = Candidate.order(:updated_at).pluck(:email_address)

expect(ordered_candidates).to eq(['first@example.com', 'second@example.com'])
end
end
end

0 comments on commit 7af1a0a

Please sign in to comment.