scinote-web/db/migrate/20220429083335_generate_step_orderable_relation.rb

34 lines
1,006 B
Ruby
Raw Normal View History

# frozen_string_literal: true
require File.expand_path('app/helpers/database_helper')
class GenerateStepOrderableRelation < ActiveRecord::Migration[6.1]
include DatabaseHelper
def up
Step.find_in_batches(batch_size: 100) do |steps|
steps.each do |step|
position = 0
orderable_elements = []
step.step_texts.each do |text|
orderable_elements << step.step_orderable_elements.new(orderable: text, position: position)
position += 1
end
step.step_tables.each do |table|
orderable_elements << step.step_orderable_elements.new(orderable: table, position: position)
position += 1
end
step.checklists.each do |checklist|
orderable_elements << step.step_orderable_elements.new(orderable: checklist, position: position)
position += 1
end
StepOrderableElement.import(orderable_elements)
end
end
end
def down
StepOrderableElement.destroy_all
end
end