-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Back/fwd compatibility for renamed metrics
attributes
#5825
Changes from 6 commits
af4d10a
6784672
9f46a61
9210aad
e728cb1
42b031a
6ed9a4d
855654a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
kind: Under the Hood | ||
body: Compatibiltiy for metric attribute renaming | ||
time: 2022-09-13T11:17:44.953536+02:00 | ||
custom: | ||
Author: jtcohen6 callum-mcdata | ||
Issue: "5807" | ||
PR: "5825" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
from typing import List, Tuple, ClassVar, Type, TypeVar, Dict, Any, Optional | ||
|
||
from dbt.clients.system import write_json, read_json | ||
from dbt import deprecations | ||
from dbt.exceptions import ( | ||
InternalException, | ||
RuntimeException, | ||
|
@@ -207,13 +208,54 @@ def get_manifest_schema_version(dct: dict) -> int: | |
return int(schema_version.split(".")[-2][-1]) | ||
|
||
|
||
# we renamed these properties in v1.3 | ||
# this method allows us to be nice to the early adopters | ||
def rename_metric_attr(data: dict, raise_deprecation_warning: bool = False) -> dict: | ||
metric_name = data["name"] | ||
if raise_deprecation_warning and ( | ||
"sql" in data.keys() | ||
or "type" in data.keys() | ||
or data.get("calculation_method") == "expression" | ||
): | ||
deprecations.warn("metric-attr-renamed", metric_name=metric_name) | ||
duplicated_attribute_msg = """\n | ||
The metric '{}' contains both the deprecated metric property '{}' | ||
and the up-to-date metric property '{}'. Please remove the deprecated property. | ||
""" | ||
if "sql" in data.keys(): | ||
if "expression" in data.keys(): | ||
raise ValidationError( | ||
duplicated_attribute_msg.format(metric_name, "sql", "expression") | ||
) | ||
else: | ||
data["expression"] = data.pop("sql") | ||
if "type" in data.keys(): | ||
callum-mcdata marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if "calculation_method" in data.keys(): | ||
raise ValidationError( | ||
duplicated_attribute_msg.format(metric_name, "type", "calculation_method") | ||
) | ||
else: | ||
calculation_method = data.pop("type") | ||
data["calculation_method"] = calculation_method | ||
# we also changed "type: expression" -> "calculation_method: derived" | ||
if data.get("calculation_method") == "expression": | ||
data["calculation_method"] = "derived" | ||
return data | ||
|
||
|
||
def upgrade_manifest_json(manifest: dict) -> dict: | ||
for node_content in manifest.get("nodes", {}).values(): | ||
if "raw_sql" in node_content: | ||
node_content["raw_code"] = node_content.pop("raw_sql") | ||
if "compiled_sql" in node_content: | ||
node_content["compiled_code"] = node_content.pop("compiled_sql") | ||
node_content["language"] = "sql" | ||
for metric_content in manifest.get("metrics", {}).values(): | ||
# handle attr renames + value translation ("expression" -> "derived") | ||
metric_content = rename_metric_attr(metric_content) | ||
# mashumaro.exceptions.MissingField: Field "window" of type Optional[MetricTime] is missing in ParsedMetric instance | ||
if "window" not in metric_content: | ||
metric_content["window"] = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of this you should change the While looking at that bit of code, I noticed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @emmyoop you were totally right! I was able to remove this component by setting the default value of window to be None in ParsedMetric. I also fixed the timestamp field to reflect UnparsedMetric like you suggested - must have missed that on my last PR! |
||
return manifest | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,18 @@ class AdapterDeprecationWarning(DBTDeprecation): | |
deprecations[dep.name] = dep | ||
|
||
|
||
class MetricAttributesRenamed(DBTDeprecation): | ||
_name = "metric-attr-renamed" | ||
_description = """\ | ||
dbt-core v1.3 renamed attributes for metrics: | ||
\n 'sql' -> 'expression' | ||
\n 'type' -> 'calculation_method' | ||
\n 'type: expression' -> 'calculation_method: derived' | ||
\nThe old metric parameter names will be fully deprecated in v1.4. | ||
\nPlease remove them from the metric definition of metric '{metric_name}' | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a comment here with a link to the issue that will do this so it doesn't get lost? Love that we're committing to a timeframe here! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue created here: #5849 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @emmyoop the link to this GH issue has been added to the deprecation warning! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @callum-mcdata I actually meant a comment in the code, not as part of the deprecation message. Sorry if that was unclear! Unless you think users will find the link to the issue useful? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤦 woops! Although now that I think about it, if we include the link to the issue then anyone who feels very strongly that this behavior should be retained could go to it easily and let us know there. That way the information isn't spread over multiple channels..... so actually I do think it would be useful! |
||
|
||
|
||
def warn(name, *args, **kwargs): | ||
if name not in deprecations: | ||
# this should (hopefully) never happen | ||
|
@@ -105,6 +117,7 @@ def warn(name, *args, **kwargs): | |
ConfigDataPathDeprecation(), | ||
PackageInstallPathDeprecation(), | ||
PackageRedirectDeprecation(), | ||
MetricAttributesRenamed(), | ||
] | ||
|
||
deprecations: Dict[str, DBTDeprecation] = {d.name: d for d in deprecations_list} | ||
|
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my own understanding, is there any reason (apart from data class vs function) for the different naming conventions here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's it!