(db)
This will add a unique index concurrently and also a table constraint that uses the index.
    The result will be the same as adding a 'unique_together' option in the Django model.
This will add a unique index concurrently and also a table constraint that uses the index.
    The result will be the same as adding a 'unique_together' option in the Django model.
This will add a unique index concurrently and also a table constraint that uses the index . The result will be the same as adding a unique_together option in the Django model .
def _add_unique_constraint_to_case_index_table(db):
    """This will add a unique index concurrently and also a table constraint that uses the index.
    The result will be the same as adding a 'unique_together' option in the Django model.
    """
    create_index_sql = """
        CREATE UNIQUE INDEX CONCURRENTLY {index_name} on {case_index_table} ("case_id", "identifier")
    """.format(
        case_index_table=CommCareCaseIndexSQL._meta.db_table,
        index_name=UNIQIE_INDEX_NAME,
    )

    add_constraint_sql = """
        ALTER TABLE {case_index_table} ADD CONSTRAINT {index_name} UNIQUE USING INDEX {index_name}
    """.format(
        case_index_table=CommCareCaseIndexSQL._meta.db_table,
        index_name=UNIQIE_INDEX_NAME,
    )

    try:
        with CommCareCaseIndexSQL.get_cursor_for_partition_db(db) as cursor:
            log_sql(create_index_sql)
            cursor.execute(create_index_sql)
            log_sql(add_constraint_sql)
            cursor.execute(add_constraint_sql)
    except:
        # if the index creation failed make sure we remove it otherwise we
        # are left with an invalid index
        _drop_index(db, UNIQIE_INDEX_NAME)
        raise
def _add_unique_constraint_to_case_index_table ( db ) : create_index_sql = """
        CREATE UNIQUE INDEX CONCURRENTLY {index_name} on {case_index_table} ("case_id", "identifier")
    """ . format ( case_index_table = CommCareCaseIndexSQL . _meta . db_table , index_name = UNIQIE_INDEX_NAME , ) add_constraint_sql = """
        ALTER TABLE {case_index_table} ADD CONSTRAINT {index_name} UNIQUE USING INDEX {index_name}
    """ . format ( case_index_table = CommCareCaseIndexSQL . _meta . db_table , index_name = UNIQIE_INDEX_NAME , ) try : with CommCareCaseIndexSQL . get_cursor_for_partition_db ( db ) as cursor : log_sql ( create_index_sql ) cursor . execute ( create_index_sql ) log_sql ( add_constraint_sql ) cursor . execute ( add_constraint_sql ) except : # if the index creation failed make sure we remove it otherwise we # are left with an invalid index _drop_index ( db , UNIQIE_INDEX_NAME ) raise