11//! Execute the prepared clone plan and propagate fingerprints.
22
3- use std:: collections:: HashMap ;
3+ use std:: collections:: { HashMap , HashSet } ;
44use std:: time:: Instant ;
55
66use pyo3:: prelude:: { Bound , Py , PyAny , PyAnyMethods , PyResult , Python } ;
@@ -13,6 +13,7 @@ use crate::bindings::_helpers::clone::clone_command::Invocation;
1313use crate :: bindings:: _helpers:: clone:: clone_progress:: {
1414 CloneFingerprintReporter , CloneItemPrinter ,
1515} ;
16+ use sqlbuild_analysis:: fingerprint_storage:: constants:: { NODE_TYPE_MODEL , NODE_TYPE_SEED } ;
1617use sqlbuild_python_base:: boundary:: main:: call_with:: call_with;
1718use sqlbuild_python_base:: boundary:: main:: within:: within;
1819use sqlbuild_python_base:: native_host:: main:: host_entry:: host_entry;
@@ -23,7 +24,10 @@ use sqlbuild_python_base::records::main::fields::field::field as native_field;
2324use sqlbuild_python_base:: records:: main:: fields:: mapping_items:: mapping_items;
2425use sqlbuild_python_base:: records:: types:: RecordId ;
2526use sqlbuild_python_front:: contracts:: main:: resolve_target_config:: resolve_target_config;
27+ use sqlbuild_python_front:: native_host:: main:: append_replaced_consumption:: append_replaced_consumption;
2628use sqlbuild_python_output:: result_output:: models:: ExecutionEventWriter ;
29+ use sqlbuild_python_planner:: consumption:: main:: relation:: relation;
30+ use sqlbuild_state:: consumption:: models:: { NodeKey , RelationKey } ;
2731
2832const DESTINATION_ENTRY_GROUPS : [ & str ; 4 ] = [
2933 "destination_source_entries" ,
@@ -37,6 +41,13 @@ const ORIGIN_ENTRY_GROUPS: [&str; 3] = [
3741 "origin_seed_entries" ,
3842] ;
3943
44+ /// A clone item that finished cleanly.
45+ const SUCCESS_STATUS : & str = "success" ;
46+ /// Clone actions that replace the destination relation with the origin's data.
47+ const REPLACING_ACTIONS : [ & str ; 2 ] = [ "cloned" , "copied" ] ;
48+ /// The materialization of a model the clone recreates as a view rather than replacing.
49+ const VIEW_MATERIALIZATION : & str = "view" ;
50+
4051/// The pipeline's plan entries of `groups`, in order.
4152fn entries < ' py > ( pipeline : & Bound < ' py , PyAny > , groups : & [ & str ] ) -> PyResult < Vec < Bound < ' py , PyAny > > > {
4253 let mut found: Vec < Bound < ' py , PyAny > > = Vec :: new ( ) ;
@@ -310,6 +321,57 @@ fn copy_fingerprints<'py>(
310321 Ok ( ( ) )
311322}
312323
324+ /// Mark each table and seed the clone replaced as having no completed consumption record, so `--changed` rebuilds it once.
325+ pub ( crate ) fn forget_replaced_consumption (
326+ ( adapter, connection) : ( & Bound < ' _ , PyAny > , & Bound < ' _ , PyAny > ) ,
327+ pipeline : & Bound < ' _ , PyAny > ,
328+ result : & Bound < ' _ , PyAny > ,
329+ ) -> PyResult < ( ) > {
330+ let mut replaced_names: HashSet < String > = HashSet :: new ( ) ;
331+ for item in native_field ( result, "item_results" ) ?. try_iter ( ) ? {
332+ let item: Bound < ' _ , PyAny > = item?;
333+ let status: String = native_field ( & native_field ( & item, "status" ) ?, "value" ) ?. extract ( ) ?;
334+ let action: String = native_field ( & native_field ( & item, "action" ) ?, "value" ) ?. extract ( ) ?;
335+ if status == SUCCESS_STATUS && REPLACING_ACTIONS . contains ( & action. as_str ( ) ) {
336+ replaced_names. insert ( native_field ( & item, "name" ) ?. extract ( ) ?) ;
337+ }
338+ }
339+ let mut replaced: Vec < ( NodeKey , RelationKey ) > = Vec :: new ( ) ;
340+ for ( group, kind) in [
341+ ( "destination_model_entries" , NODE_TYPE_MODEL ) ,
342+ ( "destination_seed_entries" , NODE_TYPE_SEED ) ,
343+ ] {
344+ for entry in native_field ( pipeline, group) ?. try_iter ( ) ? {
345+ let entry: Bound < ' _ , PyAny > = entry?;
346+ let name: String = native_field ( & entry, "name" ) ?. extract ( ) ?;
347+ let destination: Bound < ' _ , PyAny > = native_field ( & entry, "destination" ) ?;
348+ let view: bool = kind == NODE_TYPE_MODEL
349+ && native_field ( & native_field ( & entry, "materialization_type" ) ?, "value" ) ?
350+ . extract :: < String > ( ) ?
351+ == VIEW_MATERIALIZATION ;
352+ if view
353+ || !replaced_names. contains ( & name)
354+ || native_field ( & destination, "schema" ) ?. is_none ( )
355+ {
356+ continue ;
357+ }
358+ replaced. push ( (
359+ NodeKey {
360+ kind : kind. to_owned ( ) ,
361+ name,
362+ } ,
363+ relation ( & destination) ?,
364+ ) ) ;
365+ }
366+ }
367+ let project: Bound < ' _ , PyAny > = native_field ( pipeline, "destination_project" ) ?;
368+ append_replaced_consumption (
369+ ( adapter, connection) ,
370+ & native_field ( & project, "run_id" ) ?. extract :: < String > ( ) ?,
371+ replaced,
372+ )
373+ }
374+
313375/// Execute the clone, streaming items, then copy fingerprints; returns the result and elapsed seconds.
314376pub ( crate ) fn execute_clone_plan < ' py > (
315377 request : & Bound < ' py , PyAny > ,
@@ -352,5 +414,6 @@ pub(crate) fn execute_clone_plan<'py>(
352414 let result: Bound < ' py , PyAny > = executed?;
353415 closed?;
354416 copy_fingerprints ( invocation, ( pipeline, connection) , & result) ?;
417+ forget_replaced_consumption ( ( & invocation. adapter , connection) , pipeline, & result) ?;
355418 Ok ( ( result, started. elapsed ( ) . as_secs_f64 ( ) ) )
356419}
0 commit comments