Beispiel #1
0
    def run_hooks(self, adapter, hook_type, extra_context):

        nodes = self.manifest.nodes.values()
        hooks = get_nodes_by_tags(nodes, {hook_type}, NodeType.Operation)

        ordered_hooks = sorted(hooks, key=lambda h: h.get('index', len(hooks)))

        for i, hook in enumerate(ordered_hooks):
            model_name = hook.get('name')

            # This will clear out an open transaction if there is one.
            # on-run-* hooks should run outside of a transaction. This happens
            # b/c psycopg2 automatically begins a transaction when a connection
            # is created. TODO : Move transaction logic out of here, and
            # implement a for-loop over these sql statements in jinja-land.
            # Also, consider configuring psycopg2 (and other adapters?) to
            # ensure that a transaction is only created if dbt initiates it.
            adapter.clear_transaction(model_name)
            compiled = compile_node(adapter, self.config, hook, self.manifest,
                                    extra_context)
            statement = compiled.wrapped_sql

            hook_index = hook.get('index', len(hooks))
            hook_dict = get_hook_dict(statement, index=hook_index)

            if dbt.flags.STRICT_MODE:
                Hook(**hook_dict)

            sql = hook_dict.get('sql', '')

            if len(sql.strip()) > 0:
                adapter.execute(sql, model_name=model_name, auto_begin=False,
                                fetch=False)

            adapter.release_connection(model_name)
Beispiel #2
0
    def run_hooks(cls, project, adapter, flat_graph, hook_type):
        profile = project.run_environment()

        nodes = flat_graph.get('nodes', {}).values()
        hooks = get_nodes_by_tags(nodes, {hook_type}, NodeType.Operation)

        # This will clear out an open transaction if there is one.
        # on-run-* hooks should run outside of a transaction. This happens b/c
        # psycopg2 automatically begins a transaction when a connection is
        # created. TODO : Move transaction logic out of here, and implement
        # a for-loop over these sql statements in jinja-land. Also, consider
        # configuring psycopg2 (and other adapters?) to ensure that a
        # transaction is only created if dbt initiates it.
        conn_name = adapter.clear_transaction(profile)

        compiled_hooks = []
        for hook in hooks:
            compiled = cls.compile_node(adapter, project, hook, flat_graph)
            model_name = compiled.get('name')
            statement = compiled['wrapped_sql']

            hook_dict = dbt.hooks.get_hook_dict(statement)
            compiled_hooks.append(hook_dict)

        for hook in compiled_hooks:

            if dbt.flags.STRICT_MODE:
                dbt.contracts.graph.parsed.validate_hook(hook)

            sql = hook.get('sql', '')
            adapter.execute_one(profile,
                                sql,
                                model_name=conn_name,
                                auto_begin=False)
            adapter.release_connection(profile, conn_name)
Beispiel #3
0
    def run_hooks(cls, config, adapter, manifest, hook_type):

        nodes = manifest.nodes.values()
        hooks = get_nodes_by_tags(nodes, {hook_type}, NodeType.Operation)

        ordered_hooks = sorted(hooks, key=lambda h: h.get('index', len(hooks)))

        for i, hook in enumerate(ordered_hooks):
            model_name = hook.get('name')

            # This will clear out an open transaction if there is one.
            # on-run-* hooks should run outside of a transaction. This happens
            # b/c psycopg2 automatically begins a transaction when a connection
            # is created. TODO : Move transaction logic out of here, and
            # implement a for-loop over these sql statements in jinja-land.
            # Also, consider configuring psycopg2 (and other adapters?) to
            # ensure that a transaction is only created if dbt initiates it.
            adapter.clear_transaction(model_name)
            compiled = cls._compile_node(adapter, config, hook, manifest)
            statement = compiled.wrapped_sql

            hook_index = hook.get('index', len(hooks))
            hook_dict = dbt.hooks.get_hook_dict(statement, index=hook_index)

            if dbt.flags.STRICT_MODE:
                dbt.contracts.graph.parsed.Hook(**hook_dict)

            sql = hook_dict.get('sql', '')

            if len(sql.strip()) > 0:
                adapter.execute(sql, model_name=model_name, auto_begin=False,
                                fetch=False)

            adapter.release_connection(model_name)
Beispiel #4
0
 def get_hooks_by_type(self,
                       hook_type: RunHookType) -> List[ParsedHookNode]:
     nodes = self.manifest.nodes.values()
     # find all hooks defined in the manifest (could be multiple projects)
     hooks = get_nodes_by_tags(nodes, {hook_type}, NodeType.Operation)
     hooks.sort(key=self._hook_keyfunc)
     return hooks
Beispiel #5
0
    def run_hooks(self, profile, flat_graph, hook_type):
        adapter = get_adapter(profile)

        nodes = flat_graph.get('nodes', {}).values()
        start_hooks = get_nodes_by_tags(nodes, {hook_type}, NodeType.Operation)
        hooks = [self.compile_node(hook, flat_graph) for hook in start_hooks]

        master_connection = adapter.begin(profile)
        compiled_hooks = [hook['wrapped_sql'] for hook in hooks]
        adapter.execute_all(profile=profile, sqls=compiled_hooks)
        master_connection = adapter.commit(master_connection)