Skip to content

Commit 7af1a0a

Browse files
authored
Explicitly truncate updated_at and created_at (#132)
* Explicitly truncate updated_at and created_at * Add truncate to where clause * Add truncate to checksum_calculated_at
1 parent 3dd4d79 commit 7af1a0a

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

lib/dfe/analytics/services/generic_checksum_calculator.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require_relative '../shared/service_pattern'
2-
require_relative '../shared/checksum_query_components'
32

43
module DfE
54
module Analytics
@@ -8,7 +7,8 @@ module Services
87
# and order column in a generic database
98
class GenericChecksumCalculator
109
include ServicePattern
11-
include ChecksumQueryComponents
10+
11+
WHERE_CLAUSE_ORDER_COLUMNS = %w[CREATED_AT UPDATED_AT].freeze
1212

1313
def initialize(entity, order_column, checksum_calculated_at)
1414
@entity = entity
@@ -58,6 +58,12 @@ def build_select_and_order_clause(order_column, table_name_sanitized)
5858

5959
[select_clause, order_by_clause]
6060
end
61+
62+
def build_where_clause(order_column, table_name_sanitized, checksum_calculated_at_sanitized)
63+
return '' unless WHERE_CLAUSE_ORDER_COLUMNS.map(&:downcase).include?(order_column.downcase)
64+
65+
"WHERE #{table_name_sanitized}.#{order_column.downcase} < #{checksum_calculated_at_sanitized}"
66+
end
6167
end
6268
end
6369
end

lib/dfe/analytics/services/postgres_checksum_calculator.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require_relative '../shared/service_pattern'
2-
require_relative '../shared/checksum_query_components'
32

43
module DfE
54
module Analytics
@@ -8,7 +7,8 @@ module Services
87
# and order column in a PostgreSQL database
98
class PostgresChecksumCalculator
109
include ServicePattern
11-
include ChecksumQueryComponents
10+
11+
WHERE_CLAUSE_ORDER_COLUMNS = %w[CREATED_AT UPDATED_AT].freeze
1212

1313
def initialize(entity, order_column, checksum_calculated_at)
1414
@entity = entity
@@ -55,12 +55,18 @@ def build_select_and_order_clause(order_column, table_name_sanitized)
5555
end
5656
select_clause = case order_column
5757
when 'UPDATED_AT', 'CREATED_AT'
58-
"#{table_name_sanitized}.#{order_column.downcase} AS \"#{order_alias}\""
58+
"DATE_TRUNC('milliseconds', #{table_name_sanitized}.#{order_column.downcase}) AS \"#{order_alias}\""
5959
else
6060
"#{table_name_sanitized}.id::TEXT AS \"#{order_alias}\""
6161
end
6262
[select_clause, order_alias]
6363
end
64+
65+
def build_where_clause(order_column, table_name_sanitized, checksum_calculated_at_sanitized)
66+
return '' unless WHERE_CLAUSE_ORDER_COLUMNS.map(&:downcase).include?(order_column.downcase)
67+
68+
"WHERE DATE_TRUNC('milliseconds', #{table_name_sanitized}.#{order_column.downcase}) < DATE_TRUNC('milliseconds', '#{checksum_calculated_at_sanitized}')"
69+
end
6470
end
6571
end
6672
end

lib/dfe/analytics/shared/checksum_query_components.rb

Lines changed: 0 additions & 10 deletions
This file was deleted.

spec/dfe/analytics/services/entity_table_checks_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,5 +215,17 @@
215215
})])
216216
end
217217
end
218+
219+
it 'orders records by updated_at truncated to milliseconds' do
220+
time_base = Time.zone.now.beginning_of_minute
221+
Candidate.create(email_address: 'first@example.com', updated_at: time_base + 0.001.seconds)
222+
Candidate.create(email_address: 'second@example.com', updated_at: time_base + 0.005.seconds)
223+
224+
described_class.call(entity_name: candidate_entity, entity_type: entity_type, entity_tag: nil)
225+
226+
ordered_candidates = Candidate.order(:updated_at).pluck(:email_address)
227+
228+
expect(ordered_candidates).to eq(['first@example.com', 'second@example.com'])
229+
end
218230
end
219231
end

0 commit comments

Comments
 (0)