@@ -20,9 +20,9 @@ use sqlbuild_orchestration::runs::types::{CommandPlanner, PublishedJobs};
2020
2121use crate :: adhoc:: constants:: {
2222 ADHOC_VERBS , ALLOWED_DESTS , BUILD_VERB , CHANGED_COUNTS_DEST , CHANGED_DEST , COMMAND_DEST ,
23- FRESHNESS_VERB , INCLUDE_PYTHON_DEST , INTEGER_CONVERTERS , LOAD_SOURCES_DEST ,
24- POSITIVE_INTEGER_CONVERTER , REFUSED_DESTS , SEED_VERB , SELECTOR_DESTS , SQL_TEST_KIND ,
25- UNIT_TEST_VERBS ,
23+ FRESHNESS_VERB , INCLUDE_PYTHON_DEST , INTEGER_CONVERTERS , LOAD_BUILD_SETTINGS ,
24+ LOAD_SOURCES_DEST , LOAD_VERB , POSITIVE_INTEGER_CONVERTER , REFUSED_DESTS , RELOAD_DEST ,
25+ SEED_VERB , SELECTOR_DESTS , SQL_TEST_KIND , UNIT_TEST_VERBS , VARS_CONVERTER , VARS_DEST ,
2626} ;
2727use crate :: argv:: main:: command_settings_arguments:: command_settings_arguments;
2828use crate :: argv:: main:: job_settings_arguments:: job_settings_arguments;
@@ -44,7 +44,7 @@ impl CommandPlanner for CliPlanner {
4444 . ok_or_else ( || "enter a command, such as build --select orders" . to_owned ( ) ) ?;
4545 if !ADHOC_VERBS . contains ( & verb. as_str ( ) ) {
4646 return Err ( format ! (
47- "sqb {verb} is not available as an ad-hoc run; use build, test, seed, audit or freshness "
47+ "sqb {verb} is not available as an ad-hoc run; use build, test, seed, audit, freshness or load "
4848 ) ) ;
4949 }
5050 let parsed = parse ( command) ?;
@@ -66,9 +66,21 @@ impl CommandPlanner for CliPlanner {
6666 if dest == CHANGED_DEST && settings. contains_key ( CHANGED_DEST ) {
6767 continue ;
6868 }
69- settings. insert ( dest. clone ( ) , setting ( dest, value) ?) ;
69+ let setting = setting ( dest, value) ?;
70+ if dest == VARS_DEST && setting. as_object ( ) . is_some_and ( serde_json:: Map :: is_empty) {
71+ continue ;
72+ }
73+ settings. insert ( dest. clone ( ) , setting) ;
74+ }
75+ if verb == LOAD_VERB {
76+ for ( dest, value) in LOAD_BUILD_SETTINGS {
77+ settings. insert ( dest. to_owned ( ) , json ! ( value) ) ;
78+ }
79+ if settings. get ( RELOAD_DEST ) == Some ( & Json :: Bool ( true ) ) {
80+ settings. remove ( LOAD_SOURCES_DEST ) ;
81+ }
7082 }
71- if verb == BUILD_VERB {
83+ if verb == BUILD_VERB || verb == LOAD_VERB {
7284 job_settings_arguments ( & settings) ?;
7385 } else {
7486 command_settings_arguments ( verb, & settings) ?;
@@ -88,6 +100,7 @@ impl CommandPlanner for CliPlanner {
88100 seeds
89101 }
90102 FRESHNESS_VERB => freshness_sources ( publication, & select, & exclude) ?,
103+ LOAD_VERB => managed_sources ( publication, & select, & exclude) ?,
91104 _ => {
92105 let python = verb == BUILD_VERB
93106 && settings. get ( INCLUDE_PYTHON_DEST ) != Some ( & Json :: Bool ( false ) ) ;
@@ -102,11 +115,22 @@ impl CommandPlanner for CliPlanner {
102115 }
103116}
104117
105- /// Whole numbers convert as the parser needs them; paths and variables stay text so the refusal can name them.
118+ /// Whole numbers convert as the parser needs them; `--vars` must be a JSON object; paths stay text so the refusal can name them.
106119struct PlannerHost ;
107120
108121impl ArgvHost for PlannerHost {
109122 fn convert ( & mut self , converter : & str , text : & str ) -> Result < Value , TypeFailure > {
123+ if converter == VARS_CONVERTER {
124+ return match serde_json:: from_str :: < Json > ( text) {
125+ Ok ( Json :: Object ( _) ) => Ok ( Value :: Str ( text. to_owned ( ) ) ) ,
126+ Ok ( _) => Err ( TypeFailure :: ArgumentType (
127+ "--vars must be a JSON object" . to_owned ( ) ,
128+ ) ) ,
129+ Err ( error) => Err ( TypeFailure :: ArgumentType ( format ! (
130+ "--vars must be valid JSON: {error}"
131+ ) ) ) ,
132+ } ;
133+ }
110134 if !INTEGER_CONVERTERS . contains ( & converter) {
111135 return Ok ( Value :: Str ( text. to_owned ( ) ) ) ;
112136 }
@@ -165,12 +189,6 @@ fn setting(dest: &str, value: &Value) -> Result<Json, String> {
165189 if let Some ( ( _, flag, reason) ) = REFUSED_DESTS . iter ( ) . find ( |( name, _, _) | * name == dest) {
166190 return Err ( format ! ( "{flag} is not available for ad-hoc runs: {reason}" ) ) ;
167191 }
168- if dest == LOAD_SOURCES_DEST && * value == Value :: Bool ( true ) {
169- return Err (
170- "--load is not available for ad-hoc runs: source loads are not part of ad-hoc runs yet"
171- . into ( ) ,
172- ) ;
173- }
174192 if !ALLOWED_DESTS . contains ( & dest) {
175193 return Err ( format ! (
176194 "{} is not available for ad-hoc runs" ,
@@ -183,6 +201,8 @@ fn setting(dest: &str, value: &Value) -> Result<Json, String> {
183201 . parse :: < i64 > ( )
184202 . map ( |number| json ! ( number) )
185203 . map_err ( |_| format ! ( "{} must be a whole number" , flag_of( dest) ) ) ,
204+ Value :: Str ( text) if dest == VARS_DEST => serde_json:: from_str ( text)
205+ . map_err ( |error| format ! ( "--vars must be valid JSON: {error}" ) ) ,
186206 Value :: Str ( text) => Ok ( json ! ( text) ) ,
187207 _ => Err ( format ! (
188208 "{} is not available for ad-hoc runs" ,
@@ -362,6 +382,28 @@ fn published_jobs(jobs: &PublishedJobs, include_python: bool) -> BTreeMap<String
362382 selections
363383}
364384
385+ /// Managed sources `sqb load` runs; a source only a loader writes is not published as a resource but is managed.
386+ fn managed_sources (
387+ publication : ( & [ PlannerResource ] , & PublishedJobs ) ,
388+ select : & [ String ] ,
389+ exclude : & [ String ] ,
390+ ) -> Result < BTreeSet < String > , String > {
391+ let unmanaged: BTreeSet < & str > = publication
392+ . 0
393+ . iter ( )
394+ . filter ( |resource| resource. loader . is_none ( ) )
395+ . map ( |resource| resource. key . as_str ( ) )
396+ . collect ( ) ;
397+ let sources: BTreeSet < String > = resolved ( publication, select, exclude, true ) ?
398+ . into_iter ( )
399+ . filter ( |key| key. starts_with ( "source:" ) && !unmanaged. contains ( key. as_str ( ) ) )
400+ . collect ( ) ;
401+ if sources. is_empty ( ) {
402+ return Err ( "the selectors match no managed sources in the current publication" . into ( ) ) ;
403+ }
404+ Ok ( sources)
405+ }
406+
365407/// Sources `sqb freshness` observes: selected or upstream sources, less those the exclusions reach.
366408fn freshness_sources (
367409 publication : ( & [ PlannerResource ] , & PublishedJobs ) ,
0 commit comments