|
| 1 | +"""Time-travel retention decrease safety enforcement for build commands.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TextIO |
| 6 | + |
| 7 | +from sqlbuild.cli.commands.exceptions import CliUserError |
| 8 | +from sqlbuild.compiler.planner.models import PlanOutput, RetentionPlanEntry |
| 9 | +from sqlbuild.spec.contracts.types import RetentionDecreasePolicy |
| 10 | + |
| 11 | + |
| 12 | +def enforce_retention_decrease_policy( |
| 13 | + *, |
| 14 | + plan: PlanOutput, |
| 15 | + allow_retention_decrease: bool, |
| 16 | + input_stream: TextIO, |
| 17 | + output_stream: TextIO, |
| 18 | +) -> None: |
| 19 | + """Fail or confirm before lowering live time-travel retention.""" |
| 20 | + |
| 21 | + entries: tuple[RetentionPlanEntry, ...] = tuple( |
| 22 | + entry for entry in plan.retention_entries if entry.decreases |
| 23 | + ) |
| 24 | + denied: tuple[RetentionPlanEntry, ...] = tuple( |
| 25 | + entry for entry in entries if entry.decrease_policy == RetentionDecreasePolicy.DENY |
| 26 | + ) |
| 27 | + if denied: |
| 28 | + raise CliUserError( |
| 29 | + f"time travel retention decrease is denied for {_names(denied)}", |
| 30 | + help=( |
| 31 | + "Keep the current retention, or set the target's " |
| 32 | + "time_travel_retention_decrease to 'require_confirmation' or 'allow'." |
| 33 | + ), |
| 34 | + ) |
| 35 | + confirmation: tuple[RetentionPlanEntry, ...] = tuple( |
| 36 | + entry |
| 37 | + for entry in entries |
| 38 | + if entry.decrease_policy == RetentionDecreasePolicy.REQUIRE_CONFIRMATION |
| 39 | + ) |
| 40 | + if not confirmation or allow_retention_decrease: |
| 41 | + return |
| 42 | + if not input_stream.isatty(): |
| 43 | + raise CliUserError( |
| 44 | + "time travel retention decrease requires confirmation", |
| 45 | + help="Pass --allow-retention-decrease to confirm in non-interactive runs.", |
| 46 | + ) |
| 47 | + expected: str = _confirmation_text(confirmation) |
| 48 | + output_stream.write( |
| 49 | + f"Decreasing time travel retention for {_names(confirmation)} permanently discards " |
| 50 | + "history older than the new retention.\n\n" |
| 51 | + ) |
| 52 | + output_stream.write(f"Type `{expected}` to continue: ") |
| 53 | + output_stream.flush() |
| 54 | + if input_stream.readline().strip() != expected: |
| 55 | + raise CliUserError("time travel retention decrease cancelled") |
| 56 | + |
| 57 | + |
| 58 | +def _model_names(entries: tuple[RetentionPlanEntry, ...]) -> tuple[str, ...]: |
| 59 | + names: dict[str, None] = {} |
| 60 | + for entry in entries: |
| 61 | + names.update(dict.fromkeys(entry.model_names)) |
| 62 | + return tuple(names) |
| 63 | + |
| 64 | + |
| 65 | +def _confirmation_text(entries: tuple[RetentionPlanEntry, ...]) -> str: |
| 66 | + names: tuple[str, ...] = _model_names(entries) |
| 67 | + if len(names) == 1: |
| 68 | + return f"decrease retention for {names[0]}" |
| 69 | + return f"decrease retention for {len(names)} models" |
| 70 | + |
| 71 | + |
| 72 | +def _names(entries: tuple[RetentionPlanEntry, ...]) -> str: |
| 73 | + return ", ".join(f"'{name}'" for name in _model_names(entries)) |
0 commit comments