|
19 | 19 | from sqlbuild.cli.commands._helpers.build_planning.table_type import ( |
20 | 20 | enforce_table_type_downgrade_policy, |
21 | 21 | ) |
22 | | -from sqlbuild.cli.commands.constants import MISSING_ORIGIN_BUILD_HELP |
| 22 | +from sqlbuild.cli.commands.constants import ( |
| 23 | + EXECUTION_LIMIT_BUILD_NOTE, |
| 24 | + EXECUTION_LIMIT_DBT_NOTE, |
| 25 | + MISSING_ORIGIN_BUILD_HELP, |
| 26 | + MISSING_ORIGIN_DBT_HELP, |
| 27 | + RETENTION_DECREASE_BUILD_HELP, |
| 28 | + RETENTION_DECREASE_DBT_HELP, |
| 29 | + SNAPSHOT_FULL_REFRESH_BUILD_HELP, |
| 30 | + SNAPSHOT_FULL_REFRESH_DBT_HELP, |
| 31 | + TABLE_TYPE_DOWNGRADE_BUILD_HELP, |
| 32 | + TABLE_TYPE_DOWNGRADE_DBT_HELP, |
| 33 | +) |
23 | 34 | from sqlbuild.cli.commands.exceptions import CliUserError |
24 | | -from sqlbuild.cli.commands.models import BuildCommandRequest, BuildInvocation |
| 35 | +from sqlbuild.cli.commands.models import BuildCommandRequest, BuildInvocation, PlanSafetyGates |
25 | 36 | from sqlbuild.compiler.migrations.types import ColumnMigrationDecision, MigrationDecision |
26 | 37 | from sqlbuild.compiler.planner.constants import HIDDEN_ORIGIN_REMEDY |
27 | 38 | from sqlbuild.compiler.planner.models import ( |
|
31 | 42 | PlanWarning, |
32 | 43 | ) |
33 | 44 | from sqlbuild.compiler.planner.types import WarningSeverity |
| 45 | +from sqlbuild.spec.contracts.models import ExecutionLimitsConfig, SnapshotsConfig |
34 | 46 | from sqlbuild.spec.contracts.types import MissingMigrationOriginPolicy |
35 | 47 |
|
36 | 48 | _OLD_NAME_CODES: frozenset[str] = frozenset({"M114", "P008"}) |
|
44 | 56 |
|
45 | 57 | def enforce_build_plan_policies( |
46 | 58 | *, request: BuildCommandRequest, invocation: BuildInvocation, plan: PlanOutput |
| 59 | +) -> None: |
| 60 | + """Apply every plan safety gate before a direct build executes anything.""" |
| 61 | + |
| 62 | + enforce_plan_safety_policies( |
| 63 | + plan=plan, |
| 64 | + gates=PlanSafetyGates( |
| 65 | + allow_missing_migration_origin=request.allow_missing_migration_origin, |
| 66 | + allow_snapshot_full_refresh=request.allow_snapshot_full_refresh, |
| 67 | + allow_table_type_downgrade=request.allow_table_type_downgrade, |
| 68 | + allow_retention_decrease=request.allow_retention_decrease, |
| 69 | + missing_origin_help=MISSING_ORIGIN_BUILD_HELP, |
| 70 | + snapshot_full_refresh_help=SNAPSHOT_FULL_REFRESH_BUILD_HELP, |
| 71 | + table_type_downgrade_help=TABLE_TYPE_DOWNGRADE_BUILD_HELP, |
| 72 | + retention_decrease_help=RETENTION_DECREASE_BUILD_HELP, |
| 73 | + execution_limit_note=EXECUTION_LIMIT_BUILD_NOTE, |
| 74 | + ), |
| 75 | + snapshots_config=invocation.discovered_inputs.project_config.snapshots, |
| 76 | + target_name=invocation.effective_target_name, |
| 77 | + execution_limits=invocation.execution_limits, |
| 78 | + streams=(sys.stdin, sys.stdout), |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def dbt_interop_plan_safety_gates() -> PlanSafetyGates: |
| 83 | + """Gates for sqb dbt, which has no allow flags and confirms only on a terminal.""" |
| 84 | + |
| 85 | + return PlanSafetyGates( |
| 86 | + allow_missing_migration_origin=False, |
| 87 | + allow_snapshot_full_refresh=False, |
| 88 | + allow_table_type_downgrade=False, |
| 89 | + allow_retention_decrease=False, |
| 90 | + missing_origin_help=MISSING_ORIGIN_DBT_HELP, |
| 91 | + snapshot_full_refresh_help=SNAPSHOT_FULL_REFRESH_DBT_HELP, |
| 92 | + table_type_downgrade_help=TABLE_TYPE_DOWNGRADE_DBT_HELP, |
| 93 | + retention_decrease_help=RETENTION_DECREASE_DBT_HELP, |
| 94 | + execution_limit_note=EXECUTION_LIMIT_DBT_NOTE, |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +def enforce_plan_safety_policies( |
| 99 | + *, |
| 100 | + plan: PlanOutput, |
| 101 | + gates: PlanSafetyGates, |
| 102 | + snapshots_config: SnapshotsConfig, |
| 103 | + target_name: str | None, |
| 104 | + execution_limits: ExecutionLimitsConfig, |
| 105 | + streams: tuple[TextIO, TextIO], |
47 | 106 | ) -> None: |
48 | 107 | """Apply migration, execution-limit, and storage safety gates in their required order.""" |
49 | 108 |
|
| 109 | + input_stream, output_stream = streams |
50 | 110 | enforce_migration_plan_policies( |
51 | 111 | plan=plan, |
52 | | - allow_missing_migration_origin=request.allow_missing_migration_origin, |
53 | | - non_interactive_help=MISSING_ORIGIN_BUILD_HELP, |
54 | | - input_stream=sys.stdin, |
55 | | - output_stream=sys.stdout, |
| 112 | + allow_missing_migration_origin=gates.allow_missing_migration_origin, |
| 113 | + non_interactive_help=gates.missing_origin_help, |
| 114 | + input_stream=input_stream, |
| 115 | + output_stream=output_stream, |
56 | 116 | ) |
57 | 117 | _enforce_old_name_policy(plan=plan) |
58 | 118 | enforce_model_execution_limit( |
59 | 119 | model_count=executable_model_count(plan=plan), |
60 | | - target_name=invocation.effective_target_name, |
61 | | - limits=invocation.execution_limits, |
| 120 | + target_name=target_name, |
| 121 | + limits=execution_limits, |
| 122 | + refusal_note=gates.execution_limit_note, |
62 | 123 | ) |
63 | 124 | enforce_snapshot_full_refresh_policy( |
64 | 125 | plan=plan, |
65 | | - snapshots_config=invocation.discovered_inputs.project_config.snapshots, |
66 | | - allow_snapshot_full_refresh=request.allow_snapshot_full_refresh, |
67 | | - input_stream=sys.stdin, |
68 | | - output_stream=sys.stdout, |
| 126 | + snapshots_config=snapshots_config, |
| 127 | + allow_snapshot_full_refresh=gates.allow_snapshot_full_refresh, |
| 128 | + non_interactive_help=gates.snapshot_full_refresh_help, |
| 129 | + input_stream=input_stream, |
| 130 | + output_stream=output_stream, |
69 | 131 | ) |
70 | 132 | enforce_table_type_downgrade_policy( |
71 | 133 | plan=plan, |
72 | | - allow_table_type_downgrade=request.allow_table_type_downgrade, |
73 | | - input_stream=sys.stdin, |
74 | | - output_stream=sys.stdout, |
| 134 | + allow_table_type_downgrade=gates.allow_table_type_downgrade, |
| 135 | + non_interactive_help=gates.table_type_downgrade_help, |
| 136 | + input_stream=input_stream, |
| 137 | + output_stream=output_stream, |
75 | 138 | ) |
76 | 139 | enforce_retention_decrease_policy( |
77 | 140 | plan=plan, |
78 | | - allow_retention_decrease=request.allow_retention_decrease, |
79 | | - input_stream=sys.stdin, |
80 | | - output_stream=sys.stdout, |
| 141 | + allow_retention_decrease=gates.allow_retention_decrease, |
| 142 | + non_interactive_help=gates.retention_decrease_help, |
| 143 | + input_stream=input_stream, |
| 144 | + output_stream=output_stream, |
81 | 145 | ) |
82 | 146 |
|
83 | 147 |
|
|
0 commit comments