Exemplo n.º 1
0
def check_column_types(conn, schema, table, column_names, data_types):
    """
    Check if database column types match trend datatype and correct it if
    necessary.
    """
    current_data_types = get_data_types(conn, schema, table, column_names)
    full_table_name = create_full_table_name(schema, table)

    with closing(conn.cursor()) as cursor:
        for column_name, current_data_type, data_type in zip(
                column_names, current_data_types, data_types):
            required_data_type = datatype.max_datatype(
                current_data_type, data_type)

            if required_data_type != current_data_type:
                logging.debug("{} != {}".format(required_data_type,
                                                current_data_type))

                query = (
                    "ALTER TABLE {0} "
                    "ALTER \"{1}\" TYPE {2} "
                    "USING CAST(\"{1}\" AS {2})").format(
                    full_table_name, column_name, required_data_type)

                cursor.execute(query)

                logging.info(
                    "Column {0:s} modified from type {1} to {2}".format(
                        column_name, current_data_type, required_data_type))
    conn.commit()
Exemplo n.º 2
0
def check_column_types(conn, schema, table, column_names, data_types):
    """
    Check if database column types match trend datatype and correct it if
    necessary.
    """
    current_data_types = get_data_types(conn, schema, table, column_names)

    with closing(conn.cursor()) as cursor:
        for column_name, current_data_type, data_type in zip(column_names, current_data_types, data_types):
            required_data_type = datatype.max_datatype(current_data_type, data_type)

            if required_data_type != current_data_type:
                logging.debug("{} != {}".format(required_data_type, current_data_type))

                args = table, column_name, required_data_type

                cursor.callproc("trend.modify_partition_column", args)

                logging.info(
                    "Column {0:s} modified from type {1} to {2}".format(
                        column_name, current_data_type, required_data_type
                    )
                )

    conn.commit()