diff --git a/dbt/compilation.py b/dbt/compilation.py index 30d04425a4b..b92b2b22d3f 100644 --- a/dbt/compilation.py +++ b/dbt/compilation.py @@ -238,6 +238,8 @@ def get_compiler_context(self, model, flat_graph): context['adapter'] = wrapper context['flags'] = dbt.flags + context.update(wrapper.get_context_functions()) + context['run_started_at'] = '{{ run_started_at }}' context['invocation_id'] = '{{ invocation_id }}' context['sql_now'] = adapter.date_function() diff --git a/dbt/wrapper.py b/dbt/wrapper.py index b40c67b3aee..d36d4b0ae45 100644 --- a/dbt/wrapper.py +++ b/dbt/wrapper.py @@ -148,11 +148,20 @@ class DatabaseWrapper(object): functions. """ + context_functions = [ + "already_exists", + "get_columns_in_table", + "get_missing_columns" + ] + def __init__(self, model, adapter, profile): self.model = model self.adapter = adapter self.profile = profile + def get_context_functions(self): + return {name: getattr(self, name) for name in self.context_functions} + def already_exists(self, schema, table): return self.adapter.already_exists( self.profile, schema, table, self.model.get('name')) @@ -184,6 +193,8 @@ def wrap(model, project, context, injected_graph): profile = project.run_environment() + db_wrapper = DatabaseWrapper(model, adapter, profile) + opts = { "materialization": get_materialization(model), "model": model, @@ -194,7 +205,9 @@ def wrap(model, project, context, injected_graph): "post_hooks": post_hooks, "sql": rendered_query, "flags": dbt.flags, - "adapter": DatabaseWrapper(model, adapter, profile), + "adapter": db_wrapper } + opts.update(db_wrapper.get_context_functions()) + return do_wrap(model, opts, injected_graph, context, project)