From d1857b39ca617abd485960c5571b23d5899e39c8 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Mon, 30 Sep 2024 16:32:37 +0200 Subject: [PATCH] state:modified vars, behind "state_modified_compare_vars" behaviour flag (#10793) --- .../unreleased/Features-20240729-173203.yaml | 7 + core/dbt/artifacts/resources/v1/components.py | 1 + core/dbt/artifacts/resources/v1/exposure.py | 1 + .../resources/v1/source_definition.py | 1 + core/dbt/context/configured.py | 38 ++- core/dbt/context/providers.py | 8 + core/dbt/contracts/files.py | 17 ++ core/dbt/contracts/graph/nodes.py | 32 +++ core/dbt/contracts/project.py | 2 + core/dbt/graph/selector_methods.py | 1 + core/dbt/parser/schema_yaml_readers.py | 5 +- core/dbt/parser/schemas.py | 18 +- core/dbt/parser/sources.py | 6 + schemas/dbt/manifest/v12.json | 216 ++++++++++++++++++ .../functional/artifacts/expected_manifest.py | 29 ++- .../defer_state/test_modified_state.py | 213 +++++++++++++++++ tests/functional/list/test_list.py | 5 +- tests/unit/contracts/graph/test_manifest.py | 1 + tests/unit/contracts/graph/test_nodes.py | 8 + .../unit/contracts/graph/test_nodes_parsed.py | 19 +- tests/unit/graph/test_selector_methods.py | 6 + tests/unit/parser/test_parser.py | 1 + 22 files changed, 614 insertions(+), 21 deletions(-) create mode 100644 .changes/unreleased/Features-20240729-173203.yaml diff --git a/.changes/unreleased/Features-20240729-173203.yaml b/.changes/unreleased/Features-20240729-173203.yaml new file mode 100644 index 00000000000..e788c8e9cc9 --- /dev/null +++ b/.changes/unreleased/Features-20240729-173203.yaml @@ -0,0 +1,7 @@ +kind: Features +body: Include models that depend on changed vars in state:modified, add state:modified.vars + selection method +time: 2024-07-29T17:32:03.368508-04:00 +custom: + Author: michelleark + Issue: "4304" diff --git a/core/dbt/artifacts/resources/v1/components.py b/core/dbt/artifacts/resources/v1/components.py index 8eb43f35d8e..02bfa5d875d 100644 --- a/core/dbt/artifacts/resources/v1/components.py +++ b/core/dbt/artifacts/resources/v1/components.py @@ -197,6 +197,7 @@ class ParsedResource(ParsedResourceMandatory): unrendered_config_call_dict: Dict[str, Any] = field(default_factory=dict) relation_name: Optional[str] = None raw_code: str = "" + vars: Dict[str, Any] = field(default_factory=dict) def __post_serialize__(self, dct: Dict, context: Optional[Dict] = None): dct = super().__post_serialize__(dct, context) diff --git a/core/dbt/artifacts/resources/v1/exposure.py b/core/dbt/artifacts/resources/v1/exposure.py index 00f3c8b89e1..7d8c291381b 100644 --- a/core/dbt/artifacts/resources/v1/exposure.py +++ b/core/dbt/artifacts/resources/v1/exposure.py @@ -41,6 +41,7 @@ class Exposure(GraphResource): tags: List[str] = field(default_factory=list) config: ExposureConfig = field(default_factory=ExposureConfig) unrendered_config: Dict[str, Any] = field(default_factory=dict) + vars: Dict[str, Any] = field(default_factory=dict) url: Optional[str] = None depends_on: DependsOn = field(default_factory=DependsOn) refs: List[RefArgs] = field(default_factory=list) diff --git a/core/dbt/artifacts/resources/v1/source_definition.py b/core/dbt/artifacts/resources/v1/source_definition.py index 6c1c3679a00..2650486baa0 100644 --- a/core/dbt/artifacts/resources/v1/source_definition.py +++ b/core/dbt/artifacts/resources/v1/source_definition.py @@ -69,5 +69,6 @@ class SourceDefinition(ParsedSourceMandatory): config: SourceConfig = field(default_factory=SourceConfig) patch_path: Optional[str] = None unrendered_config: Dict[str, Any] = field(default_factory=dict) + vars: Dict[str, Any] = field(default_factory=dict) relation_name: Optional[str] = None created_at: float = field(default_factory=lambda: time.time()) diff --git a/core/dbt/context/configured.py b/core/dbt/context/configured.py index 240d9afb843..9ee7ebc79c3 100644 --- a/core/dbt/context/configured.py +++ b/core/dbt/context/configured.py @@ -31,23 +31,35 @@ def __init__(self, package_name: str): self.resource_type = NodeType.Model +class SchemaYamlVars: + def __init__(self): + self.env_vars = {} + self.vars = {} + + class ConfiguredVar(Var): def __init__( self, context: Dict[str, Any], config: AdapterRequiredConfig, project_name: str, + schema_yaml_vars: Optional[SchemaYamlVars] = None, ): super().__init__(context, config.cli_vars) self._config = config self._project_name = project_name + self.schema_yaml_vars = schema_yaml_vars def __call__(self, var_name, default=Var._VAR_NOTSET): my_config = self._config.load_dependencies()[self._project_name] + var_found = False + var_value = None + # cli vars > active project > local project if var_name in self._config.cli_vars: - return self._config.cli_vars[var_name] + var_found = True + var_value = self._config.cli_vars[var_name] adapter_type = self._config.credentials.type lookup = FQNLookup(self._project_name) @@ -58,19 +70,21 @@ def __call__(self, var_name, default=Var._VAR_NOTSET): all_vars.add(my_config.vars.vars_for(lookup, adapter_type)) all_vars.add(active_vars) - if var_name in all_vars: - return all_vars[var_name] + if not var_found and var_name in all_vars: + var_found = True + var_value = all_vars[var_name] - if default is not Var._VAR_NOTSET: - return default - - return self.get_missing_var(var_name) + if not var_found and default is not Var._VAR_NOTSET: + var_found = True + var_value = default + if not var_found: + return self.get_missing_var(var_name) + else: + if self.schema_yaml_vars: + self.schema_yaml_vars.vars[var_name] = var_value -class SchemaYamlVars: - def __init__(self): - self.env_vars = {} - self.vars = {} + return var_value class SchemaYamlContext(ConfiguredContext): @@ -82,7 +96,7 @@ def __init__(self, config, project_name: str, schema_yaml_vars: Optional[SchemaY @contextproperty() def var(self) -> ConfiguredVar: - return ConfiguredVar(self._ctx, self.config, self._project_name) + return ConfiguredVar(self._ctx, self.config, self._project_name, self.schema_yaml_vars) @contextmember() def env_var(self, var: str, default: Optional[str] = None) -> str: diff --git a/core/dbt/context/providers.py b/core/dbt/context/providers.py index dfc8c9bb40b..a0e3751587a 100644 --- a/core/dbt/context/providers.py +++ b/core/dbt/context/providers.py @@ -790,6 +790,14 @@ def get_missing_var(self, var_name): # in the parser, just always return None. return None + def __call__(self, var_name: str, default: Any = ModelConfiguredVar._VAR_NOTSET) -> Any: + var_value = super().__call__(var_name, default) + + if self._node and hasattr(self._node, "vars"): + self._node.vars[var_name] = var_value + + return var_value + class RuntimeVar(ModelConfiguredVar): pass diff --git a/core/dbt/contracts/files.py b/core/dbt/contracts/files.py index 04d7909133a..f62ab6ae9b8 100644 --- a/core/dbt/contracts/files.py +++ b/core/dbt/contracts/files.py @@ -213,6 +213,7 @@ class SchemaSourceFile(BaseSourceFile): sop: List[SourceKey] = field(default_factory=list) env_vars: Dict[str, Any] = field(default_factory=dict) unrendered_configs: Dict[str, Any] = field(default_factory=dict) + vars: Dict[str, Any] = field(default_factory=dict) pp_dict: Optional[Dict[str, Any]] = None pp_test_index: Optional[Dict[str, Any]] = None @@ -353,6 +354,22 @@ def delete_from_unrendered_configs(self, yaml_key, name): if not self.unrendered_configs[yaml_key]: del self.unrendered_configs[yaml_key] + def add_vars(self, vars: Dict[str, Any], yaml_key: str, name: str) -> None: + if yaml_key not in self.vars: + self.vars[yaml_key] = {} + + if name not in self.vars[yaml_key]: + self.vars[yaml_key][name] = vars + + def get_vars(self, yaml_key: str, name: str) -> Dict[str, Any]: + if yaml_key not in self.vars: + return {} + + if name not in self.vars[yaml_key]: + return {} + + return self.vars[yaml_key][name] + def add_env_var(self, var, yaml_key, name): if yaml_key not in self.env_vars: self.env_vars[yaml_key] = {} diff --git a/core/dbt/contracts/graph/nodes.py b/core/dbt/contracts/graph/nodes.py index d04d03d0f81..059d9ff3231 100644 --- a/core/dbt/contracts/graph/nodes.py +++ b/core/dbt/contracts/graph/nodes.py @@ -369,6 +369,9 @@ def same_contract(self, old, adapter_type=None) -> bool: # This would only apply to seeds return True + def same_vars(self, old) -> bool: + return self.vars == old.vars + def same_contents(self, old, adapter_type) -> bool: if old is None: return False @@ -376,12 +379,20 @@ def same_contents(self, old, adapter_type) -> bool: # Need to ensure that same_contract is called because it # could throw an error same_contract = self.same_contract(old, adapter_type) + + # Legacy behaviour + if not get_flags().state_modified_compare_vars: + same_vars = True + else: + same_vars = self.same_vars(old) + return ( self.same_body(old) and self.same_config(old) and self.same_persisted_description(old) and self.same_fqn(old) and self.same_database_representation(old) + and same_vars and same_contract and True ) @@ -1251,6 +1262,9 @@ def same_config(self, old: "SourceDefinition") -> bool: old.unrendered_config, ) + def same_vars(self, other: "SourceDefinition") -> bool: + return self.vars == other.vars + def same_contents(self, old: Optional["SourceDefinition"]) -> bool: # existing when it didn't before is a change! if old is None: @@ -1264,6 +1278,12 @@ def same_contents(self, old: Optional["SourceDefinition"]) -> bool: # freshness changes are changes, I guess # metadata/tags changes are not "changes" # patching/description changes are not "changes" + # Legacy behaviour + if not get_flags().state_modified_compare_vars: + same_vars = True + else: + same_vars = self.same_vars(old) + return ( self.same_database_representation(old) and self.same_fqn(old) @@ -1271,6 +1291,7 @@ def same_contents(self, old: Optional["SourceDefinition"]) -> bool: and self.same_quoting(old) and self.same_freshness(old) and self.same_external(old) + and same_vars and True ) @@ -1367,12 +1388,21 @@ def same_config(self, old: "Exposure") -> bool: old.unrendered_config, ) + def same_vars(self, old: "Exposure") -> bool: + return self.vars == old.vars + def same_contents(self, old: Optional["Exposure"]) -> bool: # existing when it didn't before is a change! # metadata/tags changes are not "changes" if old is None: return True + # Legacy behaviour + if not get_flags().state_modified_compare_vars: + same_vars = True + else: + same_vars = self.same_vars(old) + return ( self.same_fqn(old) and self.same_exposure_type(old) @@ -1383,6 +1413,7 @@ def same_contents(self, old: Optional["Exposure"]) -> bool: and self.same_label(old) and self.same_depends_on(old) and self.same_config(old) + and same_vars and True ) @@ -1634,6 +1665,7 @@ class ParsedNodePatch(ParsedPatch): latest_version: Optional[NodeVersion] constraints: List[Dict[str, Any]] deprecation_date: Optional[datetime] + vars: Dict[str, Any] time_spine: Optional[TimeSpine] = None diff --git a/core/dbt/contracts/project.py b/core/dbt/contracts/project.py index 5c3b80e34f0..f5a4ec605ec 100644 --- a/core/dbt/contracts/project.py +++ b/core/dbt/contracts/project.py @@ -343,6 +343,7 @@ class ProjectFlags(ExtensibleDbtClassMixin): source_freshness_run_project_hooks: bool = False skip_nodes_if_on_run_start_fails: bool = False state_modified_compare_more_unrendered_values: bool = False + state_modified_compare_vars: bool = False @property def project_only_flags(self) -> Dict[str, Any]: @@ -352,6 +353,7 @@ def project_only_flags(self) -> Dict[str, Any]: "source_freshness_run_project_hooks": self.source_freshness_run_project_hooks, "skip_nodes_if_on_run_start_fails": self.skip_nodes_if_on_run_start_fails, "state_modified_compare_more_unrendered_values": self.state_modified_compare_more_unrendered_values, + "state_modified_compare_vars": self.state_modified_compare_vars, } diff --git a/core/dbt/graph/selector_methods.py b/core/dbt/graph/selector_methods.py index dbeaf7ed4c3..c7a2490760b 100644 --- a/core/dbt/graph/selector_methods.py +++ b/core/dbt/graph/selector_methods.py @@ -752,6 +752,7 @@ def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[Uniqu "modified.relation": self.check_modified_factory("same_database_representation"), "modified.macros": self.check_modified_macros, "modified.contract": self.check_modified_contract("same_contract", adapter_type), + "modified.vars": self.check_modified_factory("same_vars"), } if selector in state_checks: checker = state_checks[selector] diff --git a/core/dbt/parser/schema_yaml_readers.py b/core/dbt/parser/schema_yaml_readers.py index dc99e87a218..4fe28a74866 100644 --- a/core/dbt/parser/schema_yaml_readers.py +++ b/core/dbt/parser/schema_yaml_readers.py @@ -91,6 +91,9 @@ def parse_exposure(self, unparsed: UnparsedExposure) -> None: unique_id = f"{NodeType.Exposure}.{package_name}.{unparsed.name}" path = self.yaml.path.relative_path + assert isinstance(self.yaml.file, SchemaSourceFile) + exposure_vars = self.yaml.file.get_vars(self.key, unparsed.name) + fqn = self.schema_parser.get_fqn_prefix(path) fqn.append(unparsed.name) @@ -133,6 +136,7 @@ def parse_exposure(self, unparsed: UnparsedExposure) -> None: maturity=unparsed.maturity, config=config, unrendered_config=unrendered_config, + vars=exposure_vars, ) ctx = generate_parse_exposure( parsed, @@ -144,7 +148,6 @@ def parse_exposure(self, unparsed: UnparsedExposure) -> None: get_rendered(depends_on_jinja, ctx, parsed, capture_macros=True) # parsed now has a populated refs/sources/metrics - assert isinstance(self.yaml.file, SchemaSourceFile) if parsed.config.enabled: self.manifest.add_exposure(self.yaml.file, parsed) else: diff --git a/core/dbt/parser/schemas.py b/core/dbt/parser/schemas.py index 29f36f96b34..3bde9a2440a 100644 --- a/core/dbt/parser/schemas.py +++ b/core/dbt/parser/schemas.py @@ -403,10 +403,14 @@ def get_key_dicts(self) -> Iterable[Dict[str, Any]]: if self.schema_yaml_vars.env_vars: self.schema_parser.manifest.env_vars.update(self.schema_yaml_vars.env_vars) - for var in self.schema_yaml_vars.env_vars.keys(): - schema_file.add_env_var(var, self.key, entry["name"]) + for env_var in self.schema_yaml_vars.env_vars.keys(): + schema_file.add_env_var(env_var, self.key, entry["name"]) self.schema_yaml_vars.env_vars = {} + if self.schema_yaml_vars.vars: + schema_file.add_vars(self.schema_yaml_vars.vars, self.key, entry["name"]) + self.schema_yaml_vars.vars = {} + yield entry def render_entry(self, dct): @@ -677,6 +681,9 @@ def parse_patch(self, block: TargetBlock[NodeTarget], refs: ParserRef) -> None: # code consistency. deprecation_date: Optional[datetime.datetime] = None time_spine: Optional[TimeSpine] = None + assert isinstance(self.yaml.file, SchemaSourceFile) + source_file: SchemaSourceFile = self.yaml.file + if isinstance(block.target, UnparsedModelUpdate): deprecation_date = block.target.deprecation_date time_spine = ( @@ -709,9 +716,9 @@ def parse_patch(self, block: TargetBlock[NodeTarget], refs: ParserRef) -> None: constraints=block.target.constraints, deprecation_date=deprecation_date, time_spine=time_spine, + vars=source_file.get_vars(block.target.yaml_key, block.target.name), ) - assert isinstance(self.yaml.file, SchemaSourceFile) - source_file: SchemaSourceFile = self.yaml.file + if patch.yaml_key in ["models", "seeds", "snapshots"]: unique_id = self.manifest.ref_lookup.get_unique_id( patch.name, self.project.project_name, None @@ -805,6 +812,8 @@ def patch_node_properties(self, node, patch: "ParsedNodePatch") -> None: node.description = patch.description node.columns = patch.columns node.name = patch.name + # Prefer node-level vars to vars from patch + node.vars = {**patch.vars, **node.vars} if not isinstance(node, ModelNode): for attr in ["latest_version", "access", "version", "constraints"]: @@ -954,6 +963,7 @@ def parse_patch(self, block: TargetBlock[UnparsedModelUpdate], refs: ParserRef) latest_version=latest_version, constraints=unparsed_version.constraints or target.constraints, deprecation_date=unparsed_version.deprecation_date, + vars=source_file.get_vars(block.target.yaml_key, block.target.name), ) # Node patched before config because config patching depends on model name, # which may have been updated in the version patch diff --git a/core/dbt/parser/sources.py b/core/dbt/parser/sources.py index 68dbed94ce5..4aa08787b37 100644 --- a/core/dbt/parser/sources.py +++ b/core/dbt/parser/sources.py @@ -12,6 +12,7 @@ ContextConfigGenerator, UnrenderedConfigGenerator, ) +from dbt.contracts.files import SchemaSourceFile from dbt.contracts.graph.manifest import Manifest, SourceKey from dbt.contracts.graph.nodes import ( GenericTestNode, @@ -158,6 +159,10 @@ def parse_source(self, target: UnpatchedSourceDefinition) -> SourceDefinition: rendered=False, ) + schema_file = self.manifest.files[target.file_id] + assert isinstance(schema_file, SchemaSourceFile) + source_vars = schema_file.get_vars("sources", source.name) + if not isinstance(config, SourceConfig): raise DbtInternalError( f"Calculated a {type(config)} for a source, but expected a SourceConfig" @@ -190,6 +195,7 @@ def parse_source(self, target: UnpatchedSourceDefinition) -> SourceDefinition: tags=tags, config=config, unrendered_config=unrendered_config, + vars=source_vars, ) if ( diff --git a/schemas/dbt/manifest/v12.json b/schemas/dbt/manifest/v12.json index d1246d526ce..3527e4d8136 100644 --- a/schemas/dbt/manifest/v12.json +++ b/schemas/dbt/manifest/v12.json @@ -706,6 +706,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -721,6 +727,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "root_path": { "anyOf": [ { @@ -1739,6 +1751,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -1754,6 +1772,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -2387,6 +2411,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -2402,6 +2432,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -3172,6 +3208,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -3187,6 +3229,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -3976,6 +4024,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -3991,6 +4045,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -5331,6 +5391,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -5346,6 +5412,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -5979,6 +6051,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -5994,6 +6072,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -6931,6 +7015,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -6946,6 +7036,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -8083,6 +8179,12 @@ "type": "string" } }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -8454,6 +8556,12 @@ "type": "string" } }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "url": { "anyOf": [ { @@ -10510,6 +10618,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -10525,6 +10639,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "root_path": { "anyOf": [ { @@ -11543,6 +11663,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -11558,6 +11684,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -12191,6 +12323,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -12206,6 +12344,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -12976,6 +13120,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -12991,6 +13141,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -13780,6 +13936,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -13795,6 +13957,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -15135,6 +15303,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -15150,6 +15324,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -15783,6 +15963,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -15798,6 +15984,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -16735,6 +16927,12 @@ "type": "string" } }, + "unrendered_config_call_dict": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -16750,6 +16948,12 @@ "type": "string", "default": "" }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "language": { "type": "string", "default": "sql" @@ -17878,6 +18082,12 @@ "type": "string" } }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "relation_name": { "anyOf": [ { @@ -18047,6 +18257,12 @@ "type": "string" } }, + "vars": { + "type": "object", + "propertyNames": { + "type": "string" + } + }, "url": { "anyOf": [ { diff --git a/tests/functional/artifacts/expected_manifest.py b/tests/functional/artifacts/expected_manifest.py index 606a3e8f5d7..8e40d6e35e4 100644 --- a/tests/functional/artifacts/expected_manifest.py +++ b/tests/functional/artifacts/expected_manifest.py @@ -363,6 +363,7 @@ def expected_seeded_manifest(project, model_database=None, quote_model=False): "extra_ctes": [], "checksum": checksum_file(model_sql_path), "unrendered_config": unrendered_model_config, + "vars": {}, "access": "protected", "version": None, "latest_version": None, @@ -462,6 +463,7 @@ def expected_seeded_manifest(project, model_database=None, quote_model=False): "extra_ctes": [], "checksum": checksum_file(second_model_sql_path), "unrendered_config": unrendered_second_config, + "vars": {}, "access": "protected", "version": None, "latest_version": None, @@ -544,6 +546,7 @@ def expected_seeded_manifest(project, model_database=None, quote_model=False): "docs": {"node_color": None, "show": True}, "checksum": checksum_file(seed_path), "unrendered_config": unrendered_seed_config, + "vars": {}, "relation_name": relation_name_node_format.format( project.database, my_schema_name, "seed" ), @@ -599,6 +602,7 @@ def expected_seeded_manifest(project, model_database=None, quote_model=False): }, "checksum": {"name": "none", "checksum": ""}, "unrendered_config": unrendered_test_config, + "vars": {}, "contract": {"checksum": None, "enforced": False, "alias_types": True}, }, "snapshot.test.snapshot_seed": { @@ -646,6 +650,7 @@ def expected_seeded_manifest(project, model_database=None, quote_model=False): "tags": [], "unique_id": "snapshot.test.snapshot_seed", "unrendered_config": unrendered_snapshot_config, + "vars": {"alternate_schema": alternate_schema}, }, "test.test.test_nothing_model_.5d38568946": { "alias": "test_nothing_model_", @@ -698,6 +703,7 @@ def expected_seeded_manifest(project, model_database=None, quote_model=False): }, "checksum": {"name": "none", "checksum": ""}, "unrendered_config": unrendered_test_config, + "vars": {}, }, "test.test.unique_model_id.67b76558ff": { "alias": "unique_model_id", @@ -751,6 +757,7 @@ def expected_seeded_manifest(project, model_database=None, quote_model=False): }, "checksum": {"name": "none", "checksum": ""}, "unrendered_config": unrendered_test_config, + "vars": {}, }, }, "sources": { @@ -807,6 +814,7 @@ def expected_seeded_manifest(project, model_database=None, quote_model=False): "unique_id": "source.test.my_source.my_table", "fqn": ["test", "my_source", "my_table"], "unrendered_config": {}, + "vars": {"test_schema": ANY}, }, }, "exposures": { @@ -841,6 +849,7 @@ def expected_seeded_manifest(project, model_database=None, quote_model=False): "unique_id": "exposure.test.notebook_exposure", "url": "http://example.com/notebook/1", "unrendered_config": {}, + "vars": {}, }, "exposure.test.simple_exposure": { "created_at": ANY, @@ -873,6 +882,7 @@ def expected_seeded_manifest(project, model_database=None, quote_model=False): "meta": {}, "tags": [], "unrendered_config": {}, + "vars": {}, }, }, "metrics": {}, @@ -990,6 +1000,7 @@ def expected_references_manifest(project): "extra_ctes": [], "checksum": checksum_file(ephemeral_copy_path), "unrendered_config": get_unrendered_model_config(materialized="ephemeral"), + "vars": {}, "access": "protected", "version": None, "latest_version": None, @@ -1062,6 +1073,7 @@ def expected_references_manifest(project): "unrendered_config": get_unrendered_model_config( materialized="table", group="test_group" ), + "vars": {}, "access": "protected", "version": None, "latest_version": None, @@ -1130,6 +1142,7 @@ def expected_references_manifest(project): "extra_ctes": [], "checksum": checksum_file(view_summary_path), "unrendered_config": get_unrendered_model_config(materialized="view"), + "vars": {}, "access": "protected", "version": None, "latest_version": None, @@ -1213,6 +1226,7 @@ def expected_references_manifest(project): "unique_id": "seed.test.seed", "checksum": checksum_file(seed_path), "unrendered_config": get_unrendered_seed_config(), + "vars": {}, "relation_name": '"{0}"."{1}".seed'.format(project.database, my_schema_name), }, "snapshot.test.snapshot_seed": { @@ -1227,7 +1241,10 @@ def expected_references_manifest(project): "config": get_rendered_snapshot_config(target_schema=alternate_schema), "contract": {"checksum": None, "enforced": False, "alias_types": True}, "database": model_database, - "depends_on": {"macros": [], "nodes": ["seed.test.seed"]}, + "depends_on": { + "macros": [], + "nodes": ["seed.test.seed"], + }, "description": "", "docs": {"node_color": None, "show": True}, "extra_ctes": [], @@ -1255,6 +1272,7 @@ def expected_references_manifest(project): "unrendered_config": get_unrendered_snapshot_config( target_schema=alternate_schema ), + "vars": {"alternate_schema": alternate_schema}, }, }, "sources": { @@ -1309,6 +1327,7 @@ def expected_references_manifest(project): "unique_id": "source.test.my_source.my_table", "fqn": ["test", "my_source", "my_table"], "unrendered_config": {}, + "vars": {"test_schema": ANY}, }, }, "exposures": { @@ -1334,6 +1353,7 @@ def expected_references_manifest(project): "package_name": "test", "path": "schema.yml", "refs": [{"name": "view_summary", "package": None, "version": None}], + "vars": {}, "resource_type": "exposure", "sources": [], "type": "notebook", @@ -1594,6 +1614,7 @@ def expected_versions_manifest(project): group="test_group", meta={"size": "large", "color": "blue"}, ), + "vars": {}, "access": "protected", "version": 1, "latest_version": 2, @@ -1665,6 +1686,7 @@ def expected_versions_manifest(project): "unrendered_config": get_unrendered_model_config( materialized="view", group="test_group", meta={"size": "large", "color": "red"} ), + "vars": {}, "access": "protected", "version": 2, "latest_version": 2, @@ -1723,6 +1745,7 @@ def expected_versions_manifest(project): "extra_ctes": [], "checksum": checksum_file(ref_versioned_model_path), "unrendered_config": get_unrendered_model_config(), + "vars": {}, "access": "protected", "version": None, "latest_version": None, @@ -1780,6 +1803,7 @@ def expected_versions_manifest(project): }, "checksum": {"name": "none", "checksum": ""}, "unrendered_config": unrendered_test_config, + "vars": {}, }, "test.test.unique_versioned_model_v1_count.0b4c0b688a": { "alias": "unique_versioned_model_v1_count", @@ -1833,6 +1857,7 @@ def expected_versions_manifest(project): }, "checksum": {"name": "none", "checksum": ""}, "unrendered_config": unrendered_test_config, + "vars": {}, }, "test.test.unique_versioned_model_v2_first_name.998430d28e": { "alias": "unique_versioned_model_v2_first_name", @@ -1886,6 +1911,7 @@ def expected_versions_manifest(project): }, "checksum": {"name": "none", "checksum": ""}, "unrendered_config": unrendered_test_config, + "vars": {}, }, }, "exposures": { @@ -1917,6 +1943,7 @@ def expected_versions_manifest(project): "unique_id": "exposure.test.notebook_exposure", "url": None, "unrendered_config": {}, + "vars": {}, }, }, "metrics": {}, diff --git a/tests/functional/defer_state/test_modified_state.py b/tests/functional/defer_state/test_modified_state.py index 2ded38e742b..3d0f0955c93 100644 --- a/tests/functional/defer_state/test_modified_state.py +++ b/tests/functional/defer_state/test_modified_state.py @@ -1144,3 +1144,216 @@ def test_changed_semantic_model_contents(self, project): write_file(modified_semantic_model_schema_yml, "models", "schema.yml") results = run_dbt(["list", "-s", "state:modified", "--state", "./state"]) assert len(results) == 1 + + +class TestModifiedVarsLegacy(BaseModifiedState): + @pytest.fixture(scope="class") + def project_config_update(self): + return { + "flags": { + "state_modified_compare_vars": False, + }, + "vars": {"my_var": 1}, + } + + @pytest.fixture(scope="class") + def models(self): + return { + "model_with_var.sql": "select {{ var('my_var') }} as id", + } + + def test_changed_vars(self, project): + self.run_and_save_state() + + # No var change + assert not run_dbt(["list", "-s", "state:modified", "--state", "./state"]) + assert not run_dbt(["list", "-s", "state:modified.vars", "--state", "./state"]) + + # Modify var (my_var: 1 -> 2) + update_config_file({"vars": {"my_var": 2}}, "dbt_project.yml") + + # By default, do not detect vars in state:modified to preserve legacy behaviour + assert run_dbt(["list", "-s", "state:modified", "--state", "./state"]) == [] + + # state:modified.vars is a new selector, opt-in method -> returns results + assert run_dbt(["list", "-s", "state:modified.vars", "--state", "./state"]) == [ + "test.model_with_var" + ] + + # Reset dbt_project.yml + update_config_file({"vars": {"my_var": 1}}, "dbt_project.yml") + + # Modify var via --var CLI flag + assert not run_dbt( + ["list", "--vars", '{"my_var": 1}', "-s", "state:modified", "--state", "./state"] + ) + assert ( + run_dbt( + ["list", "--vars", '{"my_var": 2}', "-s", "state:modified", "--state", "./state"] + ) + == [] + ) + # state:modified.vars is a new selector, opt-in method -> returns results + assert run_dbt( + ["list", "--vars", '{"my_var": 2}', "-s", "state:modified.vars", "--state", "./state"] + ) == ["test.model_with_var"] + + +class TestModifiedVars(BaseModifiedState): + @pytest.fixture(scope="class") + def project_config_update(self): + return { + "flags": { + "state_modified_compare_vars": True, + }, + "vars": {"my_var": 1}, + } + + @pytest.fixture(scope="class") + def models(self): + return { + "model_with_var.sql": "select {{ var('my_var') }} as id", + } + + def test_changed_vars(self, project): + self.run_and_save_state() + + # No var change + assert not run_dbt(["list", "-s", "state:modified", "--state", "./state"]) + assert not run_dbt(["list", "-s", "state:modified.vars", "--state", "./state"]) + + # Modify var (my_var: 1 -> 2) + update_config_file({"vars": {"my_var": 2}}, "dbt_project.yml") + assert run_dbt(["list", "-s", "state:modified", "--state", "./state"]) == [ + "test.model_with_var" + ] + assert run_dbt(["list", "-s", "state:modified.vars", "--state", "./state"]) == [ + "test.model_with_var" + ] + + # Reset dbt_project.yml + update_config_file({"vars": {"my_var": 1}}, "dbt_project.yml") + + # Modify var via --var CLI flag + assert not run_dbt( + ["list", "--vars", '{"my_var": 1}', "-s", "state:modified", "--state", "./state"] + ) + assert run_dbt( + ["list", "--vars", '{"my_var": 2}', "-s", "state:modified", "--state", "./state"] + ) == ["test.model_with_var"] + + +macro_with_var_sql = """ +{% macro macro_with_var() %} + {{ var('my_var') }} +{% endmacro %} +""" + + +class TestModifiedMacroVars(BaseModifiedState): + @pytest.fixture(scope="class") + def project_config_update(self): + return { + "flags": { + "state_modified_compare_vars": True, + }, + "vars": {"my_var": 1}, + } + + @pytest.fixture(scope="class") + def models(self): + return { + "model_with_macro.sql": "select {{ macro_with_var() }} as id", + } + + @pytest.fixture(scope="class") + def macros(self): + return { + "macro_with_var.sql": macro_with_var_sql, + } + + def test_changed_vars(self, project): + self.run_and_save_state() + + # No var change + assert not run_dbt(["list", "-s", "state:modified", "--state", "./state"]) + assert not run_dbt(["list", "-s", "state:modified.vars", "--state", "./state"]) + + # Modify var (my_var: 1 -> 2) + update_config_file({"vars": {"my_var": 2}}, "dbt_project.yml") + assert run_dbt(["list", "-s", "state:modified", "--state", "./state"]) == [ + "test.model_with_macro" + ] + assert run_dbt(["list", "-s", "state:modified.vars", "--state", "./state"]) == [ + "test.model_with_macro" + ] + + # Macros themselves not captured as modified because the var value depends on a node's context + assert not run_dbt(["list", "-s", "state:modified.macros", "--state", "./state"]) + + +# TODO: test versioned models, tests +model_with_var_schema_yml = """ +version: 2 +models: + - name: model_with_var + config: + materialized: "{{ var('my_var') }}" + +exposures: + - name: exposure_name + type: dashboard + owner: + name: "{{ var('my_var') }}" + +sources: + - name: jaffle_shop + database: "{{ var('my_var') }}" + schema: jaffle_shop + tables: + - name: orders + - name: customers +""" + + +class TestModifiedVarsSchemaYml(BaseModifiedState): + @pytest.fixture(scope="class") + def project_config_update(self): + return { + "flags": { + "state_modified_compare_vars": True, + }, + "vars": {"my_var": "table"}, + } + + @pytest.fixture(scope="class") + def models(self): + return {"model_with_var.sql": "select 1 as id", "schema.yml": model_with_var_schema_yml} + + def test_changed_vars(self, project): + self.run_and_save_state() + + # No var change + assert not run_dbt(["list", "-s", "state:modified", "--state", "./state"]) + assert not run_dbt(["list", "-s", "state:modified.vars", "--state", "./state"]) + + # Modify var (my_var: table -> view) + update_config_file({"vars": {"my_var": "view"}}, "dbt_project.yml") + assert sorted(run_dbt(["list", "-s", "state:modified", "--state", "./state"])) == sorted( + [ + "test.model_with_var", + "exposure:test.exposure_name", + "source:test.jaffle_shop.customers", + "source:test.jaffle_shop.orders", + ] + ) + assert sorted( + run_dbt(["list", "-s", "state:modified.vars", "--state", "./state"]) + ) == sorted( + [ + "test.model_with_var", + "exposure:test.exposure_name", + "source:test.jaffle_shop.customers", + "source:test.jaffle_shop.orders", + ] + ) diff --git a/tests/functional/list/test_list.py b/tests/functional/list/test_list.py index 6894ec96bb4..7b98e5c8251 100644 --- a/tests/functional/list/test_list.py +++ b/tests/functional/list/test_list.py @@ -49,7 +49,10 @@ def expect_snapshot_output(self, happy_path_project): # noqa: F811 "json": { "name": "my_snapshot", "package_name": "test", - "depends_on": {"nodes": [], "macros": []}, + "depends_on": { + "nodes": [], + "macros": [], + }, "tags": [], "config": { "enabled": True, diff --git a/tests/unit/contracts/graph/test_manifest.py b/tests/unit/contracts/graph/test_manifest.py index 36f14537b09..eaff7f6c57e 100644 --- a/tests/unit/contracts/graph/test_manifest.py +++ b/tests/unit/contracts/graph/test_manifest.py @@ -97,6 +97,7 @@ "defer_relation", "time_spine", "batches", + "vars", } ) diff --git a/tests/unit/contracts/graph/test_nodes.py b/tests/unit/contracts/graph/test_nodes.py index 4e3a2353105..1271cb7108f 100644 --- a/tests/unit/contracts/graph/test_nodes.py +++ b/tests/unit/contracts/graph/test_nodes.py @@ -1,5 +1,6 @@ import pickle import re +from argparse import Namespace from dataclasses import replace import pytest @@ -24,6 +25,11 @@ ) +@pytest.fixture +def args_for_flags() -> Namespace: + return Namespace(state_modified_compare_vars=False) + + def norm_whitespace(string): _RE_COMBINE_WHITESPACE = re.compile(r"\s+") string = _RE_COMBINE_WHITESPACE.sub(" ", string).strip() @@ -200,6 +206,7 @@ def basic_compiled_dict(): }, "unrendered_config": {}, "unrendered_config_call_dict": {}, + "vars": {}, "config_call_dict": {}, "access": "protected", "constraints": [], @@ -520,6 +527,7 @@ def basic_compiled_schema_test_dict(): "severity": "warn", }, "unrendered_config_call_dict": {}, + "vars": {}, "config_call_dict": {}, } diff --git a/tests/unit/contracts/graph/test_nodes_parsed.py b/tests/unit/contracts/graph/test_nodes_parsed.py index eeec787dd54..b89e3fac552 100644 --- a/tests/unit/contracts/graph/test_nodes_parsed.py +++ b/tests/unit/contracts/graph/test_nodes_parsed.py @@ -65,8 +65,8 @@ @pytest.fixture -def flags_for_args() -> Namespace: - return Namespace(SEND_ANONYMOUS_USAGE_STATS=False) +def args_for_flags() -> Namespace: + return Namespace(send_anonymous_usage_stats=False, state_modified_compare_vars=False) @pytest.fixture @@ -200,6 +200,7 @@ def base_parsed_model_dict(): }, "unrendered_config": {}, "unrendered_config_call_dict": {}, + "vars": {}, "config_call_dict": {}, "access": AccessType.Protected.value, "constraints": [], @@ -256,6 +257,7 @@ def minimal_parsed_model_dict(): "checksum": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", }, "unrendered_config": {}, + "vars": {}, } @@ -323,6 +325,7 @@ def complex_parsed_model_dict(): "post_hook": ['insert into blah(a, b) select "1", 1'], }, "unrendered_config_call_dict": {}, + "vars": {}, "config_call_dict": {}, "access": AccessType.Protected.value, "constraints": [], @@ -533,6 +536,7 @@ def basic_parsed_seed_dict(): "checksum": {"name": "path", "checksum": "seeds/seed.csv"}, "unrendered_config": {}, "unrendered_config_call_dict": {}, + "vars": {}, "config_call_dict": {}, } @@ -639,6 +643,7 @@ def complex_parsed_seed_dict(): "persist_docs": {"relation": True, "columns": True}, }, "unrendered_config_call_dict": {}, + "vars": {}, "config_call_dict": {}, } @@ -839,6 +844,7 @@ def base_parsed_hook_dict(): }, "unrendered_config": {}, "unrendered_config_call_dict": {}, + "vars": {}, "config_call_dict": {}, } @@ -933,6 +939,7 @@ def complex_parsed_hook_dict(): "materialized": "table", }, "unrendered_config_call_dict": {}, + "vars": {}, "config_call_dict": {}, } @@ -1078,6 +1085,7 @@ def basic_parsed_schema_test_dict(): }, "unrendered_config": {}, "unrendered_config_call_dict": {}, + "vars": {}, "config_call_dict": {}, } @@ -1167,6 +1175,7 @@ def complex_parsed_schema_test_dict(): }, "unrendered_config": {"materialized": "table", "severity": "WARN"}, "unrendered_config_call_dict": {}, + "vars": {}, "config_call_dict": {}, } @@ -1562,6 +1571,7 @@ def basic_timestamp_snapshot_dict(): "target_schema": "some_snapshot_schema", }, "unrendered_config_call_dict": {}, + "vars": {}, "config_call_dict": {}, } @@ -1668,6 +1678,7 @@ def basic_check_snapshot_dict(): }, "unrendered_config_call_dict": {}, "config_call_dict": {}, + "vars": {}, } @@ -1877,6 +1888,7 @@ def basic_parsed_source_definition_dict(): "enabled": True, }, "unrendered_config": {}, + "vars": {}, } @@ -1909,6 +1921,7 @@ def complex_parsed_source_definition_dict(): "freshness": {"warn_after": {"period": "hour", "count": 1}, "error_after": {}}, "loaded_at_field": "loaded_at", "unrendered_config": {}, + "vars": {}, } @@ -2080,6 +2093,7 @@ def basic_parsed_exposure_dict(): "enabled": True, }, "unrendered_config": {}, + "vars": {}, } @@ -2135,6 +2149,7 @@ def complex_parsed_exposure_dict(): "enabled": True, }, "unrendered_config": {}, + "vars": {}, } diff --git a/tests/unit/graph/test_selector_methods.py b/tests/unit/graph/test_selector_methods.py index 04aebe052d1..9447062a1ac 100644 --- a/tests/unit/graph/test_selector_methods.py +++ b/tests/unit/graph/test_selector_methods.py @@ -1,4 +1,5 @@ import copy +from argparse import Namespace from dataclasses import replace from pathlib import Path from unittest import mock @@ -643,6 +644,11 @@ def previous_state(manifest): return create_previous_state(manifest) +@pytest.fixture +def args_for_flags(): + return Namespace(state_modified_compare_vars=False) + + def add_node(manifest, node): manifest.nodes[node.unique_id] = node diff --git a/tests/unit/parser/test_parser.py b/tests/unit/parser/test_parser.py index b9b414ebac2..0d95a495cdf 100644 --- a/tests/unit/parser/test_parser.py +++ b/tests/unit/parser/test_parser.py @@ -463,6 +463,7 @@ def test__parse_basic_source(self): @mock.patch("dbt.parser.sources.get_adapter") def test__parse_basic_source_meta(self, mock_get_adapter): block = self.file_block_for(MULTIPLE_TABLE_SOURCE_META, "test_one.yml") + self.parser.manifest.files[block.file.file_id] = block.file dct = yaml_from_file(block.file) self.parser.parse_file(block, dct) self.assert_has_manifest_lengths(self.parser.manifest, sources=2)