|
| 1 | +"""Remove superfluous exp.Paren references from partitioned_by""" |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +import pandas as pd |
| 6 | +from sqlglot import exp |
| 7 | + |
| 8 | +from sqlmesh.utils.migration import index_text_type |
| 9 | +from sqlmesh.utils.migration import blob_text_type |
| 10 | + |
| 11 | + |
| 12 | +def migrate(state_sync, **kwargs): # type: ignore |
| 13 | + engine_adapter = state_sync.engine_adapter |
| 14 | + schema = state_sync.schema |
| 15 | + snapshots_table = "_snapshots" |
| 16 | + index_type = index_text_type(engine_adapter.dialect) |
| 17 | + if schema: |
| 18 | + snapshots_table = f"{schema}.{snapshots_table}" |
| 19 | + |
| 20 | + new_snapshots = [] |
| 21 | + updated = False |
| 22 | + |
| 23 | + for ( |
| 24 | + name, |
| 25 | + identifier, |
| 26 | + version, |
| 27 | + snapshot, |
| 28 | + kind_name, |
| 29 | + updated_ts, |
| 30 | + unpaused_ts, |
| 31 | + ttl_ms, |
| 32 | + unrestorable, |
| 33 | + ) in engine_adapter.fetchall( |
| 34 | + exp.select( |
| 35 | + "name", |
| 36 | + "identifier", |
| 37 | + "version", |
| 38 | + "snapshot", |
| 39 | + "kind_name", |
| 40 | + "updated_ts", |
| 41 | + "unpaused_ts", |
| 42 | + "ttl_ms", |
| 43 | + "unrestorable", |
| 44 | + ).from_(snapshots_table), |
| 45 | + quote_identifiers=True, |
| 46 | + ): |
| 47 | + parsed_snapshot = json.loads(snapshot) |
| 48 | + |
| 49 | + if partitioned_by := parsed_snapshot["node"].get("partitioned_by"): |
| 50 | + new_partitioned_by = [] |
| 51 | + for item in partitioned_by: |
| 52 | + # rewrite '(foo)' to 'foo' |
| 53 | + if item.startswith("(") and item.endswith(")"): |
| 54 | + item = item[1:-1] |
| 55 | + updated = True |
| 56 | + new_partitioned_by.append(item) |
| 57 | + parsed_snapshot["node"]["partitioned_by"] = new_partitioned_by |
| 58 | + |
| 59 | + new_snapshots.append( |
| 60 | + { |
| 61 | + "name": name, |
| 62 | + "identifier": identifier, |
| 63 | + "version": version, |
| 64 | + "snapshot": json.dumps(parsed_snapshot), |
| 65 | + "kind_name": kind_name, |
| 66 | + "updated_ts": updated_ts, |
| 67 | + "unpaused_ts": unpaused_ts, |
| 68 | + "ttl_ms": ttl_ms, |
| 69 | + "unrestorable": unrestorable, |
| 70 | + } |
| 71 | + ) |
| 72 | + |
| 73 | + if new_snapshots and updated: |
| 74 | + engine_adapter.delete_from(snapshots_table, "TRUE") |
| 75 | + blob_type = blob_text_type(engine_adapter.dialect) |
| 76 | + |
| 77 | + engine_adapter.insert_append( |
| 78 | + snapshots_table, |
| 79 | + pd.DataFrame(new_snapshots), |
| 80 | + columns_to_types={ |
| 81 | + "name": exp.DataType.build(index_type), |
| 82 | + "identifier": exp.DataType.build(index_type), |
| 83 | + "version": exp.DataType.build(index_type), |
| 84 | + "snapshot": exp.DataType.build(blob_type), |
| 85 | + "kind_name": exp.DataType.build(index_type), |
| 86 | + "updated_ts": exp.DataType.build("bigint"), |
| 87 | + "unpaused_ts": exp.DataType.build("bigint"), |
| 88 | + "ttl_ms": exp.DataType.build("bigint"), |
| 89 | + "unrestorable": exp.DataType.build("boolean"), |
| 90 | + }, |
| 91 | + ) |
0 commit comments