Skip to content

Commit

Permalink
CV2-6141: update opotions if relationship already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
melsawy committed Feb 25, 2025
1 parent d95e6b9 commit ca829cd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/bot/alegre.rb
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ def self.relationship_model_not_allowed(relationship_model)

def self.report_exception_if_bad_relationship(relationship, pm_id_scores, relationship_type)
if relationship.model.nil? || relationship.weight.nil? || relationship.source_field.nil? || relationship.target_field.nil? || self.relationship_model_not_allowed(relationship.model)
CheckSentry.notify(Bot::Alegre::Error.new("[Alegre] Bad relationship was stored without required metadata"), **{trace: Thread.current.backtrace.join("\n"), relationship: relationship.attributes, relationship_type: relationship_type, pm_id_scores: pm_id_scores})
CheckSentry.notify(Bot::Alegre::Error.new("[Alegre] Bad relationship with ID [#{relationship.id}] was stored without required metadata"), **{trace: Thread.current.backtrace.join("\n"), relationship: relationship.attributes, relationship_type: relationship_type, pm_id_scores: pm_id_scores})
end
end

Expand Down
7 changes: 7 additions & 0 deletions app/models/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ def create_or_update_parent_id
def self.create_unless_exists(source_id, target_id, relationship_type, options = {})
# Verify that the target is not part of another source; in this case, we should return the existing relationship.
r = Relationship.where(target_id: target_id).where.not(source_id: source_id).last
# should update options if exists
unless r.nil? || !options.blank?
options.each do |key, value|
r.send("#{key}=", value) if r.respond_to?("#{key}=")
end
r.save!
end
return r unless r.nil?
# otherwise we should get the existing relationship based on source, target and type
r = Relationship.where(source_id: source_id, target_id: target_id).where('relationship_type = ?', relationship_type.to_yaml).last
Expand Down

0 comments on commit ca829cd

Please sign in to comment.