#NEWLINE# def drop_old_duplicate_entries_from_table( migrate_engine , table_name , use_soft_delete , * uc_column_names ) : #NEWLINE# #INDENT# 'This method is used to drop all old rows that have the same values for\n    columns in uc_columns.\n    ' #NEWLINE# meta = MetaData ( ) #NEWLINE# meta . bind = migrate_engine #NEWLINE# table = Table ( table_name , meta , autoload = True ) #NEWLINE# columns_for_group_by = [ table . c [ name ] for name in uc_column_names ] #NEWLINE# columns_for_select = [ func . max ( table . c . id ) ] #NEWLINE# columns_for_select . extend ( list ( columns_for_group_by ) ) #NEWLINE# duplicated_rows_select = select ( columns_for_select , group_by = columns_for_group_by , having = ( func . count ( table . c . id ) > 1 ) ) #NEWLINE# for row in migrate_engine . execute ( duplicated_rows_select ) : #NEWLINE# #INDENT# delete_condition = ( table . c . id != row [ 0 ] ) #NEWLINE# for name in uc_column_names : #NEWLINE# #INDENT# delete_condition &= ( table . c [ name ] == row [ name ] ) #NEWLINE# #UNINDENT# rows_to_delete_select = select ( [ table . c . id ] ) . where ( delete_condition ) #NEWLINE# for row in migrate_engine . execute ( rows_to_delete_select ) . fetchall ( ) : #NEWLINE# #INDENT# LOG . info ( ( _ ( 'Deleted duplicated row with id: %(id)s from table: %(table)s' ) % dict ( id = row [ 0 ] , table = table_name ) ) ) #NEWLINE# #UNINDENT# if use_soft_delete : #NEWLINE# #INDENT# delete_statement = table . update ( ) . where ( delete_condition ) . values ( { #NEWLINE# #INDENT# 'deleted' : literal_column ( 'id' ) , #NEWLINE# 'updated_at' : literal_column ( 'updated_at' ) , #NEWLINE# 'deleted_at' : timeutils . utcnow ( ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# delete_statement = table . delete ( ) . where ( delete_condition ) #NEWLINE# #UNINDENT# migrate_engine . execute ( delete_statement )