|
| 1 | +//! The message Python raises for a target namespace check failure. |
| 2 | +
|
| 3 | +use sqlbuild_core::text::main::python_str_repr::python_str_repr; |
| 4 | + |
| 5 | +use crate::assembly::target_checks::constants::PRESERVE_TARGET_VALUE; |
| 6 | +use crate::assembly::target_checks::errors::TargetError; |
| 7 | +use crate::assembly::target_checks::main::missing_target_parts::missing_target_parts; |
| 8 | + |
| 9 | +/// Python's `repr` of an optional string. |
| 10 | +fn optional_repr(value: Option<&String>) -> String { |
| 11 | + value.map_or_else(|| "None".to_owned(), |text| python_str_repr(text)) |
| 12 | +} |
| 13 | + |
| 14 | +/// The `PlannerInputError` message of `error`. |
| 15 | +pub fn target_error_message(error: &TargetError) -> String { |
| 16 | + match error { |
| 17 | + TargetError::MissingParts(parts) => format!( |
| 18 | + "{} execution requires explicit target {}. {} '{}' resolved to database={} schema={}. \ |
| 19 | + Set them in the model config, the environment config, or the project defaults.", |
| 20 | + parts.adapter_name, |
| 21 | + missing_target_parts(parts.database.as_deref(), parts.schema.as_deref()).join(", "), |
| 22 | + parts.resource_kind, |
| 23 | + parts.resource_name, |
| 24 | + optional_repr(parts.database.as_ref()), |
| 25 | + optional_repr(parts.schema.as_ref()), |
| 26 | + ), |
| 27 | + TargetError::LoaderCollision(collision) => format!( |
| 28 | + "Managed loader write collision: targets '{left}' and '{right}' both resolve to \ |
| 29 | + database={} schema={}. Set distinct targets.{left}.loader_schema and \ |
| 30 | + targets.{right}.loader_schema values.", |
| 31 | + optional_repr(collision.database.as_ref()), |
| 32 | + optional_repr(collision.schema.as_ref()), |
| 33 | + left = collision.left_target, |
| 34 | + right = collision.right_target, |
| 35 | + ), |
| 36 | + TargetError::ManagedWriteSchema(label) => format!( |
| 37 | + "{label} has no explicitly resolved physical write schema. Set schema on the \ |
| 38 | + resource or its defaults, selected target, loader_schema, or connection." |
| 39 | + ), |
| 40 | + TargetError::NamedTargetSchema(target) => format!( |
| 41 | + "Named target '{target}' must explicitly set schema to a nonblank literal or \ |
| 42 | + '{PRESERVE_TARGET_VALUE}'. Resource schemas, defaults, and connection schemas do \ |
| 43 | + not select a named target schema strategy." |
| 44 | + ), |
| 45 | + } |
| 46 | +} |
0 commit comments