Example #1
0
def parse_node(node,
               node_path,
               root_project_config,
               package_project_config,
               all_projects,
               tags=None,
               fqn_extra=None):
    logger.debug("Parsing {}".format(node_path))
    node = copy.deepcopy(node)

    if tags is None:
        tags = set()

    if fqn_extra is None:
        fqn_extra = []

    node.update({
        'refs': [],
        'depends_on': {
            'nodes': [],
            'macros': [],
        }
    })

    fqn = get_fqn(node.get('path'), package_project_config, fqn_extra)

    config = dbt.model.SourceConfig(root_project_config,
                                    package_project_config, fqn)

    node['unique_id'] = node_path
    node['empty'] = (len(node.get('raw_sql').strip()) == 0)
    node['fqn'] = fqn
    node['tags'] = tags

    # Set this temporarily. Not the full config yet (as config() hasn't been
    # called from jinja yet). But the Var() call below needs info about project
    # level configs b/c they might contain refs. TODO: Restructure this?
    config_dict = node.get('config', {})
    config_dict.update(config.config)
    node['config'] = config_dict

    context = {}
    context['ref'] = __ref(node)
    context['config'] = __config(node, config)
    context['target'] = property(lambda x: '', lambda x: x)
    context['this'] = ''

    context['var'] = Var(node, context)

    dbt.clients.jinja.get_rendered(node.get('raw_sql'),
                                   context,
                                   node,
                                   capture_macros=True)

    # Overwrite node config
    config_dict = node.get('config', {})
    config_dict.update(config.config)
    node['config'] = config_dict

    return node
Example #2
0
    def get_context(self, linker, model, models, add_dependency=False):
        runtime = RuntimeContext(model=model)

        context = self.project.context()

        # built-ins
        context['ref'] = self.__ref(linker, context, model, models,
                                    add_dependency)
        context['config'] = self.__model_config(model, linker)
        context['this'] = This(context['env']['schema'], model.immediate_name,
                               model.name)
        context['var'] = Var(model, context=context)
        context['target'] = self.project.get_target()

        # these get re-interpolated at runtime!
        context['run_started_at'] = '{{ run_started_at }}'
        context['invocation_id'] = '{{ invocation_id }}'

        # add in context from run target
        context.update(self.target.context)

        runtime.update_global(context)

        # add in macros (can we cache these somehow?)
        for macro_data in self.macro_generator(context):
            macro = macro_data["macro"]
            macro_name = macro_data["name"]
            project = macro_data["project"]

            runtime.update_package(project['name'], {macro_name: macro})

            if project['name'] == self.project['name']:
                runtime.update_global({macro_name: macro})

        return runtime
Example #3
0
 def get_context(self, linker, model, models):
     context = self.project.context()
     context['ref'] = self.__ref(linker, context, model, models)
     context['config'] = self.__model_config(model, linker)
     context['this'] = This(context['env']['schema'], model.immediate_name,
                            model.name)
     context['compiled_at'] = time.strftime('%Y-%m-%d %H:%M:%S')
     context['var'] = Var(model, context=context)
     return context
Example #4
0
    def get_compiler_context(self, model, flat_graph):
        context = self.project.context()
        profile = self.project.run_environment()
        adapter = get_adapter(profile)

        wrapper = dbt.wrapper.DatabaseWrapper(model, adapter, profile)

        # built-ins
        context['ref'] = self.__ref(context, model, flat_graph,
                                    self.project.cfg.get('name'))
        context['config'] = self.__model_config(model)
        context['this'] = This(
            context['env']['schema'],
            dbt.utils.model_immediate_name(model, dbt.flags.NON_DESTRUCTIVE),
            model.get('name')
        )
        context['var'] = Var(model, context=context)
        context['target'] = self.project.get_target()
        context['adapter'] = wrapper
        context['flags'] = dbt.flags

        context.update(wrapper.get_context_functions())

        context['run_started_at'] = dbt.tracking.active_user.run_started_at
        context['invocation_id'] = dbt.tracking.active_user.invocation_id
        context['sql_now'] = adapter.date_function()

        for unique_id, macro in flat_graph.get('macros').items():
            package_name = macro.get('package_name')

            macro_map = {macro.get('name'): macro.get('parsed_macro')}

            if context.get(package_name) is None:
                context[package_name] = {}

            context.get(package_name, {}) \
                   .update(macro_map)

            if(package_name == model.get('package_name') or
               package_name == dbt.include.GLOBAL_PROJECT_NAME):
                context.update(macro_map)

        return context
Example #5
0
    def get_context(self, linker, model,  models, add_dependency=False):
        context = self.project.context()

        # built-ins
        context['ref'] = self.__ref(linker, context, model, models, add_dependency)
        context['config'] = self.__model_config(model, linker)
        context['this'] = This(context['env']['schema'], model.immediate_name, model.name)
        context['var'] = Var(model, context=context)
        context['target'] = self.project.get('run-target')

        # these get re-interpolated at runtime!
        context['run_started_at'] = '{{ run_started_at }}'
        context['invocation_id']  = '{{ invocation_id }}'

        # add in context from run target
        context.update(self.target.context)

        # add in macros (can we cache these somehow?)
        for macro_name, macro in self.macro_generator(context):
            context[macro_name] = macro

        return context