|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from dataclasses import replace |
5 | 6 | from datetime import UTC, datetime |
6 | 7 | from typing import Any |
7 | 8 |
|
|
14 | 15 | collect_query_diff_artifact_candidates, |
15 | 16 | gather_janitor_warehouse_facts, |
16 | 17 | ) |
17 | | -from sqlbuild.executor.janitor._helpers.plan import collect_scan_schemas, collect_target_schemas |
| 18 | +from sqlbuild.executor.janitor._helpers.plan import ( |
| 19 | + collect_scan_schemas, |
| 20 | + collect_target_schemas, |
| 21 | + pending_migration_origins, |
| 22 | +) |
18 | 23 | from sqlbuild.executor.janitor._helpers.schema_planning import classify_target_schemas |
19 | 24 | from sqlbuild.executor.janitor.classes.relation_age_reader import JanitorRelationAgeReader |
20 | 25 | from sqlbuild.executor.janitor.models import ( |
|
23 | 28 | JanitorDirectStatePruneCandidate, |
24 | 29 | JanitorOldNameViewPlanning, |
25 | 30 | JanitorPlan, |
| 31 | + JanitorRelationKey, |
26 | 32 | JanitorRelationScope, |
27 | 33 | JanitorSchemaClassification, |
28 | 34 | JanitorWarehouseFacts, |
@@ -107,12 +113,11 @@ def build_janitor_plan( |
107 | 113 | ) |
108 | 114 | inspection.completed(metadata={"item_count": len(target_schemas)}) |
109 | 115 | old_names: JanitorOldNameViewPlanning |
110 | | - old_names, scope = plan_old_name_view_cleanup( |
| 116 | + old_names, scope = _plan_protected_names( |
111 | 117 | adapter=adapter, |
112 | 118 | connection=connection, |
113 | 119 | managed_target_schemas=managed_target_schemas, |
114 | | - relations_by_schema=facts.relations_by_schema, |
115 | | - target_name=project.effective_target_name, |
| 120 | + facts=facts, |
116 | 121 | early_drops=early_old_name_view_drops, |
117 | 122 | project=project, |
118 | 123 | scope=scope, |
@@ -173,3 +178,56 @@ def build_janitor_plan( |
173 | 178 | age_metadata_supported=age_supported, |
174 | 179 | planned_at=now, |
175 | 180 | ) |
| 181 | + |
| 182 | + |
| 183 | +def _plan_protected_names( |
| 184 | + *, |
| 185 | + adapter: BaseAdapter, |
| 186 | + connection: Any, |
| 187 | + managed_target_schemas: set[tuple[str | None, str | None]], |
| 188 | + facts: JanitorWarehouseFacts, |
| 189 | + early_drops: tuple[str, ...], |
| 190 | + project: CompiledProject, |
| 191 | + scope: JanitorRelationScope, |
| 192 | + now: datetime, |
| 193 | +) -> tuple[JanitorOldNameViewPlanning, JanitorRelationScope]: |
| 194 | + """Plan old-name views, then keep general cleanup away from pending migration origins.""" |
| 195 | + |
| 196 | + old_names: JanitorOldNameViewPlanning |
| 197 | + old_names, scope = plan_old_name_view_cleanup( |
| 198 | + adapter=adapter, |
| 199 | + connection=connection, |
| 200 | + managed_target_schemas=managed_target_schemas, |
| 201 | + relations_by_schema=facts.relations_by_schema, |
| 202 | + target_name=project.effective_target_name, |
| 203 | + early_drops=early_drops, |
| 204 | + project=project, |
| 205 | + scope=scope, |
| 206 | + now=now, |
| 207 | + ) |
| 208 | + return old_names, _protect_pending_origins( |
| 209 | + project=project, facts=facts, scope=scope, old_names=old_names |
| 210 | + ) |
| 211 | + |
| 212 | + |
| 213 | +def _protect_pending_origins( |
| 214 | + *, |
| 215 | + project: CompiledProject, |
| 216 | + facts: JanitorWarehouseFacts, |
| 217 | + scope: JanitorRelationScope, |
| 218 | + old_names: JanitorOldNameViewPlanning, |
| 219 | +) -> JanitorRelationScope: |
| 220 | + pending: dict[JanitorRelationKey, str] = { |
| 221 | + key: reason |
| 222 | + for key, reason in pending_migration_origins( |
| 223 | + project=project, relations_by_schema=facts.relations_by_schema |
| 224 | + ).items() |
| 225 | + if key not in old_names.keys |
| 226 | + } |
| 227 | + if not pending: |
| 228 | + return scope |
| 229 | + return replace( |
| 230 | + scope, |
| 231 | + protected_relation_keys=scope.protected_relation_keys | frozenset(pending), |
| 232 | + protected_relation_reasons={**(scope.protected_relation_reasons or {}), **pending}, |
| 233 | + ) |
0 commit comments