Ejemplo n.º 1
0
def add_indices(conn, *, tbl_name, idx_metadata, if_not_exists=False):
    """Helper that add indexes to a potentially partitioned table.

    If table is not partitioned, will just add the index to the physical table. Otherwise, will recursively
    go through the child tables until if find non-partitioned tables to add the indices to.
    """
    if not utils.is_table_partitioned(conn, tbl_name):
        _add_indices_to_single_shard(conn, part_name=tbl_name, idx_metadata=idx_metadata,
                                     if_not_exists=if_not_exists)
    else:
        for child_tbl_name in utils.child_table_names(conn, tbl_name):
            add_indices(conn, tbl_name=child_tbl_name, idx_metadata=idx_metadata, if_not_exists=if_not_exists)
Ejemplo n.º 2
0
def _queue_add_indices_parallel_job(conn, executor, db_config, *, tbl_name, idx_metadata, if_not_exists=False):
    """Function to queue and accumulate futures."""
    futures = []
    if not utils.is_table_partitioned(conn, tbl_name):
        for idx_metadatum in idx_metadata:
            futures.append(executor.submit(_add_indices_parallel_single_job,
                                           db_config,
                                           tbl_name=tbl_name,
                                           idx_metadatum=idx_metadatum,
                                           if_not_exists=if_not_exists))
    else:
        for child_tbl_name in utils.child_table_names(conn, tbl_name):
            futures.extend(_queue_add_indices_parallel_job(conn,
                                                           executor,
                                                           db_config,
                                                           tbl_name=child_tbl_name,
                                                           idx_metadata=idx_metadata,
                                                           if_not_exists=if_not_exists))

    return futures
Ejemplo n.º 3
0
def rename_table_and_indices(conn, *, old_tbl_name, new_tbl_name, idx_metadata=None):
    """Function to rename a potentially partitioned table and all associated indices on leaf tables."""
    if idx_metadata is None:
        idx_metadata = []

    with conn.cursor() as cursor:
        cursor.execute(sql.SQL('ALTER TABLE {0} RENAME TO {1}').format(sql.Identifier(old_tbl_name),
                                                                       sql.Identifier(new_tbl_name)))

        if not utils.is_table_partitioned(conn, new_tbl_name):
            for idx_metadatum in idx_metadata:
                old_idx_name = idx_metadatum.idx_name(old_tbl_name)
                new_idx_name = idx_metadatum.idx_name(new_tbl_name)
                cursor.execute(sql.SQL('ALTER INDEX {0} RENAME TO {1}').format(
                               sql.Identifier(old_idx_name), sql.Identifier(new_idx_name)))
        else:
            for child_tbl_name in utils.child_table_names(conn, new_tbl_name):
                # Child tables should start with the old table name
                assert child_tbl_name.startswith(old_tbl_name)
                suffix = child_tbl_name[len(old_tbl_name):]
                dest_table_name = new_tbl_name + suffix
                rename_table_and_indices(conn, old_tbl_name=child_tbl_name, new_tbl_name=dest_table_name,
                                         idx_metadata=idx_metadata)