Skip to content

Commit

Permalink
Move save transaction until after object validation
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-collier committed Jun 12, 2024
1 parent 57e665a commit d74db0d
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions bin/migrate-cocina
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,8 @@ def perform_migrate(migrator_class:, obj:, mode:)
current_object_version = obj.head_version.version
obj = open_version(cocina_object: obj, version_description: migrator.version_description, mode:) if migrator.version?

# For active record migrations, we need to wrap the migration in a transaction and save the object instead of
# opening/closing versions
if mode == :commit
obj.transaction do
migrator.migrate
obj.save!
end
else
migrator.migrate # This is where the actual migration happens
end
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 @@ -102,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 d74db0d

Please sign in to comment.