<commit_before>class LessonPlanEventMaterialTable < BaseTable
  table_name 'lesson_plan_resources'
  scope { |ids| within_courses(ids) }

  def migrate_batch(batch)
    batch.each do |old|
      new = ::Course::LessonPlan::EventMaterial.new

      migrate(old, new) do
        column :lesson_plan_event_id do
          store.get(V1::LessonPlanEntry.table_name, old.lesson_plan_entry_id)
        end
        column :material_id do
          store.get(V1::Material.table_name, old.obj_id)
        end

        skip_saving_unless_valid
        store.set(model.table_name, old.id, new.id)
      end
    end
  end
end

# Schema
#
# V2:
# create_table "course_lesson_plan_event_materials", force: :cascade do |t|
#   t.integer "lesson_plan_event_id", :null=>false, :index=>{:name=>"fk__course_lesson_plan_event_materials_lesson_plan_event_id"}, :foreign_key=>{:references=>"course_lesson_plan_events", :name=>"fk_course_lesson_plan_event_materials_lesson_plan_event_id", :on_update=>:no_action, :on_delete=>:no_action}
#   t.integer "material_id",          :null=>false, :index=>{:name=>"fk__course_lesson_plan_event_materials_material_id"}, :foreign_key=>{:references=>"course_materials", :name=>"fk_course_lesson_plan_event_materials_material_id", :on_update=>:no_action, :on_delete=>:no_action}
# end
<commit_msg>Handle the case when material_id is nil<commit_after>class LessonPlanEventMaterialTable < BaseTable
  table_name 'lesson_plan_resources'
  scope { |ids| within_courses(ids) }

  def migrate_batch(batch)
    batch.each do |old|
      new = ::Course::LessonPlan::EventMaterial.new

      migrate(old, new) do
        column :lesson_plan_event_id do
          store.get(V1::LessonPlanEntry.table_name, old.lesson_plan_entry_id)
        end
        column :material_id do
          store.get(V1::Material.table_name, old.obj_id)
        end

        if new.material_id.blank?
          # There are records point to the material in other courses, material_id is nil in this case...
          logger.log "Invalid #{old.class} #{old.id}: material_id is nil"
        else
          skip_saving_unless_valid
        end

        store.set(model.table_name, old.id, new.id)
      end
    end
  end
end

# Schema
#
# V2:
# create_table "course_lesson_plan_event_materials", force: :cascade do |t|
#   t.integer "lesson_plan_event_id", :null=>false, :index=>{:name=>"fk__course_lesson_plan_event_materials_lesson_plan_event_id"}, :foreign_key=>{:references=>"course_lesson_plan_events", :name=>"fk_course_lesson_plan_event_materials_lesson_plan_event_id", :on_update=>:no_action, :on_delete=>:no_action}
#   t.integer "material_id",          :null=>false, :index=>{:name=>"fk__course_lesson_plan_event_materials_material_id"}, :foreign_key=>{:references=>"course_materials", :name=>"fk_course_lesson_plan_event_materials_material_id", :on_update=>:no_action, :on_delete=>:no_action}
# end

{'fraction_non_alphanumeric': 0.11707655005195705, 'fraction_numerical': 0.0020782819535850364, 'mean_word_length': 4.73015873015873, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '351507', 'n_tokens_mistral': 1035, 'n_tokens_neox': 1005, 'n_words': 159}