Exemple #1
0
    def get_bq_schema_for_table(self,
                                table_name: str) -> List[bigquery.SchemaField]:
        """Return a List of SchemaField objects for a given table name
            If it is an association table for a schema that includes the region code, a region code schema field
            is appended.
        """
        table = get_table_class_by_name(table_name, self.sorted_tables)
        columns = [
            bigquery.SchemaField(column.name, BQ_TYPES[type(column.type)],
                                 'NULLABLE' if column.nullable else 'REQUIRED')
            for column in table.columns
            if column.name in self._get_table_columns_to_export(table)
        ]

        if not schema_has_region_code_query_support(self.metadata_base):
            # We shouldn't add a region code column for this schema
            return columns

        column_names = {column.name for column in table.columns}
        region_code_col = get_region_code_col(self.metadata_base, table)
        if region_code_col in column_names:
            # We already have the region column we need in the table
            return columns

        columns.append(
            bigquery.SchemaField(region_code_col, BQ_TYPES[sqlalchemy.String],
                                 'NULLABLE'))

        return columns
 def _get_region_code_col(self) -> Optional[str]:
     if not schema_has_region_code_query_support(self.metadata_base):
         return None
     table = self._get_region_code_table()
     return get_region_code_col(self.metadata_base, table)