@@ -368,6 +368,7 @@ def create(
368368 on_complete : t .Optional [t .Callable [[SnapshotInfoLike ], None ]] = None ,
369369 allow_destructive_snapshots : t .Optional [t .Set [str ]] = None ,
370370 allow_additive_snapshots : t .Optional [t .Set [str ]] = None ,
371+ owner : t .Optional [str ] = None ,
371372 ) -> CompletionStatus :
372373 """Creates a physical snapshot schema and table for the given collection of snapshots.
373374
@@ -379,6 +380,7 @@ def create(
379380 on_complete: A callback to call on each successfully created snapshot.
380381 allow_destructive_snapshots: Set of snapshots that are allowed to have destructive schema changes.
381382 allow_additive_snapshots: Set of snapshots that are allowed to have additive schema changes.
383+ owner: Optional principal to set as table owner after creation.
382384
383385 Returns:
384386 CompletionStatus: The status of the creation operation (success, failure, nothing to do).
@@ -398,17 +400,22 @@ def create(
398400 on_complete = on_complete ,
399401 allow_destructive_snapshots = allow_destructive_snapshots or set (),
400402 allow_additive_snapshots = allow_additive_snapshots or set (),
403+ owner = owner ,
401404 )
402405 return CompletionStatus .SUCCESS
403406
404407 def create_physical_schemas (
405- self , snapshots : t .Iterable [Snapshot ], deployability_index : DeployabilityIndex
408+ self ,
409+ snapshots : t .Iterable [Snapshot ],
410+ deployability_index : DeployabilityIndex ,
411+ owner : t .Optional [str ] = None ,
406412 ) -> None :
407413 """Creates the physical schemas for the given snapshots.
408414
409415 Args:
410416 snapshots: Snapshots to create physical schemas for.
411417 deployability_index: Determines snapshots that are deployable in the context of this creation.
418+ owner: Optional principal to set as schema owner after creation.
412419 """
413420 tables_by_gateway : t .Dict [t .Optional [str ], t .List [str ]] = defaultdict (list )
414421 for snapshot in snapshots :
@@ -420,7 +427,7 @@ def create_physical_schemas(
420427 gateway_table_pairs = [
421428 (gateway , table ) for gateway , tables in tables_by_gateway .items () for table in tables
422429 ]
423- self ._create_schemas (gateway_table_pairs = gateway_table_pairs )
430+ self ._create_schemas (gateway_table_pairs = gateway_table_pairs , owner = owner )
424431
425432 def get_snapshots_to_create (
426433 self , target_snapshots : t .Iterable [Snapshot ], deployability_index : DeployabilityIndex
@@ -453,6 +460,7 @@ def _create_snapshots(
453460 on_complete : t .Optional [t .Callable [[SnapshotInfoLike ], None ]],
454461 allow_destructive_snapshots : t .Set [str ],
455462 allow_additive_snapshots : t .Set [str ],
463+ owner : t .Optional [str ] = None ,
456464 ) -> None :
457465 """Internal method to create tables in parallel."""
458466 with self .concurrent_context ():
@@ -465,6 +473,7 @@ def _create_snapshots(
465473 allow_destructive_snapshots = allow_destructive_snapshots ,
466474 allow_additive_snapshots = allow_additive_snapshots ,
467475 on_complete = on_complete ,
476+ owner = owner ,
468477 ),
469478 self .ddl_concurrent_tasks ,
470479 raise_on_error = False ,
@@ -870,6 +879,7 @@ def create_snapshot(
870879 allow_destructive_snapshots : t .Set [str ],
871880 allow_additive_snapshots : t .Set [str ],
872881 on_complete : t .Optional [t .Callable [[SnapshotInfoLike ], None ]] = None ,
882+ owner : t .Optional [str ] = None ,
873883 ) -> None :
874884 """Creates a physical table for the given snapshot.
875885
@@ -880,6 +890,7 @@ def create_snapshot(
880890 on_complete: A callback to call on each successfully created database object.
881891 allow_destructive_snapshots: Snapshots for which destructive schema changes are allowed.
882892 allow_additive_snapshots: Snapshots for which additive schema changes are allowed.
893+ owner: Optional principal to set as table owner after creation.
883894 """
884895 if not snapshot .is_model :
885896 return
@@ -907,6 +918,9 @@ def create_snapshot(
907918 ** create_render_kwargs
908919 )
909920
921+ is_table_deployable = deployability_index .is_deployable (snapshot )
922+ table_name = snapshot .table_name (is_deployable = is_table_deployable )
923+
910924 if self ._can_clone (snapshot , deployability_index ):
911925 self ._clone_snapshot_in_dev (
912926 snapshot = snapshot ,
@@ -919,17 +933,19 @@ def create_snapshot(
919933 run_pre_post_statements = True ,
920934 )
921935 else :
922- is_table_deployable = deployability_index .is_deployable (snapshot )
923936 self ._execute_create (
924937 snapshot = snapshot ,
925- table_name = snapshot . table_name ( is_deployable = is_table_deployable ) ,
938+ table_name = table_name ,
926939 is_table_deployable = is_table_deployable ,
927940 deployability_index = deployability_index ,
928941 create_render_kwargs = create_render_kwargs ,
929942 rendered_physical_properties = rendered_physical_properties ,
930943 dry_run = True ,
931944 )
932945
946+ if owner and not isinstance (snapshot .model .kind , ViewKind ):
947+ adapter .alter_table_owner (table_name , owner )
948+
933949 evaluation_strategy .run_post_statements (
934950 snapshot = snapshot , render_kwargs = {** create_render_kwargs , "inside_transaction" : False }
935951 )
0 commit comments