Skip to content

Commit

Permalink
Merge pull request #4881 from sul-dlss/audit-migration
Browse files Browse the repository at this point in the history
Audit migration
  • Loading branch information
justinlittman authored Apr 19, 2024
2 parents 70dd747 + 1d4f026 commit 6f97f6c
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions bin/audit-repository-migration
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# Aduits migration of DRO/Collection/AdminPolicy to RepositoryObject.

require_relative '../config/environment'
require 'tty-progressbar'

# how many druids to process as a single advance unit of the progress bar
def num_for_progress_advance(count)
return 1 if count < 100

count / 100
end

def on_finish(results, progress_bar)
progress_bar.advance(results.size)
end

def tty_progress_bar(count, model)
TTY::ProgressBar.new(
"#{model} [:bar] (:percent (:current/:total), rate: :rate/s, mean rate: :mean_rate/s, :elapsed total, ETA: :eta_time)",
bar_format: :crate,
advance: num_for_progress_advance(count),
total: count
)
end

def audit_one(legacy_model)
model = RepositoryObject.find_by(external_identifier: legacy_model.external_identifier)
[legacy_model.class.name, legacy_model.external_identifier] unless model && legacy_model.version == model.versions.count && Cocina::Models.without_metadata(legacy_model.to_cocina) == Cocina::Models.without_metadata(model.head_version.to_cocina)
end

def audit_all(ar_class:)
druids = ar_class.pluck(:external_identifier)

progress_bar = tty_progress_bar(druids.length, ar_class.name)
progress_bar.start

Parallel.map(druids.each_slice(100),
in_processes: 4,
finish: ->(_, _, results) { on_finish(results, progress_bar) }) do |druids_slice|
druids_slice.map do |druid|
audit_one(ar_class.find_by(external_identifier: druid))
end
end.flatten(1).compact
end

output = [Dro, Collection, AdminPolicy].each_with_object([]) do |klass, log|
log.concat(audit_all(ar_class: klass))
end

puts 'Class,External ID'
output.each do |row|
puts row.join(',')
end

0 comments on commit 6f97f6c

Please sign in to comment.