-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqlmesh_tasks.py
More file actions
67 lines (57 loc) · 2.12 KB
/
Copy pathsqlmesh_tasks.py
File metadata and controls
67 lines (57 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import re
import subprocess
import logging
import typing
from utils import init_duckdb
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("openstates")
def extract_audit_error(stdout: str) -> typing.Union[str, None]:
"""
Extract the audit error warning block from stdout.
"""
pattern = r"\[WARNING\].+?audit error:.*?(?=\n\S|\Z)"
match = re.search(pattern, stdout, re.DOTALL)
if match:
return match.group(0).strip()
return None
def sqlmesh_plan(entities: list[str], jurisdiction: str = None) -> list:
"""Run SQLMesh plan on initialized DuckDB data"""
initialize_entities = init_duckdb(entities, jurisdiction)
initialize_entities = [f"staged.{entity}" for entity in initialize_entities]
reports = []
if not initialize_entities:
logger.info(
"No entities were initialized for auditing. Please verify that the data directory exists and contains valid JSON files."
)
return reports
for entity in initialize_entities:
command = [
"poetry",
"run",
"sqlmesh",
"plan",
"--verbose",
"--auto-apply",
"--select-model",
entity,
]
try:
logger.info(f"Running SQLMesh plan for entity: {entity} via subprocess...")
result = subprocess.run(
command, cwd=".", check=True, capture_output=True, text=True
)
report = extract_audit_error(result.stdout)
logger.info(f"SQLMesh plan output:\n{result.stdout}")
if result.stderr:
logger.warning(f"SQLMesh plan warnings/errors:\n{result.stderr}")
except subprocess.CalledProcessError as e:
logger.error(f"SQLMesh plan failed. Exit code: {e.returncode}")
logger.error(f"stdout:\n{e.stdout}")
logger.error(f"stderr:\n{e.stderr}")
raise
if report:
logger.info(f"Entity: {entity} audit failed {report}")
reports.append(report)
else:
logger.info(f"Entity: {entity} audit passed.")
return reports