diff --git a/core/dbt/artifacts/resources/v1/snapshot.py b/core/dbt/artifacts/resources/v1/snapshot.py index dee235a19df..c9f1acdb50f 100644 --- a/core/dbt/artifacts/resources/v1/snapshot.py +++ b/core/dbt/artifacts/resources/v1/snapshot.py @@ -19,10 +19,9 @@ class SnapshotConfig(NodeConfig): check_cols: Union[str, List[str], None] = None def final_validate(self): - if not self.strategy or not self.unique_key or not self.target_schema: + if not self.strategy or not self.unique_key: raise ValidationError( - "Snapshots must be configured with a 'strategy', 'unique_key', " - "and 'target_schema'." + "Snapshots must be configured with a 'strategy' and 'unique_key'." ) if self.strategy == "check": if not self.check_cols: diff --git a/core/dbt/parser/base.py b/core/dbt/parser/base.py index 05d5b7d114d..f0d39bc11b5 100644 --- a/core/dbt/parser/base.py +++ b/core/dbt/parser/base.py @@ -72,6 +72,7 @@ def __init__( class RelationUpdate: + # "component" is database, schema or alias def __init__(self, config: RuntimeConfig, manifest: Manifest, component: str) -> None: default_macro = manifest.find_generate_macro_by_name( component=component, @@ -127,6 +128,7 @@ def __init__( ) -> None: super().__init__(project, manifest, root_project) + # this sets callables from RelationUpdate self._update_node_database = RelationUpdate( manifest=manifest, config=root_project, component="database" ) @@ -288,13 +290,6 @@ def update_parsed_node_relation_names( self._update_node_schema(parsed_node, config_dict.get("schema")) self._update_node_alias(parsed_node, config_dict.get("alias")) - # Snapshot nodes use special "target_database" and "target_schema" fields for some reason - if getattr(parsed_node, "resource_type", None) == NodeType.Snapshot: - if "target_database" in config_dict and config_dict["target_database"]: - parsed_node.database = config_dict["target_database"] - if "target_schema" in config_dict and config_dict["target_schema"]: - parsed_node.schema = config_dict["target_schema"] - self._update_node_relation_name(parsed_node) def update_parsed_node_config( diff --git a/core/dbt/parser/snapshots.py b/core/dbt/parser/snapshots.py index b4d1c7e5349..eb8a80fca2d 100644 --- a/core/dbt/parser/snapshots.py +++ b/core/dbt/parser/snapshots.py @@ -29,16 +29,22 @@ def set_snapshot_attributes(self, node): # `database` value of the node (ultimately sourced from the `database` # config value), and if that is not set, use the database defined in # the adapter's credentials. + changed = False if node.config.target_database: node.database = node.config.target_database - elif not node.database: + changed = True + elif not node.database: # does this ever happen? node.database = self.root_project.credentials.database + changed = True # the target schema must be set if we got here, so overwrite the node's # schema - node.schema = node.config.target_schema - # We need to set relation_name again, since database/schema might have changed - self._update_node_relation_name(node) + if node.config.target_schema: + node.schema = node.config.target_schema + changed = True + if changed: + # We need to set relation_name again, since database/schema might have changed + self._update_node_relation_name(node) return node