Example #1
0
def get_indicator_table(indicator_config, metadata, override_table_name=None):
    sql_columns = [
        column_to_sql(col) for col in indicator_config.get_columns()
    ]
    table_name = override_table_name or get_table_name(
        indicator_config.domain, indicator_config.table_id)
    columns_by_col_id = {
        col.database_column_name.decode('utf-8')
        for col in indicator_config.get_columns()
    }
    extra_indices = []

    for index in indicator_config.sql_column_indexes:
        if set(index.column_ids).issubset(columns_by_col_id):
            extra_indices.append(
                Index(_custom_index_name(table_name, index.column_ids),
                      *index.column_ids))
        else:
            logger.error(
                f"Invalid index specified on {table_name} ({index.column_ids})"
            )
    constraints = [PrimaryKeyConstraint(*indicator_config.pk_columns)]
    columns_and_indices = sql_columns + extra_indices + constraints
    current_table = metadata.tables.get(table_name)
    if current_table is not None:
        metadata.remove(current_table)
    return sqlalchemy.Table(table_name, metadata, *columns_and_indices)
Example #2
0
def get_indicator_table(indicator_config, metadata, override_table_name=None):
    sql_columns = [column_to_sql(col) for col in indicator_config.get_columns()]
    table_name = override_table_name or get_table_name(indicator_config.domain, indicator_config.table_id)
    columns_by_col_id = {col.database_column_name.decode('utf-8') for col in indicator_config.get_columns()}
    extra_indices = []

    citus_config = indicator_config.sql_settings.citus_config
    if citus_config.distribution_type == 'hash':
        # Create hash index on doc_id for distributed tables
        extra_indices.append(Index(
            _custom_index_name(table_name, ['doc_id']),
            'doc_id',
            postgresql_using='hash'
        ))

    for index in indicator_config.sql_column_indexes:
        if set(index.column_ids).issubset(columns_by_col_id):
            extra_indices.append(Index(
                _custom_index_name(table_name, index.column_ids),
                *index.column_ids
            ))
        else:
            logger.error(f"Invalid index specified on {table_name} ({index.column_ids})")
    constraints = [PrimaryKeyConstraint(*indicator_config.pk_columns)]
    columns_and_indices = sql_columns + extra_indices + constraints
    # todo: needed to add extend_existing=True to support multiple calls to this function for the same table.
    # is that valid?
    return sqlalchemy.Table(
        table_name,
        metadata,
        extend_existing=True,
        *columns_and_indices
    )
Example #3
0
def get_indicator_table(indicator_config, custom_metadata=None):
    sql_columns = [
        column_to_sql(col) for col in indicator_config.get_columns()
    ]
    table_name = get_table_name(indicator_config.domain,
                                indicator_config.table_id)
    columns_by_col_id = {
        col.database_column_name
        for col in indicator_config.get_columns()
    }
    extra_indices = []
    for index in indicator_config.sql_column_indexes:
        if set(index.column_ids).issubset(columns_by_col_id):
            extra_indices.append(
                Index(_custom_index_name(table_name, index.column_ids),
                      *index.column_ids))
        else:
            _assert = soft_assert('{}@{}'.format('jemord', 'dimagi.com'))
            _assert(False, "Invalid index specified on {}".format(table_name))
            break
    columns_and_indices = sql_columns + extra_indices
    # todo: needed to add extend_existing=True to support multiple calls to this function for the same table.
    # is that valid?
    return sqlalchemy.Table(table_name,
                            custom_metadata or metadata,
                            extend_existing=True,
                            *columns_and_indices)
Example #4
0
def get_indicator_table(indicator_config, metadata, override_table_name=None):
    sql_columns = [
        column_to_sql(col) for col in indicator_config.get_columns()
    ]
    table_name = override_table_name or get_table_name(
        indicator_config.domain, indicator_config.table_id)
    columns_by_col_id = {
        col.database_column_name.decode('utf-8')
        for col in indicator_config.get_columns()
    }
    extra_indices = []

    citus_config = indicator_config.sql_settings.citus_config
    if citus_config.distribution_type == 'hash':
        # Create hash index on doc_id for distributed tables
        extra_indices.append(
            Index(_custom_index_name(table_name, ['doc_id']),
                  'doc_id',
                  postgresql_using='hash'))

    for index in indicator_config.sql_column_indexes:
        if set(index.column_ids).issubset(columns_by_col_id):
            extra_indices.append(
                Index(_custom_index_name(table_name, index.column_ids),
                      *index.column_ids))
        else:
            logger.error(
                f"Invalid index specified on {table_name} ({index.column_ids})"
            )
    constraints = [PrimaryKeyConstraint(*indicator_config.pk_columns)]
    columns_and_indices = sql_columns + extra_indices + constraints
    current_table = metadata.tables.get(table_name)
    if current_table is not None:
        metadata.remove(current_table)
    return sqlalchemy.Table(table_name, metadata, *columns_and_indices)
Example #5
0
def get_indicator_table(indicator_config, metadata, override_table_name=None):
    sql_columns = [column_to_sql(col) for col in indicator_config.get_columns()]
    table_name = override_table_name or get_table_name(indicator_config.domain, indicator_config.table_id)
    columns_by_col_id = {col.database_column_name.decode('utf-8') for col in indicator_config.get_columns()}
    extra_indices = []
    for index in indicator_config.sql_column_indexes:
        if set(index.column_ids).issubset(columns_by_col_id):
            extra_indices.append(Index(
                _custom_index_name(table_name, index.column_ids),
                *index.column_ids
            ))
        else:
            _assert = soft_assert('{}@{}'.format('jemord', 'dimagi.com'))
            _assert(False, "Invalid index specified on {}".format(table_name))
            break
    constraints = [PrimaryKeyConstraint(*indicator_config.pk_columns)]
    columns_and_indices = sql_columns + extra_indices + constraints
    # todo: needed to add extend_existing=True to support multiple calls to this function for the same table.
    # is that valid?
    return sqlalchemy.Table(
        table_name,
        metadata,
        extend_existing=True,
        *columns_and_indices
    )
Example #6
0
def get_indicator_table(indicator_config, custom_metadata=None):
    sql_columns = [column_to_sql(col) for col in indicator_config.get_columns()]
    table_name = get_table_name(indicator_config.domain, indicator_config.table_id)
    # todo: needed to add extend_existing=True to support multiple calls to this function for the same table.
    # is that valid?
    return sqlalchemy.Table(
        table_name,
        custom_metadata or metadata,
        extend_existing=True,
        *sql_columns
    )
Example #7
0
def get_indicator_table(indicator_config, custom_metadata=None):
    sql_columns = [column_to_sql(col) for col in indicator_config.get_columns()]
    table_name = get_table_name(indicator_config.domain, indicator_config.table_id)
    # todo: needed to add extend_existing=True to support multiple calls to this function for the same table.
    # is that valid?
    return sqlalchemy.Table(
        table_name,
        custom_metadata or metadata,
        extend_existing=True,
        *sql_columns
    )