@@ -873,6 +873,7 @@ def create_snapshot(
873
873
deployability_index = deployability_index ,
874
874
create_render_kwargs = create_render_kwargs ,
875
875
rendered_physical_properties = rendered_physical_properties ,
876
+ skip_grants = True ,
876
877
dry_run = True ,
877
878
)
878
879
@@ -1411,12 +1412,12 @@ def _execute_create(
1411
1412
table_name = table_name ,
1412
1413
model = snapshot .model ,
1413
1414
is_table_deployable = is_table_deployable ,
1415
+ skip_grants = skip_grants ,
1414
1416
render_kwargs = create_render_kwargs ,
1415
1417
is_snapshot_deployable = is_snapshot_deployable ,
1416
1418
is_snapshot_representative = is_snapshot_representative ,
1417
1419
dry_run = dry_run ,
1418
1420
physical_properties = rendered_physical_properties ,
1419
- skip_grants = skip_grants ,
1420
1421
)
1421
1422
if run_pre_post_statements :
1422
1423
adapter .execute (snapshot .model .render_post_statements (** create_render_kwargs ))
@@ -1580,6 +1581,7 @@ def create(
1580
1581
model : Model ,
1581
1582
is_table_deployable : bool ,
1582
1583
render_kwargs : t .Dict [str , t .Any ],
1584
+ skip_grants : bool ,
1583
1585
** kwargs : t .Any ,
1584
1586
) -> None :
1585
1587
"""Creates the target table or view.
@@ -1723,6 +1725,7 @@ def create(
1723
1725
model : Model ,
1724
1726
is_table_deployable : bool ,
1725
1727
render_kwargs : t .Dict [str , t .Any ],
1728
+ skip_grants : bool ,
1726
1729
** kwargs : t .Any ,
1727
1730
) -> None :
1728
1731
pass
@@ -1798,10 +1801,10 @@ def promote(
1798
1801
view_properties = model .render_virtual_properties (** render_kwargs ),
1799
1802
)
1800
1803
1801
- # Apply grants to the physical layer table
1804
+ # Apply grants to the physical layer (referenced table / view) after promotion
1802
1805
self ._apply_grants (model , table_name , GrantsTargetLayer .PHYSICAL )
1803
1806
1804
- # Apply grants to the virtual layer view
1807
+ # Apply grants to the virtual layer ( view) after promotion
1805
1808
self ._apply_grants (model , view_name , GrantsTargetLayer .VIRTUAL )
1806
1809
1807
1810
def demote (self , view_name : str , ** kwargs : t .Any ) -> None :
@@ -1816,6 +1819,7 @@ def create(
1816
1819
model : Model ,
1817
1820
is_table_deployable : bool ,
1818
1821
render_kwargs : t .Dict [str , t .Any ],
1822
+ skip_grants : bool ,
1819
1823
** kwargs : t .Any ,
1820
1824
) -> None :
1821
1825
ctas_query = model .ctas_query (** render_kwargs )
@@ -1861,7 +1865,7 @@ def create(
1861
1865
)
1862
1866
1863
1867
# Apply grants after table creation (unless explicitly skipped by caller)
1864
- if not kwargs . get ( " skip_grants" , False ) :
1868
+ if not skip_grants :
1865
1869
self ._apply_grants (model , table_name , GrantsTargetLayer .PHYSICAL )
1866
1870
1867
1871
def migrate (
@@ -1903,6 +1907,7 @@ def _replace_query_for_model(
1903
1907
name : str ,
1904
1908
query_or_df : QueryOrDF ,
1905
1909
render_kwargs : t .Dict [str , t .Any ],
1910
+ skip_grants : bool = False ,
1906
1911
** kwargs : t .Any ,
1907
1912
) -> None :
1908
1913
"""Replaces the table for the given model.
@@ -1940,7 +1945,7 @@ def _replace_query_for_model(
1940
1945
)
1941
1946
1942
1947
# Apply grants after table replacement (unless explicitly skipped by caller)
1943
- if not kwargs . get ( " skip_grants" , False ) :
1948
+ if not skip_grants :
1944
1949
self ._apply_grants (model , name , GrantsTargetLayer .PHYSICAL )
1945
1950
1946
1951
def _get_target_and_source_columns (
@@ -2190,6 +2195,7 @@ def create(
2190
2195
model : Model ,
2191
2196
is_table_deployable : bool ,
2192
2197
render_kwargs : t .Dict [str , t .Any ],
2198
+ skip_grants : bool ,
2193
2199
** kwargs : t .Any ,
2194
2200
) -> None :
2195
2201
model = t .cast (SeedModel , model )
@@ -2203,29 +2209,38 @@ def create(
2203
2209
)
2204
2210
return
2205
2211
2206
- # Skip grants in parent create call since we'll apply them after data insertion
2207
- kwargs_no_grants = {** kwargs }
2208
- kwargs_no_grants ["skip_grants" ] = True
2209
-
2210
- super ().create (table_name , model , is_table_deployable , render_kwargs , ** kwargs_no_grants )
2212
+ super ().create (
2213
+ table_name ,
2214
+ model ,
2215
+ is_table_deployable ,
2216
+ render_kwargs ,
2217
+ skip_grants = True , # Skip grants; they're applied after data insertion
2218
+ ** kwargs ,
2219
+ )
2211
2220
# For seeds we insert data at the time of table creation.
2212
2221
try :
2213
2222
for index , df in enumerate (model .render_seed ()):
2214
2223
if index == 0 :
2215
2224
self ._replace_query_for_model (
2216
- model , table_name , df , render_kwargs , ** kwargs_no_grants
2225
+ model ,
2226
+ table_name ,
2227
+ df ,
2228
+ render_kwargs ,
2229
+ skip_grants = True , # Skip grants; they're applied after data insertion
2230
+ ** kwargs ,
2217
2231
)
2218
2232
else :
2219
2233
self .adapter .insert_append (
2220
2234
table_name , df , target_columns_to_types = model .columns_to_types
2221
2235
)
2236
+
2237
+ if not skip_grants :
2238
+ # Apply grants after seed table creation and data insertion
2239
+ self ._apply_grants (model , table_name , GrantsTargetLayer .PHYSICAL )
2222
2240
except Exception :
2223
2241
self .adapter .drop_table (table_name )
2224
2242
raise
2225
2243
2226
- # Apply grants after seed table creation or data insertion
2227
- self ._apply_grants (model , table_name , GrantsTargetLayer .PHYSICAL )
2228
-
2229
2244
def insert (
2230
2245
self ,
2231
2246
table_name : str ,
@@ -2257,6 +2272,7 @@ def create(
2257
2272
model : Model ,
2258
2273
is_table_deployable : bool ,
2259
2274
render_kwargs : t .Dict [str , t .Any ],
2275
+ skip_grants : bool ,
2260
2276
** kwargs : t .Any ,
2261
2277
) -> None :
2262
2278
assert isinstance (model .kind , (SCDType2ByTimeKind , SCDType2ByColumnKind ))
@@ -2286,11 +2302,13 @@ def create(
2286
2302
model ,
2287
2303
is_table_deployable ,
2288
2304
render_kwargs ,
2305
+ skip_grants ,
2289
2306
** kwargs ,
2290
2307
)
2291
2308
2292
- # Apply grants after SCD Type 2 table creation
2293
- self ._apply_grants (model , table_name , GrantsTargetLayer .PHYSICAL )
2309
+ if not skip_grants :
2310
+ # Apply grants after SCD Type 2 table creation
2311
+ self ._apply_grants (model , table_name , GrantsTargetLayer .PHYSICAL )
2294
2312
2295
2313
def insert (
2296
2314
self ,
@@ -2434,14 +2452,17 @@ def create(
2434
2452
model : Model ,
2435
2453
is_table_deployable : bool ,
2436
2454
render_kwargs : t .Dict [str , t .Any ],
2455
+ skip_grants : bool ,
2437
2456
** kwargs : t .Any ,
2438
2457
) -> None :
2439
2458
if self .adapter .table_exists (table_name ):
2440
2459
# Make sure we don't recreate the view to prevent deletion of downstream views in engines with no late
2441
2460
# binding support (because of DROP CASCADE).
2442
2461
logger .info ("View '%s' already exists" , table_name )
2443
- # Always apply grants when present, even if view exists, to handle grants updates
2444
- self ._apply_grants (model , table_name , GrantsTargetLayer .PHYSICAL )
2462
+
2463
+ if not skip_grants :
2464
+ # Always apply grants when present, even if view exists, to handle grants updates
2465
+ self ._apply_grants (model , table_name , GrantsTargetLayer .PHYSICAL )
2445
2466
return
2446
2467
2447
2468
logger .info ("Creating view '%s'" , table_name )
@@ -2465,8 +2486,9 @@ def create(
2465
2486
column_descriptions = model .column_descriptions if is_table_deployable else None ,
2466
2487
)
2467
2488
2468
- # Apply grants after view creation
2469
- self ._apply_grants (model , table_name , GrantsTargetLayer .PHYSICAL )
2489
+ if not skip_grants :
2490
+ # Apply grants after view creation
2491
+ self ._apply_grants (model , table_name , GrantsTargetLayer .PHYSICAL )
2470
2492
2471
2493
def migrate (
2472
2494
self ,
@@ -2643,6 +2665,7 @@ def create(
2643
2665
model : Model ,
2644
2666
is_table_deployable : bool ,
2645
2667
render_kwargs : t .Dict [str , t .Any ],
2668
+ skip_grants : bool ,
2646
2669
** kwargs : t .Any ,
2647
2670
) -> None :
2648
2671
is_snapshot_deployable : bool = kwargs ["is_snapshot_deployable" ]
@@ -2663,24 +2686,21 @@ def create(
2663
2686
)
2664
2687
2665
2688
# Apply grants after managed table creation
2666
- self ._apply_grants (model , table_name , GrantsTargetLayer .PHYSICAL )
2689
+ if not skip_grants :
2690
+ self ._apply_grants (model , table_name , GrantsTargetLayer .PHYSICAL )
2667
2691
2668
2692
elif not is_table_deployable :
2669
2693
# Only create the dev preview table as a normal table.
2670
2694
# For the main table, if the snapshot is cant be deployed to prod (eg upstream is forward-only) do nothing.
2671
2695
# Any downstream models that reference it will be updated to point to the dev preview table.
2672
2696
# If the user eventually tries to deploy it, the logic in insert() will see it doesnt exist and create it
2673
-
2674
- # Create preview table but don't apply grants here since the table is not deployable
2675
- # Grants will be applied later when the table becomes deployable
2676
- kwargs_no_grants = {** kwargs }
2677
- kwargs_no_grants ["skip_grants" ] = True
2678
2697
super ().create (
2679
2698
table_name = table_name ,
2680
2699
model = model ,
2681
2700
is_table_deployable = is_table_deployable ,
2682
2701
render_kwargs = render_kwargs ,
2683
- ** kwargs_no_grants ,
2702
+ skip_grants = skip_grants ,
2703
+ ** kwargs ,
2684
2704
)
2685
2705
2686
2706
def insert (
@@ -2707,6 +2727,7 @@ def insert(
2707
2727
column_descriptions = model .column_descriptions ,
2708
2728
table_format = model .table_format ,
2709
2729
)
2730
+ self ._apply_grants (model , table_name , GrantsTargetLayer .PHYSICAL )
2710
2731
elif not is_snapshot_deployable :
2711
2732
# Snapshot isnt deployable; update the preview table instead
2712
2733
# If the snapshot was deployable, then data would have already been loaded in create() because a managed table would have been created
@@ -2756,7 +2777,7 @@ def migrate(
2756
2777
)
2757
2778
2758
2779
# Apply grants after verifying no schema changes
2759
- # This ensures metadata-only changes ( grants) are applied
2780
+ # This ensures metadata-only grants changes are applied
2760
2781
self ._apply_grants (snapshot .model , target_table_name , GrantsTargetLayer .PHYSICAL )
2761
2782
2762
2783
def delete (self , name : str , ** kwargs : t .Any ) -> None :
0 commit comments