From f8abf8dab3aadabb56d9a256ded8c2a4e71dc669 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Thu, 6 Oct 2022 10:50:26 +0200
Subject: [PATCH] handle school-less programs in cohorts migration

---
 db/migrate/20221003144016_add_school_to_cohorts.rb | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/db/migrate/20221003144016_add_school_to_cohorts.rb b/db/migrate/20221003144016_add_school_to_cohorts.rb
index 251b13f69..c2fee9957 100644
--- a/db/migrate/20221003144016_add_school_to_cohorts.rb
+++ b/db/migrate/20221003144016_add_school_to_cohorts.rb
@@ -2,8 +2,11 @@ class AddSchoolToCohorts < ActiveRecord::Migration[6.1]
   def change
     add_reference :education_cohorts, :school, foreign_key: {to_table: :education_schools}, type: :uuid
     Education::Cohort.all.each do |cohort|
-      cohort.school_id = cohort.program.schools.first.id
-      cohort.save
+      if cohort.program.schools.any?
+        cohort.update(school_id: cohort.program.schools.first.id)
+      else
+        cohort.destroy
+      end
     end
     change_column_null :education_cohorts, :school_id, false
   end
-- 
GitLab