6767 SnapshotTableCleanupTask ,
6868)
6969from sqlmesh .core .snapshot .execution_tracker import QueryExecutionTracker
70- from sqlmesh .utils import random_id , CorrelationId
70+ from sqlmesh .utils import random_id , CorrelationId , AttributeDict
7171from sqlmesh .utils .concurrency import (
7272 concurrent_apply_to_snapshots ,
7373 concurrent_apply_to_values ,
@@ -2731,12 +2731,12 @@ def _execute_materialization(
27312731 ** kwargs : t .Any ,
27322732 ) -> None :
27332733 jinja_macros = model .jinja_macros
2734- existing_globals = jinja_macros .global_objs .copy ()
27352734
27362735 # For vdes we need to use the table, since we don't know the schema/table at parse time
27372736 parts = exp .to_table (table_name , dialect = self .adapter .dialect )
27382737
2739- relation_info = existing_globals .pop ("this" )
2738+ existing_globals = jinja_macros .global_objs
2739+ relation_info = existing_globals .get ("this" )
27402740 if isinstance (relation_info , dict ):
27412741 relation_info ["database" ] = parts .catalog
27422742 relation_info ["identifier" ] = parts .name
@@ -2750,29 +2750,29 @@ def _execute_materialization(
27502750 "identifier" : parts .name ,
27512751 "target" : existing_globals .get ("target" , {"type" : self .adapter .dialect }),
27522752 "execution_dt" : kwargs .get ("execution_time" ),
2753+ "engine_adapter" : self .adapter ,
2754+ "sql" : str (query_or_df ),
2755+ "is_first_insert" : is_first_insert ,
2756+ "create_only" : create_only ,
2757+ # FIXME: Add support for transaction=False
2758+ "pre_hooks" : [
2759+ AttributeDict ({"sql" : s .this .this , "transaction" : True })
2760+ for s in model .pre_statements
2761+ ],
2762+ "post_hooks" : [
2763+ AttributeDict ({"sql" : s .this .this , "transaction" : True })
2764+ for s in model .post_statements
2765+ ],
2766+ "model_instance" : model ,
2767+ ** kwargs ,
27532768 }
27542769
2755- context = jinja_macros ._create_builtin_globals (
2756- {"engine_adapter" : self .adapter , ** jinja_globals }
2757- )
2758-
2759- context .update (
2760- {
2761- "sql" : str (query_or_df ),
2762- "is_first_insert" : is_first_insert ,
2763- "create_only" : create_only ,
2764- "pre_hooks" : model .render_pre_statements (** render_kwargs ),
2765- "post_hooks" : model .render_post_statements (** render_kwargs ),
2766- ** kwargs ,
2767- }
2768- )
2769-
27702770 try :
2771- jinja_env = jinja_macros .build_environment (** context )
2771+ jinja_env = jinja_macros .build_environment (** jinja_globals )
27722772 template = jinja_env .from_string (self .materialization_template )
27732773
27742774 try :
2775- template .render (** context )
2775+ template .render ()
27762776 except MacroReturnVal as ret :
27772777 # this is a successful return from a macro call (dbt uses this list of Relations to update their relation cache)
27782778 returned_relations = ret .value .get ("relations" , [])
0 commit comments