def expand_target_column_types(cls, profile, temp_table, to_schema, to_table, model_name=None): reference_columns = cls._table_columns_to_dict( cls.get_columns_in_table(profile, None, temp_table, model_name)) target_columns = cls._table_columns_to_dict( cls.get_columns_in_table(profile, to_schema, to_table, model_name)) for column_name, reference_column in reference_columns.items(): target_column = target_columns.get(column_name) if target_column is not None and \ target_column.can_expand_to(reference_column): new_type = Column.string_type(reference_column.string_size()) logger.debug("Changing col type from %s to %s in table %s.%s", target_column.data_type, new_type, to_schema, to_table) cls.alter_column_type(profile, to_schema, to_table, column_name, new_type, model_name)
def get_columns_in_table(cls, profile, schema_name, table_name, model_name=None): sql = """ select column_name, data_type, character_maximum_length from information_schema.columns where table_name = '{table_name}' """.format(table_name=table_name).strip() if schema_name is not None: sql += (" AND table_schema = '{schema_name}'".format( schema_name=schema_name)) connection, cursor = cls.add_query(profile, sql, model_name) data = cursor.fetchall() columns = [] for row in data: name, data_type, char_size = row column = Column(name, data_type, char_size) columns.append(column) return columns
def get_columns_in_table(cls, profile, schema_name, table_name): connection = cls.get_connection(profile) if flags.STRICT_MODE: validate_connection(connection) query = """ select column_name, data_type, character_maximum_length from information_schema.columns where table_name = '{table_name}' """.format(table_name=table_name).strip() if schema_name is not None: query += (" AND table_schema = '{schema_name}'".format( schema_name=schema_name)) handle, cursor = cls.add_query_to_transaction(query, connection, table_name) data = cursor.fetchall() columns = [] for row in data: name, data_type, char_size = row column = Column(name, data_type, char_size) columns.append(column) return columns
def expand_target_column_types(cls, profile, temp_table, to_schema, to_table): connection = cls.get_connection(profile) if flags.STRICT_MODE: validate_connection(connection) reference_columns = { col.name: col for col in cls.get_columns_in_table(profile, None, temp_table) } target_columns = { col.name: col for col in cls.get_columns_in_table(profile, to_schema, to_table) } for column_name, reference_column in reference_columns.items(): target_column = target_columns.get(column_name) if target_column is not None and \ target_column.can_expand_to(reference_column): new_type = Column.string_type(reference_column.string_size()) logger.debug("Changing col type from %s to %s in table %s.%s", target_column.data_type, new_type, to_schema, to_table) cls.alter_column_type(connection, to_schema, to_table, column_name, new_type)
def get_columns_in_table(cls, profile, schema_name, table_name, model_name=None): sql = cls._get_columns_in_table_sql(schema_name, table_name) connection, cursor = cls.add_query(profile, sql, model_name) data = cursor.fetchall() columns = [] for row in data: name, data_type, char_size = row column = Column(name, data_type, char_size) columns.append(column) return columns