@@ -750,13 +750,19 @@ def _evaluate_snapshot(
750750 ** render_statements_kwargs
751751 )
752752
753+ evaluation_strategy = _evaluation_strategy (snapshot , adapter )
754+ evaluation_strategy .run_pre_statements (
755+ snapshot = snapshot ,
756+ render_kwargs = {** render_statements_kwargs , "inside_transaction" : False },
757+ )
758+
753759 with (
754760 adapter .transaction (),
755761 adapter .session (snapshot .model .render_session_properties (** render_statements_kwargs )),
756762 ):
757- evaluation_strategy = _evaluation_strategy (snapshot , adapter )
758763 evaluation_strategy .run_pre_statements (
759- snapshot = snapshot , render_kwargs = render_statements_kwargs
764+ snapshot = snapshot ,
765+ render_kwargs = {** render_statements_kwargs , "inside_transaction" : True },
760766 )
761767
762768 if not target_table_exists or (model .is_seed and not snapshot .intervals ):
@@ -828,10 +834,16 @@ def _evaluate_snapshot(
828834 )
829835
830836 evaluation_strategy .run_post_statements (
831- snapshot = snapshot , render_kwargs = render_statements_kwargs
837+ snapshot = snapshot ,
838+ render_kwargs = {** render_statements_kwargs , "inside_transaction" : True },
832839 )
833840
834- return wap_id
841+ evaluation_strategy .run_post_statements (
842+ snapshot = snapshot ,
843+ render_kwargs = {** render_statements_kwargs , "inside_transaction" : False },
844+ )
845+
846+ return wap_id
835847
836848 def create_snapshot (
837849 self ,
@@ -865,6 +877,11 @@ def create_snapshot(
865877 deployability_index = deployability_index ,
866878 )
867879
880+ evaluation_strategy = _evaluation_strategy (snapshot , adapter )
881+ evaluation_strategy .run_pre_statements (
882+ snapshot = snapshot , render_kwargs = {** create_render_kwargs , "inside_transaction" : False }
883+ )
884+
868885 with (
869886 adapter .transaction (),
870887 adapter .session (snapshot .model .render_session_properties (** create_render_kwargs )),
@@ -896,6 +913,10 @@ def create_snapshot(
896913 dry_run = True ,
897914 )
898915
916+ evaluation_strategy .run_post_statements (
917+ snapshot = snapshot , render_kwargs = {** create_render_kwargs , "inside_transaction" : False }
918+ )
919+
899920 if on_complete is not None :
900921 on_complete (snapshot )
901922
@@ -1097,6 +1118,11 @@ def _migrate_snapshot(
10971118 )
10981119 target_table_name = snapshot .table_name ()
10991120
1121+ evaluation_strategy = _evaluation_strategy (snapshot , adapter )
1122+ evaluation_strategy .run_pre_statements (
1123+ snapshot = snapshot , render_kwargs = {** render_kwargs , "inside_transaction" : False }
1124+ )
1125+
11001126 with (
11011127 adapter .transaction (),
11021128 adapter .session (snapshot .model .render_session_properties (** render_kwargs )),
@@ -1134,6 +1160,10 @@ def _migrate_snapshot(
11341160 dry_run = True ,
11351161 )
11361162
1163+ evaluation_strategy .run_post_statements (
1164+ snapshot = snapshot , render_kwargs = {** render_kwargs , "inside_transaction" : False }
1165+ )
1166+
11371167 # Retry in case when the table is migrated concurrently from another plan application
11381168 @retry (
11391169 reraise = True ,
@@ -1454,7 +1484,8 @@ def _execute_create(
14541484 }
14551485 if run_pre_post_statements :
14561486 evaluation_strategy .run_pre_statements (
1457- snapshot = snapshot , render_kwargs = create_render_kwargs
1487+ snapshot = snapshot ,
1488+ render_kwargs = {** create_render_kwargs , "inside_transaction" : True },
14581489 )
14591490 evaluation_strategy .create (
14601491 table_name = table_name ,
@@ -1471,7 +1502,8 @@ def _execute_create(
14711502 )
14721503 if run_pre_post_statements :
14731504 evaluation_strategy .run_post_statements (
1474- snapshot = snapshot , render_kwargs = create_render_kwargs
1505+ snapshot = snapshot ,
1506+ render_kwargs = {** create_render_kwargs , "inside_transaction" : True },
14751507 )
14761508
14771509 def _can_clone (self , snapshot : Snapshot , deployability_index : DeployabilityIndex ) -> bool :
@@ -2944,12 +2976,20 @@ def append(
29442976 )
29452977
29462978 def run_pre_statements (self , snapshot : Snapshot , render_kwargs : t .Any ) -> None :
2947- # in dbt custom materialisations it's up to the user when to run the pre hooks
2948- pass
2979+ # in dbt custom materialisations it's up to the user to run the pre hooks inside the transaction
2980+ if not render_kwargs .get ("inside_transaction" , True ):
2981+ super ().run_pre_statements (
2982+ snapshot = snapshot ,
2983+ render_kwargs = render_kwargs ,
2984+ )
29492985
29502986 def run_post_statements (self , snapshot : Snapshot , render_kwargs : t .Any ) -> None :
2951- # in dbt custom materialisations it's up to the user when to run the post hooks
2952- pass
2987+ # in dbt custom materialisations it's up to the user to run the post hooks inside the transaction
2988+ if not render_kwargs .get ("inside_transaction" , True ):
2989+ super ().run_post_statements (
2990+ snapshot = snapshot ,
2991+ render_kwargs = render_kwargs ,
2992+ )
29532993
29542994 def _execute_materialization (
29552995 self ,
@@ -2985,14 +3025,15 @@ def _execute_materialization(
29853025 "sql" : str (query_or_df ),
29863026 "is_first_insert" : is_first_insert ,
29873027 "create_only" : create_only ,
2988- # FIXME: Add support for transaction=False
29893028 "pre_hooks" : [
2990- AttributeDict ({"sql" : s .this .this , "transaction" : True })
3029+ AttributeDict ({"sql" : s .this .this , "transaction" : transaction })
29913030 for s in model .pre_statements
3031+ if (transaction := s .args .get ("transaction" , True ))
29923032 ],
29933033 "post_hooks" : [
2994- AttributeDict ({"sql" : s .this .this , "transaction" : True })
3034+ AttributeDict ({"sql" : s .this .this , "transaction" : transaction })
29953035 for s in model .post_statements
3036+ if (transaction := s .args .get ("transaction" , True ))
29963037 ],
29973038 "model_instance" : model ,
29983039 ** kwargs ,
0 commit comments