|
| 1 | +"""Fix seed paths that have a Windows forward slash in them.""" |
| 2 | +import json |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from sqlglot import exp |
| 6 | + |
| 7 | + |
| 8 | +def migrate(state_sync): # type: ignore |
| 9 | + engine_adapter = state_sync.engine_adapter |
| 10 | + schema = state_sync.schema |
| 11 | + snapshots_table = f"{schema}._snapshots" |
| 12 | + |
| 13 | + new_snapshots = [] |
| 14 | + |
| 15 | + for name, identifier, version, snapshot, kind_name in engine_adapter.fetchall( |
| 16 | + exp.select("name", "identifier", "version", "snapshot", "kind_name").from_(snapshots_table) |
| 17 | + ): |
| 18 | + parsed_snapshot = json.loads(snapshot) |
| 19 | + model_kind = parsed_snapshot["model"]["kind"] |
| 20 | + if "path" in model_kind: |
| 21 | + model_kind["path"] = model_kind["path"].replace("\\", "/") |
| 22 | + |
| 23 | + new_snapshots.append( |
| 24 | + { |
| 25 | + "name": name, |
| 26 | + "identifier": identifier, |
| 27 | + "version": version, |
| 28 | + "snapshot": json.dumps(parsed_snapshot), |
| 29 | + "kind_name": kind_name, |
| 30 | + } |
| 31 | + ) |
| 32 | + |
| 33 | + if new_snapshots: |
| 34 | + engine_adapter.delete_from(snapshots_table, "TRUE") |
| 35 | + |
| 36 | + engine_adapter.insert_append( |
| 37 | + snapshots_table, |
| 38 | + pd.DataFrame(new_snapshots), |
| 39 | + columns_to_types={ |
| 40 | + "name": exp.DataType.build("text"), |
| 41 | + "identifier": exp.DataType.build("text"), |
| 42 | + "version": exp.DataType.build("text"), |
| 43 | + "snapshot": exp.DataType.build("text"), |
| 44 | + "kind_name": exp.DataType.build("text"), |
| 45 | + }, |
| 46 | + contains_json=True, |
| 47 | + ) |
0 commit comments