From 3861a21c4e37ba8c5b3678ae6e4addcdfe204a15 Mon Sep 17 00:00:00 2001 From: Quigley Malcolm Date: Wed, 11 Oct 2023 15:24:16 -0700 Subject: [PATCH] Update `upgrade_v10_metric_filters` to handled disabled metrics 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. --- core/dbt/contracts/graph/manifest_upgrade.py | 84 +++++++++++--------- 1 file changed, 47 insertions(+), 37 deletions(-) diff --git a/core/dbt/contracts/graph/manifest_upgrade.py b/core/dbt/contracts/graph/manifest_upgrade.py index d05488a0564..e37d9c1c637 100644 --- a/core/dbt/contracts/graph/manifest_upgrade.py +++ b/core/dbt/contracts/graph/manifest_upgrade.py @@ -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': ''}} whereas v11 filters look like: {..., 'filter': {'where_filters': [{'where_sql_template': ''}, ...]}} + """ + 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: