-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_runner.py
More file actions
153 lines (124 loc) · 5.16 KB
/
Copy pathtransform_runner.py
File metadata and controls
153 lines (124 loc) · 5.16 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""Pipeline runner for SQL transformations defined by ``transform.toml``.
A pipeline directory contains one ``transform.toml`` declaring one or more
output tables/views. Each ``[[table]]`` entry references its own ``.sql``
file in the same directory.
TOML schema
-----------
::
[[table]]
name = "ipca"
schema = "analytics"
strategy = "replace" # "replace" or "view"
sql = "ipca.sql"
description = "IPCA - série detalhada"
[[table]]
name = "ipca_resumo"
schema = "analytics"
strategy = "view"
sql = "ipca_resumo.sql"
Required fields per entry: ``name``, ``schema``, ``strategy``, ``sql``.
Optional: ``description``, ``primary_key``, ``indexes``.
Strategies
~~~~~~~~~~
``replace``
``DROP TABLE IF EXISTS`` followed by ``CREATE TABLE ... AS``.
Full refresh — best for batch imports into Power BI, Excel, etc.
``view``
``CREATE OR REPLACE VIEW``. Zero storage cost, always up-to-date,
best for live database connections.
Each entry runs in its own transaction; if one fails, previously
materialized outputs from the same pipeline persist.
The SQL queries use unqualified table names (``dados``, ``dimensao``,
``localidade``, ``tabela_sidra``). They resolve via the database
``search_path`` set by ``get_engine`` from ``config.ini``.
"""
import logging
import tomllib
from pathlib import Path
from rich.console import Console
from rich.progress import (
Progress,
SpinnerColumn,
TextColumn,
TimeElapsedColumn,
)
from . import database
from .config import Config
logger = logging.getLogger(__name__)
class TransformRunner:
"""Run SQL transformations declared in a ``transform.toml`` file."""
def __init__(self, config: Config, toml_path: Path, console: Console | None = None):
self.config = config
self.toml_path = toml_path
self.console = console
def run(self):
with open(self.toml_path, "rb") as f:
data = tomllib.load(f)
tables = data.get("table")
if not isinstance(tables, list) or not tables:
raise ValueError(
f"{self.toml_path}: esperado um ou mais [[table]] (array). "
"O schema [table] singular foi removido — migre para [[table]] "
"com campo 'sql' explícito por entrada."
)
engine = database.get_engine(self.config)
with Progress(
SpinnerColumn(finished_text="[green]✓[/green]"),
TextColumn("[progress.description]{task.description}"),
TimeElapsedColumn(),
console=self.console,
transient=False,
disable=self.console is None,
) as progress:
for entry in tables:
self._materialize(engine, entry, progress)
def _materialize(self, engine, entry: dict, progress: Progress) -> None:
missing = [f for f in ("name", "schema", "strategy", "sql") if f not in entry]
if missing:
raise ValueError(
f"{self.toml_path}: [[table]] sem campo(s) obrigatório(s): "
f"{', '.join(missing)}"
)
name = entry["name"]
schema = entry["schema"]
strategy = entry["strategy"]
sql_rel = entry["sql"]
primary_key = entry.get("primary_key")
indexes = entry.get("indexes", [])
sql_path = self.toml_path.parent / sql_rel
if not sql_path.exists():
raise FileNotFoundError(
f"{self.toml_path}: arquivo SQL '{sql_rel}' não encontrado em "
f"{self.toml_path.parent}"
)
query = sql_path.read_text(encoding="utf-8").strip().replace("%", "%%")
qualified = f'"{schema}"."{name}"'
strategy_label = {"replace": "tabela", "view": "view"}.get(strategy, strategy)
task = progress.add_task(
f"{qualified} [dim][{strategy_label}][/dim]", total=None
)
with engine.begin() as conn:
conn.exec_driver_sql(f'CREATE SCHEMA IF NOT EXISTS "{schema}"')
if strategy == "view":
conn.exec_driver_sql(f"CREATE OR REPLACE VIEW {qualified} AS\n{query}")
elif strategy == "replace":
conn.exec_driver_sql(f"DROP TABLE IF EXISTS {qualified}")
conn.exec_driver_sql(f"CREATE TABLE {qualified} AS\n{query}")
if primary_key:
pk_cols = ", ".join(f'"{c}"' for c in primary_key)
conn.exec_driver_sql(
f"ALTER TABLE {qualified} ADD PRIMARY KEY ({pk_cols})"
)
for idx in indexes:
idx_name = idx["name"]
idx_cols = ", ".join(f'"{c}"' for c in idx["columns"])
unique = "UNIQUE" if idx.get("unique") else ""
conn.exec_driver_sql(
f'CREATE {unique} INDEX "{idx_name}" '
f"ON {qualified} ({idx_cols})"
)
else:
raise ValueError(
f"Unknown strategy {strategy!r} for {qualified} in {self.toml_path}"
)
progress.update(task, total=1, completed=1)