From 60d610eef468848efa6af722755467b881aa29e9 Mon Sep 17 00:00:00 2001 From: luciajanikova <19lucia99@gmail.com> Date: Fri, 20 Oct 2023 13:18:57 +0200 Subject: [PATCH] Fix migrations - consider box when selecting messages & add dependent option to message relations --- app/jobs/govbox/process_message_job.rb | 3 ++- app/models/message.rb | 3 ++- ...0231008053236_add_collapsed_to_messages.rb | 5 ++-- ...255_change_messages_collapsed_attribute.rb | 26 +++++++++++++++++++ .../20231020095657_fix_message_relations.rb | 13 ++++++++++ db/schema.rb | 2 +- 6 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20231020095255_change_messages_collapsed_attribute.rb create mode 100644 db/migrate/20231020095657_fix_message_relations.rb diff --git a/app/jobs/govbox/process_message_job.rb b/app/jobs/govbox/process_message_job.rb index 8d7f031e8..8f523f14b 100644 --- a/app/jobs/govbox/process_message_job.rb +++ b/app/jobs/govbox/process_message_job.rb @@ -33,7 +33,8 @@ def mark_associated_delivery_notification_authorized(govbox_message) .joins(folder: :box).where(folders: { boxes: { id: govbox_message.box.id } }).take if delivery_notification_govbox_message - delivery_notification_message = ::Message.find_by(uuid: delivery_notification_govbox_message.message_id) + delivery_notification_message = ::Message.where(uuid: delivery_notification_govbox_message.message_id) + .joins(thread: :folder).where(folders: { box_id: govbox_message.box.id }).take delivery_notification_message.collapsed = true delivery_notification_message.metadata["authorized"] = true delivery_notification_message.save! diff --git a/app/models/message.rb b/app/models/message.rb index 300d9f083..957f1bc74 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -22,7 +22,8 @@ class Message < ApplicationRecord belongs_to :thread, class_name: 'MessageThread', foreign_key: :message_thread_id belongs_to :author, class_name: 'User', foreign_key: :author_id, optional: true - has_many :message_relations, class_name: 'MessageRelation' + has_many :message_relations, dependent: :destroy + has_many :message_relations_as_related_message, class_name: 'MessageRelation', foreign_key: :related_message_id, dependent: :destroy has_many :related_messages, through: :message_relations has_many :messages_tags, dependent: :destroy has_many :tags, through: :messages_tags diff --git a/db/migrate/20231008053236_add_collapsed_to_messages.rb b/db/migrate/20231008053236_add_collapsed_to_messages.rb index 38c8b787b..d09befd9c 100644 --- a/db/migrate/20231008053236_add_collapsed_to_messages.rb +++ b/db/migrate/20231008053236_add_collapsed_to_messages.rb @@ -9,14 +9,13 @@ def change message.update(collapsed: govbox_message.collapsed?) - delivery_notification_govbox_message = Govbox::Message.where("payload -> 'delivery_notification' -> 'consignment' ->> 'message_id' = ?", govbox_message.message_id).joins(folder: :box).where(folders: { boxes: { id: govbox_message.box.id } }).take + delivery_notification_govbox_message = Govbox::Message.where("payload -> 'delivery_notification' -> 'consignment' ->> 'message_id' = ?", govbox_message.message_id) + .joins(folder: :box).where(folders: { boxes: { id: govbox_message.box.id } }).take if delivery_notification_govbox_message delivery_notification_message = ::Message.find_by(uuid: delivery_notification_govbox_message.message_id) delivery_notification_message.update(collapsed: true) end end - - # change_column :messages, :collapsed, :boolean, null: false, default: false end end diff --git a/db/migrate/20231020095255_change_messages_collapsed_attribute.rb b/db/migrate/20231020095255_change_messages_collapsed_attribute.rb new file mode 100644 index 000000000..5c9633f40 --- /dev/null +++ b/db/migrate/20231020095255_change_messages_collapsed_attribute.rb @@ -0,0 +1,26 @@ +class ChangeMessagesCollapsedAttribute < ActiveRecord::Migration[7.0] + def change + Message.order(:delivered_at).find_each do |message| + if message.is_a?(MessageDraft) + message.update(collapsed: false) + next + end + + govbox_message = Govbox::Message.where(message_id: message.uuid) + .joins(folder: :box).where(folders: { boxes: { id: message.thread.box.id } }).take + + message.update(collapsed: govbox_message.collapsed?) + + delivery_notification_govbox_message = Govbox::Message.where("payload -> 'delivery_notification' -> 'consignment' ->> 'message_id' = ?", govbox_message.message_id) + .joins(folder: :box).where(folders: { boxes: { id: govbox_message.box.id } }).take + + if delivery_notification_govbox_message + delivery_notification_message = ::Message.where(uuid: delivery_notification_govbox_message.message_id) + .joins(thread: :folder).where(folders: { box_id: govbox_message.box.id }).take + delivery_notification_message.update(collapsed: true) + end + end + + change_column :messages, :collapsed, :boolean, null: false, default: false + end +end diff --git a/db/migrate/20231020095657_fix_message_relations.rb b/db/migrate/20231020095657_fix_message_relations.rb new file mode 100644 index 000000000..c45b40f71 --- /dev/null +++ b/db/migrate/20231020095657_fix_message_relations.rb @@ -0,0 +1,13 @@ +class FixMessageRelations < ActiveRecord::Migration[7.0] + def up + MessageRelation.destroy_all + + Message.find_each do |message| + main_message = Message.where(uuid: message.metadata["reference_id"]).joins(thread: :folder).where(folders: { box_id: message.thread.box.id }).take + + main_message.message_relations.find_or_create_by!( + related_message: message, + ) if main_message + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 11f6b2ff7..23f17e89d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_10_17_132154) do +ActiveRecord::Schema[7.0].define(version: 2023_10_20_095657) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql"