|
| 1 | +import json |
| 2 | +from sqlglot import exp |
| 3 | + |
| 4 | +from sqlmesh.core.console import get_console |
| 5 | + |
| 6 | + |
| 7 | +KEYS_TO_MAKE_DETERMINISTIC = ["__sqlmesh__vars__", "__sqlmesh__blueprint__vars__"] |
| 8 | + |
| 9 | + |
| 10 | +def migrate(state_sync, **kwargs): # type: ignore |
| 11 | + engine_adapter = state_sync.engine_adapter |
| 12 | + schema = state_sync.schema |
| 13 | + snapshots_table = "_snapshots" |
| 14 | + versions_table = "_versions" |
| 15 | + if schema: |
| 16 | + snapshots_table = f"{schema}.{snapshots_table}" |
| 17 | + versions_table = f"{schema}.{versions_table}" |
| 18 | + |
| 19 | + result = engine_adapter.fetchone( |
| 20 | + exp.select("schema_version").from_(versions_table), quote_identifiers=True |
| 21 | + ) |
| 22 | + if not result: |
| 23 | + # This must be the first migration, so we can skip the check since the project was not exposed to 85 migration bug |
| 24 | + return |
| 25 | + schema_version = result[0] |
| 26 | + if schema_version < 85: |
| 27 | + # The project was not exposed to the bugged 85 migration, so we can skip it. |
| 28 | + return |
| 29 | + |
| 30 | + warning = ( |
| 31 | + "SQLMesh detected that it may not be able to fully migrate the state database. This should not impact " |
| 32 | + "the migration process, but may result in unexpected changes being reported by the next `sqlmesh plan` " |
| 33 | + "command. Please run `sqlmesh diff prod` after the migration has completed, before making any new " |
| 34 | + "changes. If any unexpected changes are reported, consider running a forward-only plan to apply these " |
| 35 | + "changes and avoid unnecessary backfills: sqlmesh plan prod --forward-only. " |
| 36 | + "See https://sqlmesh.readthedocs.io/en/stable/concepts/plans/#forward-only-plans for more details.\n" |
| 37 | + ) |
| 38 | + |
| 39 | + for ( |
| 40 | + name, |
| 41 | + identifier, |
| 42 | + version, |
| 43 | + snapshot, |
| 44 | + kind_name, |
| 45 | + updated_ts, |
| 46 | + unpaused_ts, |
| 47 | + ttl_ms, |
| 48 | + unrestorable, |
| 49 | + ) in engine_adapter.fetchall( |
| 50 | + exp.select( |
| 51 | + "name", |
| 52 | + "identifier", |
| 53 | + "version", |
| 54 | + "snapshot", |
| 55 | + "kind_name", |
| 56 | + "updated_ts", |
| 57 | + "unpaused_ts", |
| 58 | + "ttl_ms", |
| 59 | + "unrestorable", |
| 60 | + ).from_(snapshots_table), |
| 61 | + quote_identifiers=True, |
| 62 | + ): |
| 63 | + parsed_snapshot = json.loads(snapshot) |
| 64 | + python_env = parsed_snapshot["node"].get("python_env") |
| 65 | + |
| 66 | + if python_env: |
| 67 | + for key, executable in python_env.items(): |
| 68 | + if ( |
| 69 | + key not in KEYS_TO_MAKE_DETERMINISTIC |
| 70 | + and isinstance(executable, dict) |
| 71 | + and executable.get("kind") == "value" |
| 72 | + ): |
| 73 | + get_console().log_warning(warning) |
| 74 | + return |
0 commit comments