Skip to content

Commit c024aea

Browse files
authored
fix: give Python node results and provider setup the selected target (#1529)
* fix: keep Python node results out of per-model target schemas A target schema such as s_${CTX:model.name} resolves only inside a model. sqb build passed the raw template to Python tasks and loaders as their default location, so persisting a node result ran CREATE SCHEMA on the template and the build failed. Build and check now fall back to the adapter's default schema and database when the target location holds a context placeholder. * feat: set up exporter, sink and sensor providers with their target Event exporters and command output sinks bind their providers when the command starts, and that session is shared with build, check and load. It was set up without the target, so a provider used by a sink got setup(None) even inside sqb build. The scope now resolves the selected target first and passes its settings; when the project can't resolve them, ctx stays None and the command still runs. A custom sensor's providers get the settings of the target the sensor evaluates for. Calendars keep ctx None and the providers page says so. * fix: fail build, check and load when the exporter target can't be resolved Exporter and sink providers fell back to setup(None) whenever the selected target couldn't be resolved, so a build with an undefined connection set them up without a target before failing. build, check and load now fail at startup instead, before any provider is set up. Other commands, such as sqb format, still run and pass None. A sensor evaluation fails rather than set its providers up without its target. Target resolution at startup also runs with stdout sent to stderr in machine-output modes, like extension discovery next to it. * refactor: resolve exporter and sensor setup contexts in one helper Exporter, sink and sensor providers now get their setup context from one native host helper. It returns None when the project has no providers, fails build, check and load on an unresolvable target, and ignores the model name that mv, rename and scope take as TARGET. * fix: fail every target command when exporter providers can't get the target Compile, dag, plan, test, seed, query, freshness, audit and debug need the target too, and already fail on an unresolvable one. They now fail before setting up exporter and sink providers, like build, check and load, so only target-free commands give those providers None. * style: format the custom sensor test
1 parent f757e04 commit c024aea

19 files changed

Lines changed: 314 additions & 54 deletions

File tree

‎crates/sqlbuild-model-config/src/templates/_helpers/deferred.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! The `${CTX:key}` and `${name}` placeholders of a deferred target's schema or database.
22
33
/// The deferred-target context marker inside `${...}`.
4-
const CONTEXT_PREFIX: &str = "CTX:";
4+
pub(crate) const CONTEXT_PREFIX: &str = "CTX:";
55
/// The opening of a `${...}` placeholder.
66
pub(crate) const PLACEHOLDER_OPEN: &str = "${";
77
const PLACEHOLDER_CLOSE: char = '}';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//! Whether a target's schema or database resolves only inside a model.
2+
3+
use crate::templates::_helpers::deferred::{CONTEXT_PREFIX, PLACEHOLDER_OPEN, placeholders};
4+
5+
/// Whether `template` holds a `${CTX:key}` placeholder, so it names a location only inside a model.
6+
pub fn holds_context_placeholder(template: &str) -> bool {
7+
!placeholders(
8+
template,
9+
&format!("{PLACEHOLDER_OPEN}{CONTEXT_PREFIX}"),
10+
None,
11+
)
12+
.is_empty()
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod deferred_target_segments;
22
pub mod expand_template_string;
33
pub mod expand_template_text;
4+
pub mod holds_context_placeholder;
45
pub mod template_error_message;

‎crates/sqlbuild-python-base/src/event_exporting/models.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl EventExporterCommandScope {
380380
pub(crate) fn bind_extensions<'py>(
381381
&self,
382382
py: Python<'py>,
383-
project_dir: Option<Bound<'py, PyAny>>,
383+
(project_dir, setup_context): (Option<Bound<'py, PyAny>>, Bound<'py, PyAny>),
384384
(providers, event_exporters, command_output_sinks): (
385385
Bound<'py, PyAny>,
386386
Bound<'py, PyAny>,
@@ -413,6 +413,7 @@ impl EventExporterCommandScope {
413413
&host_entry(py, UserCodeHost::BuildProviderSession)?,
414414
&[
415415
("discovered_providers", providers),
416+
("setup_context", setup_context),
416417
("allow_shared", PyBool::new(py, false).to_owned().into_any()),
417418
],
418419
)?;
@@ -508,7 +509,7 @@ impl EventExporterCommandScope {
508509
) -> PyResult<()> {
509510
self.bind_extensions(
510511
py,
511-
None,
512+
(None, py.None().into_bound(py)),
512513
(
513514
native_field(discovered_inputs, "providers")?,
514515
native_field(discovered_inputs, "event_exporters")?,
@@ -530,7 +531,7 @@ impl EventExporterCommandScope {
530531
options,
531532
(
532533
&["providers", "event_exporters"],
533-
&["project_dir", "command_output_sinks"],
534+
&["project_dir", "command_output_sinks", "setup_context"],
534535
),
535536
)?;
536537
let project_dir: Bound<'_, PyAny> = options.value("project_dir")?;
@@ -542,7 +543,10 @@ impl EventExporterCommandScope {
542543
};
543544
self.bind_extensions(
544545
py,
545-
(!project_dir.is_none()).then_some(project_dir),
546+
(
547+
(!project_dir.is_none()).then_some(project_dir),
548+
options.value("setup_context")?,
549+
),
546550
(
547551
options.value("providers")?,
548552
options.value("event_exporters")?,

‎crates/sqlbuild-python-compile/src/compile_driver/main/setup/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod compile_project_phase;
1111
pub mod compiled_phase;
1212
pub mod expanded_loader_namespace;
1313
pub mod managed_write_schema;
14+
pub mod model_free_locations;
1415
pub mod plan_has_executable_work;
1516
pub mod preserved_loader_schemas;
1617
pub mod progress;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//! The target database and schema that Python nodes use outside any model.
2+
3+
use pyo3::prelude::{Bound, PyAny, PyAnyMethods, PyResult};
4+
use sqlbuild_model_config::templates::main::holds_context_placeholder::holds_context_placeholder;
5+
use sqlbuild_python_base::records::main::fields::field::field as native_field;
6+
7+
/// The compiled project's target database and schema, each from `default` with the adapter method's name when it is unset, empty, or resolves only inside a model, such as a `${CTX:...}` schema.
8+
pub fn model_free_locations<'py>(
9+
project: &Bound<'py, PyAny>,
10+
default: impl Fn(&str) -> PyResult<Bound<'py, PyAny>>,
11+
) -> PyResult<(Bound<'py, PyAny>, Bound<'py, PyAny>)> {
12+
let location = |field: &str, fallback: &str| {
13+
let value = native_field(project, field)?;
14+
match value.extract::<Option<String>>()? {
15+
Some(text) if !text.is_empty() && !holds_context_placeholder(&text) => Ok(value),
16+
_ => default(fallback),
17+
}
18+
};
19+
Ok((
20+
location("effective_target_database", "default_database")?,
21+
location("effective_target_schema", "default_schema")?,
22+
))
23+
}

‎crates/sqlbuild-python/src/bindings/_helpers/build_command/lifecycle.rs‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use sqlbuild_python_base::boundary::main::keywords::keywords;
1818
use sqlbuild_python_base::build_command::main::boolean::boolean;
1919
use sqlbuild_python_base::build_command::main::emit::emit;
2020
use sqlbuild_python_base::build_command::main::frozenset::frozenset;
21-
use sqlbuild_python_base::build_command::main::or_none::or_none;
2221
use sqlbuild_python_base::build_command::main::text::text;
2322
use sqlbuild_python_base::build_command::main::tuple::tuple;
2423
use sqlbuild_python_base::native_host::main::host_entry::host_entry;
@@ -29,6 +28,7 @@ use sqlbuild_python_base::records::main::fields::replaced_keywords::replaced_key
2928
use sqlbuild_python_base::records::types::RecordId;
3029
use sqlbuild_python_compile::compile_driver::main::setup::build_project_relation_targets::build_project_relation_targets;
3130
use sqlbuild_python_compile::compile_driver::main::setup::build_python_relation_targets::build_python_relation_targets;
31+
use sqlbuild_python_compile::compile_driver::main::setup::model_free_locations::model_free_locations;
3232
use sqlbuild_python_compile::compile_driver::main::setup::project_target_settings::project_target_settings;
3333
use sqlbuild_python_compile::compile_driver::models::TargetSettingsValues;
3434
use sqlbuild_python_project::graph::main::python_nodes::build_python_sql_run_lifecycle::build_python_sql_run_lifecycle;
@@ -437,14 +437,7 @@ pub(crate) fn prepare_direct_python_lifecycle<'py>(
437437
connection_config: &connection_config,
438438
project: &project,
439439
reload: context.request_flag("reload_sources")?,
440-
defaults: (
441-
or_none(native_field(&project, "effective_target_database")?, || {
442-
adapter.call_method0("default_database")
443-
})?,
444-
or_none(native_field(&project, "effective_target_schema")?, || {
445-
adapter.call_method0("default_schema")
446-
})?,
447-
),
440+
defaults: model_free_locations(&project, |method| adapter.call_method0(method))?,
448441
target_config: project_target_settings(
449442
&discovered,
450443
&project,

‎crates/sqlbuild-python/src/bindings/_helpers/check/check_command.rs‎

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use sqlbuild_python_base::records::main::construct::record_class::record_class;
2727
use sqlbuild_python_compile::compile_driver::main::setup::build_project_relation_targets::build_project_relation_targets;
2828
use sqlbuild_python_compile::compile_driver::main::setup::build_python_relation_targets::build_python_relation_targets;
2929
use sqlbuild_python_compile::compile_driver::main::setup::build_static_pipeline_result::build_static_pipeline_result;
30+
use sqlbuild_python_compile::compile_driver::main::setup::model_free_locations::model_free_locations;
3031
use sqlbuild_python_compile::compile_driver::main::setup::project_target_settings::project_target_settings;
3132
use sqlbuild_python_compile::compile_driver::models::TargetSettingsValues;
3233
use sqlbuild_python_front::discovery::main::sources::consumer_uses::consumer_uses;
@@ -271,19 +272,6 @@ fn compile_pipeline<'py>(
271272
)
272273
}
273274

274-
fn target_default<'py>(
275-
project: &Bound<'py, PyAny>,
276-
adapter: &Bound<'py, PyAny>,
277-
(field, fallback): (&str, &str),
278-
) -> PyResult<Bound<'py, PyAny>> {
279-
let value: Bound<'py, PyAny> = native_field(project, field)?;
280-
if value.is_none() {
281-
adapter.call_method0(fallback)
282-
} else {
283-
Ok(value)
284-
}
285-
}
286-
287275
/// The discovered Python graph, after refusing unit-test selectors, and the selected checks.
288276
fn python_selection<'py>(
289277
request: &Bound<'py, PyAny>,
@@ -360,18 +348,7 @@ fn prepare<'py>(
360348
lifecycle_plan,
361349
relation_targets,
362350
relation_refs,
363-
defaults: (
364-
target_default(
365-
&project,
366-
&adapter,
367-
("effective_target_database", "default_database"),
368-
)?,
369-
target_default(
370-
&project,
371-
&adapter,
372-
("effective_target_schema", "default_schema"),
373-
)?,
374-
),
351+
defaults: model_free_locations(&project, |method| adapter.call_method0(method))?,
375352
project_relations: object(
376353
py,
377354
build_project_relation_targets(

‎crates/sqlbuild-python/src/bindings/_helpers/cli/observability.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use sqlbuild_core::event_export::constants::DEFAULT_EVENT_EXPORT_SHUTDOWN_TIMEOU
1616
use sqlbuild_host::entries::models::{ExecutorHost, UserCodeHost};
1717

1818
use crate::bindings::_helpers::event_exporting::dispatcher::EventExporterDispatcher;
19+
use crate::bindings::_helpers::native_host::setup_context::invocation_setup_context;
1920
use crate::bindings::_helpers::recall::console::write_stderr;
2021
use crate::bindings::_helpers::result_output::terminal_index::TerminalEventIndex;
2122
use crate::bindings::_helpers::result_output::terminal_index::terminal_event_index_scope;
@@ -407,13 +408,16 @@ impl Observed {
407408
)
408409
.map(pyo3::Bound::into_any)?;
409410
self.exporter_scope = Some(scope.clone().unbind());
411+
let providers: Bound<'_, PyAny> = native_field(&extensions, "providers")?;
412+
let setup_context = invocation_setup_context(args, (project_dir, &providers))?;
410413
call_with(
411414
&native_field(&scope, "configure_extensions")?,
412415
&[
413416
("project_dir", project_dir.clone()),
414-
("providers", native_field(&extensions, "providers")?),
417+
("providers", providers),
415418
("event_exporters", event_exporters),
416419
("command_output_sinks", sinks),
420+
("setup_context", setup_context),
417421
],
418422
)?;
419423
self.unsubscribe_exporters =

‎crates/sqlbuild-python/src/bindings/_helpers/native_host/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
pub(crate) mod orchestration_sensors;
44
pub(crate) mod registration;
55
pub(crate) mod resource_costs_state;
6+
pub(crate) mod setup_context;
67
pub(crate) mod statement_report;
78
pub(crate) mod warehouse_metadata;

0 commit comments

Comments
 (0)