|
| 1 | +import json |
| 2 | +import typing as t |
| 3 | +from sqlglot import exp |
| 4 | + |
| 5 | +from sqlmesh.core.console import get_console |
| 6 | + |
| 7 | + |
| 8 | +KEYS_TO_MAKE_DETERMINISTIC = ["__sqlmesh__vars__", "__sqlmesh__blueprint__vars__"] |
| 9 | + |
| 10 | + |
| 11 | +def would_sorting_be_applied(obj: t.Any) -> bool: |
| 12 | + """ |
| 13 | + Detects if sorting would be applied to an object based on the |
| 14 | + deterministic_repr logic. |
| 15 | + |
| 16 | + Returns True if the object is a dictionary or contains a dictionary |
| 17 | + at any nesting level (in lists or tuples). |
| 18 | + |
| 19 | + Args: |
| 20 | + obj: The object to check |
| 21 | + |
| 22 | + Returns: |
| 23 | + bool: True if sorting would be applied, False otherwise |
| 24 | + """ |
| 25 | + |
| 26 | + def _check_for_dict(o: t.Any) -> bool: |
| 27 | + if isinstance(o, dict): |
| 28 | + return True |
| 29 | + if isinstance(o, (list, tuple)): |
| 30 | + return any(_check_for_dict(item) for item in o) |
| 31 | + |
| 32 | + return False |
| 33 | + |
| 34 | + try: |
| 35 | + return _check_for_dict(obj) |
| 36 | + except Exception: |
| 37 | + # If any error occurs during checking, assume no sorting |
| 38 | + return False |
| 39 | + |
| 40 | + |
| 41 | +def migrate(state_sync, **kwargs): # type: ignore |
| 42 | + engine_adapter = state_sync.engine_adapter |
| 43 | + schema = state_sync.schema |
| 44 | + snapshots_table = "_snapshots" |
| 45 | + versions_table = "_versions" |
| 46 | + if schema: |
| 47 | + snapshots_table = f"{schema}.{snapshots_table}" |
| 48 | + versions_table = f"{schema}.{versions_table}" |
| 49 | + |
| 50 | + result = engine_adapter.fetchone( |
| 51 | + exp.select("schema_version").from_(versions_table), quote_identifiers=True |
| 52 | + ) |
| 53 | + if not result: |
| 54 | + # This must be the first migration, so we can skip the check since the project was not exposed to 85 migration bug |
| 55 | + return |
| 56 | + schema_version = result[0] |
| 57 | + if schema_version < 85: |
| 58 | + # The project was not exposed to the bugged 85 migration, so we can skip it. |
| 59 | + return |
| 60 | + |
| 61 | + warning = ( |
| 62 | + "SQLMesh detected that it may not be able to fully migrate the state database. This should not impact " |
| 63 | + "the migration process, but may result in unexpected changes being reported by the next `sqlmesh plan` " |
| 64 | + "command. Please run `sqlmesh diff prod` after the migration has completed, before making any new " |
| 65 | + "changes. If any unexpected changes are reported, consider running a forward-only plan to apply these " |
| 66 | + "changes and avoid unnecessary backfills: sqlmesh plan prod --forward-only. " |
| 67 | + "See https://sqlmesh.readthedocs.io/en/stable/concepts/plans/#forward-only-plans for more details.\n" |
| 68 | + ) |
| 69 | + |
| 70 | + for ( |
| 71 | + name, |
| 72 | + identifier, |
| 73 | + version, |
| 74 | + snapshot, |
| 75 | + kind_name, |
| 76 | + updated_ts, |
| 77 | + unpaused_ts, |
| 78 | + ttl_ms, |
| 79 | + unrestorable, |
| 80 | + ) in engine_adapter.fetchall( |
| 81 | + exp.select( |
| 82 | + "name", |
| 83 | + "identifier", |
| 84 | + "version", |
| 85 | + "snapshot", |
| 86 | + "kind_name", |
| 87 | + "updated_ts", |
| 88 | + "unpaused_ts", |
| 89 | + "ttl_ms", |
| 90 | + "unrestorable", |
| 91 | + ).from_(snapshots_table), |
| 92 | + quote_identifiers=True, |
| 93 | + ): |
| 94 | + parsed_snapshot = json.loads(snapshot) |
| 95 | + python_env = parsed_snapshot["node"].get("python_env") |
| 96 | + |
| 97 | + if python_env: |
| 98 | + for key, executable in python_env.items(): |
| 99 | + if ( |
| 100 | + key not in KEYS_TO_MAKE_DETERMINISTIC |
| 101 | + and isinstance(executable, dict) |
| 102 | + and executable.get("kind") == "value" |
| 103 | + ): |
| 104 | + try: |
| 105 | + parsed_value = eval(executable["payload"]) |
| 106 | + if would_sorting_be_applied(parsed_value): |
| 107 | + get_console().log_warning(warning) |
| 108 | + return |
| 109 | + except Exception: |
| 110 | + pass |
0 commit comments