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

Checksum ordered by update #102

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Changes from 2 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
31 changes: 22 additions & 9 deletions lib/dfe/analytics/entity_table_check_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,51 @@ def fetch_current_timestamp_in_time_zone
end

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make a method for checking existence of id column and call this from the perform method loop.

def id_column_exists_for_entity?(entity)
  return true if ActiveRecord::Base.connection.column_exists?(entity, :id)

  Rails.logger.info("DfE::Analytics: Entity checksum: ID column missing in #{entity} - Skipping checks")

  false
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good plan, updates made. Ready for another review - so much complexity in this job now!

def fetch_checksum_data(entity, checksum_calculated_at)
return [0, ''] unless ActiveRecord::Base.connection.column_exists?(entity, :id)

table_name_sanitized = ActiveRecord::Base.connection.quote_table_name(entity)
checksum_calculated_at_sanitized = ActiveRecord::Base.connection.quote(checksum_calculated_at)
order_column = determine_order_column(entity)

if adapter_name == 'postgresql'
fetch_postgresql_checksum_data(table_name_sanitized, checksum_calculated_at_sanitized)
fetch_postgresql_checksum_data(table_name_sanitized, checksum_calculated_at_sanitized, order_column)
else
fetch_generic_checksum_data(table_name_sanitized, checksum_calculated_at_sanitized, order_column)
end
end

def determine_order_column(entity)
if ActiveRecord::Base.connection.column_exists?(entity, :updated_at)
'UPDATED_AT'
elsif ActiveRecord::Base.connection.column_exists?(entity, :created_at)
'CREATED_AT'
else
fetch_generic_checksum_data(table_name_sanitized, checksum_calculated_at_sanitized)
'ID'
end
end

def fetch_postgresql_checksum_data(table_name_sanitized, checksum_calculated_at_sanitized)
def fetch_postgresql_checksum_data(table_name_sanitized, checksum_calculated_at_sanitized, order_column)
checksum_sql_query = <<-SQL
SELECT COUNT(*) as row_count,
MD5(COALESCE(STRING_AGG(CHECKSUM_TABLE.ID, '' ORDER BY CHECKSUM_TABLE.UPDATED_AT ASC), '')) as checksum
MD5(COALESCE(STRING_AGG(CHECKSUM_TABLE.ID, '' ORDER BY CHECKSUM_TABLE.#{order_column} ASC), '')) as checksum
FROM (
SELECT #{table_name_sanitized}.id::TEXT as ID,
#{table_name_sanitized}.updated_at as UPDATED_AT
#{table_name_sanitized}.#{order_column} as #{order_column}
FROM #{table_name_sanitized}
WHERE #{table_name_sanitized}.updated_at < #{checksum_calculated_at_sanitized}
WHERE #{table_name_sanitized}.#{order_column} < #{checksum_calculated_at_sanitized}
) CHECKSUM_TABLE
SQL

result = ActiveRecord::Base.connection.execute(checksum_sql_query).first
[result['row_count'].to_i, result['checksum']]
end

def fetch_generic_checksum_data(table_name_sanitized, checksum_calculated_at_sanitized)
def fetch_generic_checksum_data(table_name_sanitized, checksum_calculated_at_sanitized, order_column)
checksum_sql_query = <<-SQL
SELECT #{table_name_sanitized}.ID
FROM #{table_name_sanitized}
WHERE #{table_name_sanitized}.UPDATED_AT < #{checksum_calculated_at_sanitized}
ORDER BY #{table_name_sanitized}.UPDATED_AT ASC
WHERE #{table_name_sanitized}.#{order_column} < #{checksum_calculated_at_sanitized}
ORDER BY #{table_name_sanitized}.#{order_column} ASC
SQL

table_ids = ActiveRecord::Base.connection.execute(checksum_sql_query).pluck('id')
Expand Down
Loading