Skip to content

Commit

Permalink
Update upgrade_v10_metric_filters to handled disabled metrics
Browse files Browse the repository at this point in the history
Regenerating the v10 and v11 test manifest artifacts uncovered an
issue wherein we weren't handling disabled metrics that need to
get upgraded. This commit fixes that. Additionally, the
`upgrade_v10_metric_filters` was getting a bit unwieldy, so I broke
extracted the abstracted sub functions.
  • Loading branch information
QMalcolm committed Oct 11, 2023
1 parent 6d9467c commit 3861a21
Showing 1 changed file with 47 additions and 37 deletions.
84 changes: 47 additions & 37 deletions core/dbt/contracts/graph/manifest_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,54 +62,64 @@ def drop_v9_and_prior_metrics(manifest: dict) -> None:
manifest["disabled"] = filtered_disabled_entries


def upgrade_v10_metric_filters(manifest: dict):
"""Metric filters changed from v10 to v11
def _convert_dct_with_filter(v10_dct_with_opt_filter):
"""Upgrage the filter object from v10 to v11.
v10 filters from a serialized manifest looked like:
{..., 'filter': {'where_sql_template': '<filter_value>'}}
whereas v11 filters look like:
{..., 'filter': {'where_filters': [{'where_sql_template': '<filter_value>'}, ...]}}
"""
if v10_dct_with_opt_filter is not None and v10_dct_with_opt_filter.get("filter") is not None:
v10_dct_with_opt_filter["filter"] = {"where_filters": [v10_dct_with_opt_filter["filter"]]}


Additionally filters can live in multiple places on metrics:
1. metrics.filter
2. metrics.type_params.measure.filter
3. metrics.type_params.input_measures[x].filter
4. metrics.type_params.numerator.filter
5. metrics.type_params.denominator.filter
6. metrics.type_params.metrics[x].filter
def _convert_metric(v10_metric_dict):
"""Upgrades a v10 metric object to a v11 metric object.
Specifcally the following properties change
1. metric.filter
2. metric.type_params.measure.filter
3. metric.type_params.input_measures[x].filter
4. metric.type_params.numerator.filter
5. metric.type_params.denominator.filter
6. metric.type_params.metrics[x].filter"
"""

def _convert_dct_with_filter(v10_dct_with_opt_filter):
if (
v10_dct_with_opt_filter is not None
and v10_dct_with_opt_filter.get("filter") is not None
):
v10_dct_with_opt_filter["filter"] = {
"where_filters": [v10_dct_with_opt_filter["filter"]]
}
# handles top level metric filter
_convert_dct_with_filter(v10_metric_dict)

type_params = v10_metric_dict.get("type_params")
if type_params is not None:
_convert_dct_with_filter(type_params.get("measure"))
_convert_dct_with_filter(type_params.get("numerator"))
_convert_dct_with_filter(type_params.get("denominator"))

# handles metric.type_params.input_measures[x].filter
input_measures = type_params.get("input_measures")
if input_measures is not None:
for input_measure in input_measures:
_convert_dct_with_filter(input_measure)

# handles metric.type_params.metrics[x].filter
metrics = type_params.get("metrics")
if metrics is not None:
for metric in metrics:
_convert_dct_with_filter(metric)


def upgrade_v10_metric_filters(manifest: dict):
"""Handles metric filters changes from v10 to v11."""

metrics = manifest.get("metrics", {})
for metric in metrics.values():
# handles top level metric filter
_convert_dct_with_filter(metric)

type_params = metric.get("type_params")
if type_params is not None:
_convert_dct_with_filter(type_params.get("measure"))
_convert_dct_with_filter(type_params.get("numerator"))
_convert_dct_with_filter(type_params.get("denominator"))

# handles metric.type_params.input_measures[x].filter
input_measures = type_params.get("input_measures")
if input_measures is not None:
for input_measure in input_measures:
_convert_dct_with_filter(input_measure)

# handles metric.type_params.metrics[x].filter
metrics = type_params.get("metrics")
if metrics is not None:
for metric in metrics:
_convert_dct_with_filter(metric)
_convert_metric(metric)

disabled_nodes = manifest.get("disabled", {})
for unique_id, nodes in disabled_nodes.items():
if unique_id.split(".")[0] == "metric":
for node in nodes:
_convert_metric(node)


def upgrade_manifest_json(manifest: dict, manifest_schema_version: int) -> dict:
Expand Down

0 comments on commit 3861a21

Please sign in to comment.