|
| 1 | +""" |
| 2 | +This migration script has two purposes: |
| 3 | +
|
| 4 | +1) Mark all python env macros referenced in audits, signals or on_virtual_update statements |
| 5 | + as metadata, unless they're referenced elsewhere in the model and they're not metadata-only. |
| 6 | +
|
| 7 | +2) Warn if there is both metadata and non-metadata reference in the python environment of a model. |
| 8 | +
|
| 9 | + The metadata status for macros and signals is now transitive, i.e. every dependency of a |
| 10 | + metadata macro or signal is also metadata, unless it is referenced by a non-metadata object. |
| 11 | +
|
| 12 | + This means that global references of metadata objects may now be excluded from the |
| 13 | + data hash calculation because of their new metadata status, which would lead to a |
| 14 | + diff. This script detects the possibility for such a diff and warns users ahead of time. |
| 15 | +""" |
| 16 | + |
| 17 | +import json |
| 18 | + |
| 19 | +from sqlglot import exp |
| 20 | + |
| 21 | +import sqlmesh.core.dialect as d |
| 22 | +from sqlmesh.core.console import get_console |
| 23 | + |
| 24 | + |
| 25 | +def migrate(state_sync, **kwargs): # type: ignore |
| 26 | + engine_adapter = state_sync.engine_adapter |
| 27 | + schema = state_sync.schema |
| 28 | + snapshots_table = "_snapshots" |
| 29 | + if schema: |
| 30 | + snapshots_table = f"{schema}.{snapshots_table}" |
| 31 | + |
| 32 | + common_msg = ( |
| 33 | + "Since the metadata status is now propagated transitively, this means that the next plan " |
| 34 | + "command may detect unexpected changes and prompt about backfilling this model, or others, " |
| 35 | + "for the same reason. If this is a concern, consider running a forward-only plan instead: " |
| 36 | + "https://sqlmesh.readthedocs.io/en/stable/concepts/plans/#forward-only-plans.\n" |
| 37 | + ) |
| 38 | + |
| 39 | + for (snapshot,) in engine_adapter.fetchall( |
| 40 | + exp.select("snapshot").from_(snapshots_table), quote_identifiers=True |
| 41 | + ): |
| 42 | + parsed_snapshot = json.loads(snapshot) |
| 43 | + node = parsed_snapshot["node"] |
| 44 | + |
| 45 | + # Standalone audits don't have a data hash, so they're unaffected |
| 46 | + if node.get("source_type") == "audit": |
| 47 | + continue |
| 48 | + |
| 49 | + name = node["name"] |
| 50 | + python_env = node.get("python_env") or {} |
| 51 | + |
| 52 | + has_metadata = False |
| 53 | + has_non_metadata = False |
| 54 | + |
| 55 | + for k, v in python_env.items(): |
| 56 | + if v.get("is_metadata"): |
| 57 | + has_metadata = True |
| 58 | + else: |
| 59 | + has_non_metadata = True |
| 60 | + |
| 61 | + if has_metadata and has_non_metadata: |
| 62 | + get_console().log_warning( |
| 63 | + f"Model '{name}' references both metadata and non-metadata functions (macros or signals). " |
| 64 | + + common_msg |
| 65 | + ) |
| 66 | + return |
| 67 | + |
| 68 | + dialect = node.get("dialect") |
| 69 | + metadata_hash_statements = [] |
| 70 | + |
| 71 | + if on_virtual_update := node.get("on_virtual_update"): |
| 72 | + metadata_hash_statements.extend(parse_expression(on_virtual_update, dialect)) |
| 73 | + |
| 74 | + for _, audit_args in func_call_validator(node.get("audits") or []): |
| 75 | + metadata_hash_statements.extend(audit_args.values()) |
| 76 | + |
| 77 | + for signal_name, signal_args in func_call_validator( |
| 78 | + node.get("signals") or [], is_signal=True |
| 79 | + ): |
| 80 | + metadata_hash_statements.extend(signal_args.values()) |
| 81 | + |
| 82 | + if audit_definitions := node.get("audit_definitions"): |
| 83 | + audit_queries = [ |
| 84 | + parse_expression(audit["query"], audit["dialect"]) |
| 85 | + for audit in audit_definitions.values() |
| 86 | + ] |
| 87 | + metadata_hash_statements.extend(audit_queries) |
| 88 | + |
| 89 | + for macro_name in extract_used_macros(metadata_hash_statements): |
| 90 | + serialized_macro = python_env.get(macro_name) |
| 91 | + if isinstance(serialized_macro, dict) and not serialized_macro.get("is_metadata"): |
| 92 | + get_console().log_warning( |
| 93 | + f"Model '{name}' references macro '{macro_name}' which is now implicitly treated as metadata-only. " |
| 94 | + + common_msg |
| 95 | + ) |
| 96 | + return |
| 97 | + |
| 98 | + |
| 99 | +def extract_used_macros(expressions): |
| 100 | + used_macros = set() |
| 101 | + for expression in expressions: |
| 102 | + if isinstance(expression, d.Jinja): |
| 103 | + continue |
| 104 | + |
| 105 | + for macro_func in expression.find_all(d.MacroFunc): |
| 106 | + if macro_func.__class__ is d.MacroFunc: |
| 107 | + used_macros.add(macro_func.this.name.lower()) |
| 108 | + |
| 109 | + return used_macros |
| 110 | + |
| 111 | + |
| 112 | +def func_call_validator(v, is_signal=False): |
| 113 | + assert isinstance(v, list) |
| 114 | + |
| 115 | + audits = [] |
| 116 | + for entry in v: |
| 117 | + if isinstance(entry, dict): |
| 118 | + args = entry |
| 119 | + name = "" if is_signal else entry.pop("name") |
| 120 | + else: |
| 121 | + assert isinstance(entry, (tuple, list)) |
| 122 | + name, args = entry |
| 123 | + |
| 124 | + parsed_audit = { |
| 125 | + key: d.parse_one(value) if isinstance(value, str) else value |
| 126 | + for key, value in args.items() |
| 127 | + } |
| 128 | + audits.append((name.lower(), parsed_audit)) |
| 129 | + |
| 130 | + return audits |
| 131 | + |
| 132 | + |
| 133 | +def parse_expression(v, dialect): |
| 134 | + if v is None: |
| 135 | + return None |
| 136 | + |
| 137 | + if isinstance(v, list): |
| 138 | + return [d.parse_one(e, dialect=dialect) for e in v] |
| 139 | + |
| 140 | + assert isinstance(v, str) |
| 141 | + return d.parse_one(v, dialect=dialect) |
0 commit comments