Ejemplo n.º 1
0
    def valid_snapshot_target(self, relation: BaseRelation) -> None:
        """Ensure that the target relation is valid, by making sure it has the
        expected columns.

        :param Relation relation: The relation to check
        :raises CompilationException: If the columns are
            incorrect.
        """
        if not isinstance(relation, self.Relation):
            invalid_type_error(method_name='valid_snapshot_target',
                               arg_name='relation',
                               got_value=relation,
                               expected_type=self.Relation)

        columns = self.get_columns_in_relation(relation)
        names = set(c.name.lower() for c in columns)
        expanded_keys = ('scd_id', 'valid_from', 'valid_to')
        extra = []
        missing = []
        for legacy in expanded_keys:
            desired = 'dbt_' + legacy
            if desired not in names:
                missing.append(desired)
                if legacy in names:
                    extra.append(legacy)

        if missing:
            if extra:
                msg = ('Snapshot target has ("{}") but not ("{}") - is it an '
                       'unmigrated previous version archive?'.format(
                           '", "'.join(extra), '", "'.join(missing)))
            else:
                msg = ('Snapshot target is not a snapshot table (missing "{}")'
                       .format('", "'.join(missing)))
            raise_compiler_error(msg)
Ejemplo n.º 2
0
    def get_missing_columns(self, from_relation: BaseRelation,
                            to_relation: BaseRelation) -> List[BaseColumn]:
        """Returns a list of Columns in from_relation that are missing from
        to_relation.
        """
        if not isinstance(from_relation, self.Relation):
            invalid_type_error(method_name='get_missing_columns',
                               arg_name='from_relation',
                               got_value=from_relation,
                               expected_type=self.Relation)

        if not isinstance(to_relation, self.Relation):
            invalid_type_error(method_name='get_missing_columns',
                               arg_name='to_relation',
                               got_value=to_relation,
                               expected_type=self.Relation)

        from_columns = {
            col.name: col
            for col in self.get_columns_in_relation(from_relation)
        }

        to_columns = {
            col.name: col
            for col in self.get_columns_in_relation(to_relation)
        }

        missing_columns = set(from_columns.keys()) - set(to_columns.keys())

        return [
            col for (col_name, col) in from_columns.items()
            if col_name in missing_columns
        ]
Ejemplo n.º 3
0
    def expand_target_column_types(self, from_relation: BaseRelation,
                                   to_relation: BaseRelation) -> None:
        if not isinstance(from_relation, self.Relation):
            invalid_type_error(method_name='expand_target_column_types',
                               arg_name='from_relation',
                               got_value=from_relation,
                               expected_type=self.Relation)

        if not isinstance(to_relation, self.Relation):
            invalid_type_error(method_name='expand_target_column_types',
                               arg_name='to_relation',
                               got_value=to_relation,
                               expected_type=self.Relation)

        self.expand_column_types(from_relation, to_relation)