(case_ids, db)
Delete duplicate indices on cases only if they point to the same target case and
    have the same identifier and relationship
Delete duplicate indices on cases only if they point to the same target case and
    have the same identifier and relationship
Delete duplicate indices on cases only if they point to the same target case and have the same identifier and relationship
def _delete_duplicate_indices(case_ids, db):
    """Delete duplicate indices on cases only if they point to the same target case and
    have the same identifier and relationship"""
    delete_dupes_sql = """
        DELETE FROM {case_index_table} WHERE id in (
        SELECT id FROM (
          SELECT id, case_id, row_number() OVER (PARTITION BY case_id, identifier, referenced_id, relationship_id)
          FROM {case_index_table} JOIN (SELECT UNNEST(ARRAY['{{case_ids}}']) AS case_id) AS cx USING (case_id)) as indices
        WHERE row_number > 1
        )
    """.format(case_index_table=CommCareCaseIndexSQL._meta.db_table)

    for chunk in chunked(case_ids, 100):
        with CommCareCaseIndexSQL.get_cursor_for_partition_db(db) as cursor:
            delete_sql = delete_dupes_sql.format(case_ids="','".join(chunk))
            log_sql(delete_sql)
            cursor.execute(delete_sql)
def _delete_duplicate_indices ( case_ids , db ) : delete_dupes_sql = """
        DELETE FROM {case_index_table} WHERE id in (
        SELECT id FROM (
          SELECT id, case_id, row_number() OVER (PARTITION BY case_id, identifier, referenced_id, relationship_id)
          FROM {case_index_table} JOIN (SELECT UNNEST(ARRAY['{{case_ids}}']) AS case_id) AS cx USING (case_id)) as indices
        WHERE row_number > 1
        )
    """ . format ( case_index_table = CommCareCaseIndexSQL . _meta . db_table ) for chunk in chunked ( case_ids , 100 ) : with CommCareCaseIndexSQL . get_cursor_for_partition_db ( db ) as cursor : delete_sql = delete_dupes_sql . format ( case_ids = "','" . join ( chunk ) ) log_sql ( delete_sql ) cursor . execute ( delete_sql )