Class: ProjectUnification::SpecialHandlers::OtuPageLayoutHandler

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

Overview

Handler for OtuPageLayout with name-based duplicate detection.

Phase 1 (migrate): source layouts whose name collides with a target layout have their name replaced with a per-record sentinel string so the bulk SQL update can move them without a uniqueness conflict. They are registered in merge_registry for Phase 2.

Phase 2 (run_cleanup): target.unify(sentinel) reroutes OtuPageLayoutSections from the sentinel to the surviving target layout, then destroys the sentinel. Sections that would create a topic_id conflict on the target layout are dropped (cascade-destroyed with the sentinel) — target's sections take precedence.

Constant Summary collapse

SENTINEL_PREFIX =
'UNIFICATION TO PROJECT'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of OtuPageLayoutHandler.



277
278
279
280
# File 'lib/project_unification/special_handlers.rb', line 277

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.



275
276
277
# File 'lib/project_unification/special_handlers.rb', line 275

def source_project_id
  @source_project_id
end

#target_project_idObject (readonly)

Returns the value of attribute target_project_id.



275
276
277
# File 'lib/project_unification/special_handlers.rb', line 275

def target_project_id
  @target_project_id
end

Instance Method Details

#find_conflictsObject (private)



316
317
318
319
320
321
322
323
# File 'lib/project_unification/special_handlers.rb', line 316

def find_conflicts
  target_names = OtuPageLayout.where(project_id: target_project_id).pluck(:name, :id).to_h

  OtuPageLayout.where(project_id: source_project_id).filter_map do |layout|
    target_id = target_names[layout.name]
    [layout, target_id] if target_id
  end
end

#migrateObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/project_unification/special_handlers.rb', line 282

def migrate
  result = {
    track: :special,
    model: 'OtuPageLayout',
    migrated: 0,
    duplicates_found: [],
    errors: [],
    merge_registry: []
  }

  find_conflicts.each do |source_layout, target_id|
    source_layout.update_columns(
      name: "#{SENTINEL_PREFIX} #{target_project_id} #{source_layout.id}"
    )
    result[:duplicates_found] << { source_id: source_layout.id, target_id: target_id }
    result[:merge_registry] << {
      model: 'OtuPageLayout', renamed_id: source_layout.id, target_id: target_id
    }
  end

  result[:migrated] = OtuPageLayout.where(project_id: source_project_id)
                                   .update_all(project_id: target_project_id)
  result
rescue => e
  {
    track: :special,
    model: 'OtuPageLayout',
    migrated: 0,
    errors: [{ error: e.message, backtrace: e.backtrace.first(3) }]
  }
end