add completed submitters and documents
This commit is contained in:
committed by
Pete Matsyburka
parent
c303159c63
commit
e2f930f6f7
@@ -0,0 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class CreateCompletedSubmittersAndDocuments < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
create_table :completed_submitters do |t|
|
||||
t.bigint :submitter_id, null: false
|
||||
t.bigint :submission_id, null: false
|
||||
t.bigint :account_id, null: false, index: true
|
||||
t.bigint :template_id, null: false
|
||||
t.string :source, null: false
|
||||
t.integer :sms_count, null: false
|
||||
t.datetime :completed_at, null: false
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
create_table :completed_documents do |t|
|
||||
t.bigint :submitter_id, null: false, index: true
|
||||
t.string :sha256, null: false, index: true
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,73 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class PopulateCompletedSubmittersAndDocuments < ActiveRecord::Migration[7.2]
|
||||
disable_ddl_transaction
|
||||
|
||||
class MigrationSubmitter < ApplicationRecord
|
||||
self.table_name = 'submitters'
|
||||
|
||||
belongs_to :submission, class_name: 'MigrationSubmission'
|
||||
has_many :submission_events, class_name: 'MigrationSubmissionEvent', foreign_key: :submitter_id
|
||||
end
|
||||
|
||||
class MigrationSubmission < ApplicationRecord
|
||||
self.table_name = 'submissions'
|
||||
end
|
||||
|
||||
class MigrationSubmissionEvent < ApplicationRecord
|
||||
self.table_name = 'submission_events'
|
||||
end
|
||||
|
||||
class MigrationCompletedSubmitter < ApplicationRecord
|
||||
self.table_name = 'completed_submitters'
|
||||
end
|
||||
|
||||
class MigrationCompletedDocument < ApplicationRecord
|
||||
self.table_name = 'completed_documents'
|
||||
end
|
||||
|
||||
def up
|
||||
completed_submitters = MigrationSubmitter.where.not(completed_at: nil)
|
||||
|
||||
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:,
|
||||
completed_at: submitter.completed_at,
|
||||
created_at: submitter.completed_at,
|
||||
updated_at: submitter.completed_at
|
||||
)
|
||||
|
||||
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|
|
||||
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.save!
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
nil
|
||||
end
|
||||
end
|
||||
+23
-1
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[7.1].define(version: 2024_08_20_180922) do
|
||||
ActiveRecord::Schema[7.2].define(version: 2024_10_22_125135) do
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
||||
@@ -89,6 +89,28 @@ ActiveRecord::Schema[7.1].define(version: 2024_08_20_180922) do
|
||||
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
|
||||
end
|
||||
|
||||
create_table "completed_documents", force: :cascade do |t|
|
||||
t.bigint "submitter_id", null: false
|
||||
t.string "sha256", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["sha256"], name: "index_completed_documents_on_sha256"
|
||||
t.index ["submitter_id"], name: "index_completed_documents_on_submitter_id"
|
||||
end
|
||||
|
||||
create_table "completed_submitters", force: :cascade do |t|
|
||||
t.bigint "submitter_id", null: false
|
||||
t.bigint "submission_id", null: false
|
||||
t.bigint "account_id", null: false
|
||||
t.bigint "template_id", null: false
|
||||
t.string "source", null: false
|
||||
t.integer "sms_count", null: false
|
||||
t.datetime "completed_at", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["account_id"], name: "index_completed_submitters_on_account_id"
|
||||
end
|
||||
|
||||
create_table "document_generation_events", force: :cascade do |t|
|
||||
t.bigint "submitter_id", null: false
|
||||
t.string "event_name", null: false
|
||||
|
||||
Reference in New Issue
Block a user