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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 | @snapshot.command("run")
def snapshot_run(
project: ProjectArg = ".",
env_name: EnvOpt = "dev",
engine: EngineOpt = None,
vars: VarsOpt = None,
select: SelectOpt = None,
exclude: ExcludeOpt = None,
jobs: JobsOpt = "1",
keep_going: KeepOpt = False,
prune: bool = typer.Option(
False,
"--prune",
help="Prune historical snapshot rows after a successful run.",
),
keep_last: int = typer.Option(
3,
"--keep-last",
min=1,
help="Number of latest versions per key to keep when pruning.",
),
dry_run: bool = typer.Option(
False,
"--dry-run",
help="Show pruning actions without modifying any data.",
),
) -> None:
"""
Execute only snapshot models (materialized='snapshot').
Selection works like `fft run` but the final set is restricted to snapshot
models. Use --prune/--keep-last/--dry-run for retention.
"""
ctx: CLIContext = _prepare_context(project, env_name, engine, vars)
bind_context(engine=ctx.profile.engine, env=env_name)
engine_ = _SnapshotRunEngine(
ctx=ctx,
pred=None,
env_name=env_name,
cache_mode=CacheMode.OFF, # snapshots always run; no cache skipping
force_rebuild=set(),
)
# Selection identical to run(), but we filter to snapshots afterwards.
select_tokens, _, raw_selected = _select_predicate_and_raw(
engine_, ctx, select, include_snapshots=True
)
wanted_all = _wanted_names(
select_tokens=select_tokens, exclude=exclude, raw_selected=raw_selected
)
# Restrict to snapshot models only
snapshot_names: set[str] = {
name
for name in wanted_all
if (getattr(REGISTRY.nodes[name], "meta", {}) or {}).get("materialized") == "snapshot"
}
if not snapshot_names:
typer.secho(
"Nothing to run (no snapshot models in selection).",
fg="yellow",
)
clear_context()
raise typer.Exit(0)
# Build DAG levels for the full wanted set so dependency validation still runs.
lvls_all = _levels_for_run([], wanted_all)
# Only execute snapshot nodes while preserving their relative order.
lvls = [lvl for lvl in ([n for n in level if n in snapshot_names] for level in lvls_all) if lvl]
result, logq, started_at, finished_at = _run_schedule(engine_, lvls, jobs, keep_going, ctx)
# Evaluate budgets.yml based on collected query stats
budget_error, budgets_summary = _evaluate_budgets(ctx.project, engine_)
_write_artifacts(ctx, result, started_at, finished_at, engine_, budgets_summary)
_attempt_catalog(ctx)
_emit_logs_and_errors(logq, result, engine_)
if result.failed or budget_error:
clear_context()
raise typer.Exit(1)
# Optional retention
if prune:
executor = engine_.shared[0]
_prune_snapshots(executor, snapshot_names, keep_last, dry_run)
engine_.persist_on_success(result)
engine_.print_timings(result)
echo("✓ Snapshot run done")
clear_context()
|