Skip to content

Commit

Permalink
Add deferred constraint to unique index & update implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
luciajanikova committed Oct 25, 2024
1 parent f0a7a73 commit fc21ac5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 37 deletions.
33 changes: 12 additions & 21 deletions app/models/govbox/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,28 @@ class Govbox::Message < ApplicationRecord
def self.create_message_with_thread!(govbox_message)
message = nil
message_draft = Upvs::MessageDraft.where(uuid: govbox_message.message_id).joins(:thread).where(thread: { box_id: govbox_message.box.id }).take
tags_to_migrate = message_draft&.tags_to_migrate_to_message

MessageThread.with_advisory_lock!(govbox_message.correlation_id, transaction: true, timeout_seconds: 10) do
message = create_message(govbox_message)

message.thread = govbox_message.box.message_threads.find_or_create_by_merge_uuid!(
message.thread = message_draft.thread if message_draft || govbox_message.box.message_threads.find_or_create_by_merge_uuid!(
box: govbox_message.box,
merge_uuid: govbox_message.correlation_id,
title: message.metadata.dig("delivery_notification", "consignment", "subject").presence || message.title,
delivered_at: govbox_message.delivered_at
)

message_draft&.destroy
message.save!

add_upvs_related_tags(message, govbox_message)
end
create_message_objects(message, govbox_message.payload)

create_message_objects(message, govbox_message.payload)
add_upvs_related_tags(message, govbox_message)
migrate_tags_from_draft(message, message_draft) if message_draft

migrate_tags_from_draft(message, tags_to_migrate) if tags_to_migrate
MessageObject.mark_message_objects_externally_signed(message.objects)

MessageObject.mark_message_objects_externally_signed(message.objects)
message_draft.destroy if message_draft
end

EventBus.publish(:message_thread_created, message.thread) if message.thread.previously_new_record?
EventBus.publish(:message_created, message)
Expand Down Expand Up @@ -146,21 +145,13 @@ def self.add_upvs_related_tags(message, govbox_message)
add_delivery_notification_tag(message) if message.can_be_authorized?
end

def self.migrate_tags_from_draft(message, tags_to_migrate)
tags_to_migrate[:objects].each do |object_data|
object = message.objects.find_by(uuid: object_data[:uuid])
object_data[:tags].each do |object_tag|
object&.assign_tag(object_tag)
end
end

tags_to_migrate[:message].each do |message_tag|
message.tags.add_cascading_tag(message_tag)
def self.migrate_tags_from_draft(message, message_draft)
message_draft.objects.map do |message_draft_object|
message_object = message.objects.find_by(uuid: message_draft_object.uuid)
message_draft_object.tags.signed.each { |tag| message_object.assign_tag(tag) }
end

tags_to_migrate[:thread].each do |thread_tag|
message.thread.assign_tag(thread_tag)
end
(message_draft.tags.simple + message_draft.tags.signed).each { |tag| message.assign_tag(tag) }
end

def self.add_delivery_notification_tag(message)
Expand Down
15 changes: 0 additions & 15 deletions app/models/message_draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,6 @@ def update_content(parameters)
def submit
raise NotImplementedError
end

def tags_to_migrate_to_message
migration_list = {}

migration_list[:objects] = objects.map do |object| {
uuid: object.uuid,
tags: object.tags.signed.to_a
} if object.tags.signed.any?
end.compact
migration_list[:message] = tags.simple.or(tags.signed).to_a
migration_list[:thread] = (thread.tags.simple + thread.tags.signed).to_a

migration_list
end

def draft?
true
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class UpdateIndexOnUuidMessageThreadIdToMessages < ActiveRecord::Migration[7.1]
def change
remove_index :messages, name: 'index_messages_on_uuid_and_message_thread_id'

add_unique_constraint :messages, [:uuid, :message_thread_id], deferrable: :deferred
end
end

3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc21ac5

Please sign in to comment.