diff --git a/bin/migrate-cocina b/bin/migrate-cocina index 2a0f7d236..bb3cabaae 100755 --- a/bin/migrate-cocina +++ b/bin/migrate-cocina @@ -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. @@ -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