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.



230
231
232
233
# File 'lib/project_unification/special_handlers.rb', line 230

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.



228
229
230
# File 'lib/project_unification/special_handlers.rb', line 228

def source_project_id
  @source_project_id
end

#target_project_idObject (readonly)

Returns the value of attribute target_project_id.



228
229
230
# File 'lib/project_unification/special_handlers.rb', line 228

def target_project_id
  @target_project_id
end

Instance Method Details

#find_conflictsObject (private)



269
270
271
272
273
274
275
276
# File 'lib/project_unification/special_handlers.rb', line 269

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



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/project_unification/special_handlers.rb', line 235

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