-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4881 from sul-dlss/audit-migration
Audit migration
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |