@@ -269,15 +269,20 @@ def migrate_team_locations(merged_team)
269269 Rails . logger . debug "Migrating team locations..."
270270
271271 source_teams . each do |source_team |
272- source_team . team_locations . find_each do |tl |
273- if TeamLocation . exists? (
274- team_id : merged_team . id ,
275- academic_year : tl . academic_year ,
276- location_id : tl . location_id
277- )
278- tl . destroy!
272+ source_team . team_locations . find_each do |source_tl |
273+ previously_migrated_tl =
274+ TeamLocation . find_by (
275+ team_id : merged_team . id ,
276+ academic_year : source_tl . academic_year ,
277+ location_id : source_tl . location_id
278+ )
279+ if previously_migrated_tl . present?
280+ ConsentForm . where ( team_location_id : source_tl . id ) . update_all (
281+ team_location_id : previously_migrated_tl . id
282+ )
283+ source_tl . destroy!
279284 else
280- tl . update_columns ( team_id : merged_team . id )
285+ source_tl . update_columns ( team_id : merged_team . id )
281286 end
282287 end
283288 end
@@ -341,39 +346,42 @@ def migrate_generic_locations(merged_team)
341346 source_ids = pairs . map ( &:first )
342347 merged_loc = pairs . first . last
343348
344- VaccinationRecord . where ( location_id : source_ids ) . update_all (
345- location_id : merged_loc . id
346- )
349+ [
350+ AttendanceRecord ,
351+ GillickAssessment ,
352+ PreScreening ,
353+ VaccinationRecord
354+ ] . each do |model |
355+ model . where ( location_id : source_ids ) . update_all (
356+ location_id : merged_loc . id
357+ )
358+ end
347359
348- SchoolMove . where ( school_id : source_ids ) . update_all (
349- school_id : merged_loc . id
350- )
360+ [
361+ ConsentForm ,
362+ Patient ,
363+ PatientChangeset ,
364+ SchoolMove ,
365+ SchoolMoveLogEntry
366+ ] . each do |model |
367+ model . where ( school_id : source_ids ) . update_all (
368+ school_id : merged_loc . id
369+ )
370+ end
351371
352- keep_ids =
372+ patient_location_ids_to_keep =
353373 PatientLocation
354374 . where ( location_id : source_ids )
355375 . group ( :patient_id , :academic_year )
356376 . minimum ( :id )
357377 . values
358378 PatientLocation
359379 . where ( location_id : source_ids )
360- . where . not ( id : keep_ids )
380+ . where . not ( id : patient_location_ids_to_keep )
361381 . delete_all
362- PatientLocation . where ( id : keep_ids ) . update_all (
382+ PatientLocation . where ( id : patient_location_ids_to_keep ) . update_all (
363383 location_id : merged_loc . id
364384 )
365-
366- [ AttendanceRecord , GillickAssessment , PreScreening ] . each do |model |
367- model . where ( location_id : source_ids ) . update_all (
368- location_id : merged_loc . id
369- )
370- end
371- ConsentForm . where ( school_id : source_ids ) . update_all (
372- school_id : merged_loc . id
373- )
374- Patient . where ( school_id : source_ids ) . update_all (
375- school_id : merged_loc . id
376- )
377385 end
378386
379387 source_gl_tls . delete_all
@@ -386,8 +394,7 @@ def migrate_generic_locations(merged_team)
386394 end
387395
388396 def refresh_materialized_views
389- ReportingAPI ::Total . refresh! ( concurrently : false )
390- ReportingAPI ::PatientProgrammeStatus . refresh! ( concurrently : false )
397+ ReportingAPI ::RefreshJob . perform_later
391398 rescue StandardError => e
392399 Rails . logger . warn "TeamMerge: could not refresh materialized views: #{ e . message } "
393400 end
0 commit comments