Skip to content

Commit

Permalink
Merge pull request #5079 from sul-dlss/releaseTag-transaction
Browse files Browse the repository at this point in the history
Add a commit mode to migrate-cocina
  • Loading branch information
justinlittman authored Jun 12, 2024
2 parents b4bacc6 + d74db0d commit 21ac887
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/models/repository_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RepositoryObject < ApplicationRecord
class VersionAlreadyOpened < StandardError; end
class VersionNotOpened < StandardError; end

has_many :versions, -> { order(version: :asc) }, class_name: 'RepositoryObjectVersion', dependent: :destroy, inverse_of: 'repository_object'
has_many :versions, -> { order(version: :asc) }, class_name: 'RepositoryObjectVersion', dependent: :destroy, inverse_of: 'repository_object', autosave: true
has_many :user_versions, through: :versions

belongs_to :head_version, class_name: 'RepositoryObjectVersion', optional: true
Expand Down
11 changes: 9 additions & 2 deletions bin/migrate-cocina
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ options = { processes: 4, mode: :dryrun, sample: nil }

parser = OptionParser.new do |option_parser|
option_parser.banner = 'Usage: bin/migrate-cocina MIGRATION_CLASS [options]'
option_parser.on('--mode [MODE]', %i[dryrun migrate verify],
'Migration mode (dryrun, migrate, verify). Default is dryrun')
option_parser.on('--mode [MODE]', %i[commit dryrun migrate verify],
'Migration mode (commit dryrun, migrate, verify). Default is dryrun')
option_parser.on('-pPROCESSES', '--processes PROCESSES', Integer, "Number of processes. Default is #{options[:processes]}.")
option_parser.on('-sSAMPLE', '--sample SAMPLE', Integer, 'Sample size per type, otherwise all objects.')
option_parser.on('-h', '--help', 'Displays help.') do
Expand Down Expand Up @@ -85,6 +85,7 @@ def perform_migrate(migrator_class:, obj:, mode:)
obj = open_version(cocina_object: obj, version_description: migrator.version_description, mode:) if migrator.version?

migrator.migrate # This is where the actual migration happens

updated_cocina_object = obj.head_version.to_cocina_with_metadata # This validates the cocina object
Cocina::ObjectValidator.validate(updated_cocina_object) # This validation is performed by UpdateObjectService.

Expand All @@ -93,6 +94,12 @@ def perform_migrate(migrator_class:, obj:, mode:)
updated_cocina_object = UpdateObjectService.update(updated_cocina_object, skip_open_check: !migrator.version?)
Publish::MetadataTransferService.publish(updated_cocina_object) if migrator.publish?
close_version(cocina_object: updated_cocina_object, version_description: migrator.version_description) unless updated_cocina_object.version == current_object_version
elsif mode == :commit
# For active record migrations, we need to wrap the migration in a transaction and save the object instead of
# opening/closing versions
obj.transaction do
obj.save!
end
end
[obj.id, obj.external_identifier, 'SUCCESS']
rescue Dry::Struct::Error, Cocina::Models::ValidationError, Cocina::ValidationError => e
Expand Down

0 comments on commit 21ac887

Please sign in to comment.