@@ -704,20 +704,21 @@ def cleanup_school_master_rows():
704704 if task_instance :
705705 logger .debug ('Not found running job for school master cleanup task: {}' .format (task_key ))
706706 # Delete all the old records where more than 1 record are in DRAFT/UPDATED_IN_DRAFT or
707- # ROW_STATUS_DRAFT_LOCKED/ROW_STATUS_UPDATED_IN_DRAFT_LOCKED for same School GIGA ID
707+ # ROW_STATUS_DRAFT_LOCKED/ROW_STATUS_UPDATED_IN_DRAFT_LOCKED/ROW_STATUS_DELETED for same School GIGA ID
708708 rows_with_more_than_1_record_in_draft = sources_models .SchoolMasterData .objects .filter (
709709 status__in = [
710710 sources_models .SchoolMasterData .ROW_STATUS_DRAFT ,
711711 sources_models .SchoolMasterData .ROW_STATUS_UPDATED_IN_DRAFT ,
712712 sources_models .SchoolMasterData .ROW_STATUS_DRAFT_LOCKED ,
713713 sources_models .SchoolMasterData .ROW_STATUS_UPDATED_IN_DRAFT_LOCKED ,
714+ sources_models .SchoolMasterData .ROW_STATUS_DELETED ,
714715 ]
715716 ).values ('school_id_giga' , 'country_id' ).annotate (
716717 total_records = Count ('school_id_giga' , distinct = False ),
717718 ).order_by ('-total_records' , 'school_id_giga' , 'country_id' ).filter (total_records__gt = 1 )
718719
719720 logger .debug ('Queryset to get all the old records to delete where more than 1 record are in DRAFT/'
720- 'UPDATED_IN_DRAFT/ROW_STATUS_DRAFT_LOCKED/ROW_STATUS_UPDATED_IN_DRAFT_LOCKED '
721+ 'UPDATED_IN_DRAFT/ROW_STATUS_DRAFT_LOCKED/ROW_STATUS_UPDATED_IN_DRAFT_LOCKED/ROW_STATUS_DELETED '
721722 'for same School GIGA ID: {0}' .format (rows_with_more_than_1_record_in_draft .query ))
722723
723724 for row in rows_with_more_than_1_record_in_draft :
@@ -727,26 +728,48 @@ def cleanup_school_master_rows():
727728 ).order_by ('-created' )[1 :]:
728729 row_to_delete .delete ()
729730 task_instance .info ('Deleted rows where more than 1 record are in DRAFT/'
730- 'UPDATED_IN_DRAFT/ROW_STATUS_DRAFT_LOCKED/ROW_STATUS_UPDATED_IN_DRAFT_LOCKED '
731+ 'UPDATED_IN_DRAFT/ROW_STATUS_DRAFT_LOCKED/ROW_STATUS_UPDATED_IN_DRAFT_LOCKED/ROW_STATUS_DELETED '
731732 'for same School GIGA ID' )
732733
733- # Delete all the old records where more than 1 record are in is_read=True for same School GIGA ID
734+ # At least keep 1 PUBLISHED row for each school in the school master table
735+ rows_with_more_than_1_record_in_published = sources_models .SchoolMasterData .objects .filter (
736+ status = sources_models .SchoolMasterData .ROW_STATUS_PUBLISHED ,
737+ ).values ('school_id_giga' , 'country_id' ).annotate (
738+ total_records = Count ('school_id_giga' , distinct = False ),
739+ ).order_by ('-total_records' ).filter (total_records__gt = 1 )
740+
741+ logger .debug ('Queryset to get all the old records to delete where more than 1 record are in PUBLISHED '
742+ 'for same School GIGA ID: {0}' .format (rows_with_more_than_1_record_in_published .query ))
743+
744+ for row in rows_with_more_than_1_record_in_published :
745+ for row_to_delete in sources_models .SchoolMasterData .objects .filter (
746+ school_id_giga = row ['school_id_giga' ],
747+ country_id = row ['country_id' ],
748+ ).order_by ('-published_at' )[1 :]:
749+ row_to_delete .delete ()
750+ task_instance .info ('Deleted rows where more than 1 record are in PUBLISHED for same School GIGA ID' )
751+
752+ # Delete all the old records where more than 1 record are in is_read=True
753+ # by keeping at least 1 PUBLISHED row for same School GIGA ID
734754 rows_with_more_than_1_record_in_read = sources_models .SchoolMasterData .objects .filter (
735755 is_read = True ,
736756 ).values ('school_id_giga' , 'country_id' ).annotate (
737757 total_records = Count ('school_id_giga' , distinct = False ),
738758 ).order_by ('-total_records' ).filter (total_records__gt = 1 )
739759
740- logger .debug ('Queryset to get all the old records to delete where more than 1 record are in is_read=True '
741- 'for same School GIGA ID: {0}' .format (rows_with_more_than_1_record_in_read .query ))
760+ logger .debug ('Queryset to get all the old records to delete where more than 1 record are in is_read=True'
761+ ' by keeping at least 1 PUBLISHED row for same School GIGA ID: {0}' .format (
762+ rows_with_more_than_1_record_in_read .query ))
742763
743764 for row in rows_with_more_than_1_record_in_read :
744765 for row_to_delete in sources_models .SchoolMasterData .objects .filter (
745766 school_id_giga = row ['school_id_giga' ],
746767 country_id = row ['country_id' ],
747768 ).order_by ('-published_at' )[1 :]:
748- row_to_delete .delete ()
749- task_instance .info ('Deleted rows where more than 1 record are in is_read=True for same School GIGA ID' )
769+ if row_to_delete .status != sources_models .SchoolMasterData .ROW_STATUS_PUBLISHED :
770+ row_to_delete .delete ()
771+ task_instance .info ('Deleted rows where more than 1 record are in is_read=True '
772+ 'by keeping at least 1 PUBLISHED row for same School GIGA ID' )
750773
751774 background_task_utilities .task_on_complete (task_instance )
752775 else :
0 commit comments