Skip to content

Commit

Permalink
Change box foreign keys to weak references in Govbox::Folder, Govbox:…
Browse files Browse the repository at this point in the history
…:ApiConnection & Update Govbox::DestroyBoxDataJob
  • Loading branch information
luciajanikova committed Sep 20, 2023
1 parent 28ae0e7 commit 899cec7
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 12 deletions.
6 changes: 3 additions & 3 deletions app/jobs/govbox/destroy_box_data_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Govbox
class DestroyBoxDataJob < ApplicationJob
queue_as :default

def perform(box)
Govbox::ApiConnection.find_by(box_id: box.id).destroy
Govbox::Folder.where(box_id: box.id).destroy_all
def perform(box_id)
Govbox::ApiConnection.find_by(box_id: box_id).destroy
Govbox::Folder.where(box_id: box_id).destroy_all
end
end
end
3 changes: 1 addition & 2 deletions app/jobs/govbox/sync_box_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ def find_or_create_folder_with_parent(folder_hash, folders, box)
parent_folder = find_or_create_folder_with_parent(folders[folder_hash['parent_id']], folders, box)
end

folder = Govbox::Folder.find_or_initialize_by(edesk_folder_id: folder_hash['id']).tap do |f|
folder = Govbox::Folder.find_or_initialize_by(edesk_folder_id: folder_hash['id'], box_id: box.id).tap do |f|
f.edesk_folder_id = folder_hash['id']
f.parent_folder = parent_folder
f.name = folder_hash['name']
f.system = folder_hash['system'] || false
f.box = box
f.save!
end

Expand Down
2 changes: 1 addition & 1 deletion app/lib/event_bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ def self.reset!
->(message_thread_tag) { Searchable::ReindexMessageThreadJob.perform_later(message_thread_tag.message_thread) }
EventBus.subscribe :tag_renamed, ->(tag) { Searchable::ReindexMessageThreadsWithTagIdJob.perform_later(tag.id) }
EventBus.subscribe :tag_removed, ->(tag) { Searchable::ReindexMessageThreadsWithTagIdJob.perform_later(tag.id) }
EventBus.subscribe :box_destroyed, ->(box) { Govbox::DestroyBoxDataJob.perform_now(box) }
EventBus.subscribe :box_destroyed, ->(box_id) { Govbox::DestroyBoxDataJob.perform_later(box_id) }
2 changes: 1 addition & 1 deletion app/models/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class Box < ApplicationRecord
has_many :message_threads, through: :folders, extend: MessageThreadsExtensions, dependent: :destroy
has_many :message_drafts_imports, dependent: :destroy

before_destroy ->(box) { EventBus.publish(:box_destroyed, box) }
before_destroy ->(box) { EventBus.publish(:box_destroyed, box.id) }
end

1 change: 0 additions & 1 deletion app/models/govbox/api_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
# updated_at :datetime not null

class Govbox::ApiConnection < ApplicationRecord
belongs_to :box
end
5 changes: 4 additions & 1 deletion app/models/govbox/folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
# updated_at :datetime not null

class Govbox::Folder < ApplicationRecord
belongs_to :box
belongs_to :parent_folder, class_name: 'Govbox::Folder', dependent: :destroy, optional: true
has_many :messages, class_name: 'Govbox::Message', dependent: :destroy
has_many :child_folders, class_name: 'Govbox::Folder', foreign_key: :parent_folder_id, dependent: :destroy

def box
Box.find(box_id)
end

def full_name
parent_folder_id.present? ? "#{parent_folder.full_name}/#{name}" : name
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class UpdateBoxReferenceInGovboxFolders < ActiveRecord::Migration[7.0]
def change
remove_foreign_key :govbox_folders, :boxes
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class UpdateBoxReferenceInGovboxApiConnections < ActiveRecord::Migration[7.0]
def change
remove_foreign_key :govbox_api_connections, :boxes
end
end
4 changes: 1 addition & 3 deletions db/schema.rb

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

0 comments on commit 899cec7

Please sign in to comment.