|
|
|
|
@ -7,7 +7,8 @@ class PopulateCompletedSubmittersAndDocuments < ActiveRecord::Migration[7.2]
|
|
|
|
|
self.table_name = 'submitters'
|
|
|
|
|
|
|
|
|
|
belongs_to :submission, class_name: 'MigrationSubmission'
|
|
|
|
|
has_many :submission_events, class_name: 'MigrationSubmissionEvent', foreign_key: :submitter_id
|
|
|
|
|
has_many :submission_sms_events, -> { where(event_type: %w[send_sms send_2fa_sms]) },
|
|
|
|
|
class_name: 'MigrationSubmissionEvent', foreign_key: :submitter_id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class MigrationSubmission < ApplicationRecord
|
|
|
|
|
@ -27,18 +28,26 @@ class PopulateCompletedSubmittersAndDocuments < ActiveRecord::Migration[7.2]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def up
|
|
|
|
|
completed_submitters = MigrationSubmitter.where.not(completed_at: nil)
|
|
|
|
|
submitters = MigrationSubmitter.where.not(completed_at: nil)
|
|
|
|
|
.preload(:submission, :submission_sms_events)
|
|
|
|
|
|
|
|
|
|
count = submitters.count
|
|
|
|
|
|
|
|
|
|
puts "Updating the database - it might take ~#{(count / 1000 * 3) + 1} seconds" if count > 2000
|
|
|
|
|
|
|
|
|
|
submitters.find_each do |submitter|
|
|
|
|
|
completed_submitter = MigrationCompletedSubmitter.find_or_initialize_by(submitter_id: submitter.id)
|
|
|
|
|
|
|
|
|
|
next if completed_submitter.persisted?
|
|
|
|
|
|
|
|
|
|
completed_submitters.order(created_at: :asc).preload(:submission).find_each do |submitter|
|
|
|
|
|
submission = submitter.submission
|
|
|
|
|
sms_count = submitter.submission_events.where(event_type: %w[send_sms send_2fa_sms]).count
|
|
|
|
|
completed_submitter = MigrationCompletedSubmitter.where(submitter_id: submitter.id).first_or_initialize
|
|
|
|
|
|
|
|
|
|
completed_submitter.assign_attributes(
|
|
|
|
|
submission_id: submitter.submission_id,
|
|
|
|
|
account_id: submission.account_id,
|
|
|
|
|
template_id: submission.template_id,
|
|
|
|
|
source: submission.source,
|
|
|
|
|
sms_count:,
|
|
|
|
|
sms_count: submitter.submission_sms_events.size,
|
|
|
|
|
completed_at: submitter.completed_at,
|
|
|
|
|
created_at: submitter.completed_at,
|
|
|
|
|
updated_at: submitter.completed_at
|
|
|
|
|
@ -47,21 +56,18 @@ class PopulateCompletedSubmittersAndDocuments < ActiveRecord::Migration[7.2]
|
|
|
|
|
completed_submitter.save!
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
ActiveStorage::Attachment.where(record_id: completed_submitters.select(:id),
|
|
|
|
|
record_type: 'Submitter',
|
|
|
|
|
name: 'documents')
|
|
|
|
|
.order(created_at: :asc)
|
|
|
|
|
.find_each do |attachment|
|
|
|
|
|
attachments = ActiveStorage::Attachment.where(record_type: 'Submitter', name: 'documents').preload(:blob)
|
|
|
|
|
|
|
|
|
|
attachments.find_each do |attachment|
|
|
|
|
|
sha256 = attachment.metadata['sha256']
|
|
|
|
|
submitter_id = attachment.record_id
|
|
|
|
|
|
|
|
|
|
next if sha256.blank?
|
|
|
|
|
|
|
|
|
|
completed_document = MigrationCompletedDocument.where(submitter_id:, sha256:).first_or_initialize
|
|
|
|
|
completed_document.assign_attributes(
|
|
|
|
|
created_at: attachment.created_at,
|
|
|
|
|
updated_at: attachment.created_at
|
|
|
|
|
)
|
|
|
|
|
completed_document = MigrationCompletedDocument.find_or_initialize_by(submitter_id: attachment.record_id, sha256:)
|
|
|
|
|
|
|
|
|
|
next if completed_document.persisted?
|
|
|
|
|
|
|
|
|
|
completed_document.assign_attributes(created_at: attachment.created_at, updated_at: attachment.created_at)
|
|
|
|
|
|
|
|
|
|
completed_document.save!
|
|
|
|
|
end
|
|
|
|
|
|