Class: ProjectUnification::SpecialHandlers::ProjectOrganizationHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/project_unification/special_handlers.rb

Overview

Handler for ProjectOrganization with duplicate detection.

ProjectOrganization is an index of "organizations associated with this project". When an organization_id already exists in the target project's project_organizations the source record is redundant, delete it and bulk update the rest.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_project_id:, target_project_id:, options: {}) ⇒ ProjectOrganizationHandler

Returns a new instance of ProjectOrganizationHandler.



160
161
162
163
# File 'lib/project_unification/special_handlers.rb', line 160

def initialize(source_project_id:, target_project_id:, options: {})
  @source_project_id = source_project_id
  @target_project_id = target_project_id
end

Instance Attribute Details

#source_project_idObject (readonly)

Returns the value of attribute source_project_id.



158
159
160
# File 'lib/project_unification/special_handlers.rb', line 158

def source_project_id
  @source_project_id
end

#target_project_idObject (readonly)

Returns the value of attribute target_project_id.



158
159
160
# File 'lib/project_unification/special_handlers.rb', line 158

def target_project_id
  @target_project_id
end

Instance Method Details

#find_conflict_idsObject (private)



191
192
193
194
195
# File 'lib/project_unification/special_handlers.rb', line 191

def find_conflict_ids
  ProjectOrganization.where(project_id: source_project_id)
                     .where(organization_id: ProjectOrganization.where(project_id: target_project_id).select(:organization_id))
                     .pluck(:id)
end

#migrateObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/project_unification/special_handlers.rb', line 165

def migrate
  conflict_ids = find_conflict_ids

  ProjectOrganization.where(id: conflict_ids).delete_all if conflict_ids.any?

  migrated = ProjectOrganization.where(project_id: source_project_id)
                                .update_all(project_id: target_project_id)

  {
    track: :special,
    model: 'ProjectOrganization',
    migrated: migrated,
    duplicates_deleted: conflict_ids.size,
    errors: []
  }
rescue => e
  {
    track: :special,
    model: 'ProjectOrganization',
    migrated: 0,
    errors: [{ error: e.message, backtrace: e.backtrace.first(3) }]
  }
end