Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly truncate updated_at and created_at #132

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading