Skip to content

Commit

Permalink
Tweak snapshot config to allow using schema/database/alias macros
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Jul 12, 2024
1 parent 1aeff2c commit 1fb1cca
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
5 changes: 2 additions & 3 deletions core/dbt/artifacts/resources/v1/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 2 additions & 7 deletions core/dbt/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
)
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 10 additions & 4 deletions core/dbt/parser/snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 38 in core/dbt/parser/snapshots.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/snapshots.py#L38

Added line #L38 was not covered by tests

# 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

Expand Down

0 comments on commit 1fb1cca

Please sign in to comment.