umf-access 0.1.0.dev8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- umf_access/__init__.py +109 -0
- umf_access/adoption.py +41 -0
- umf_access/build/__init__.py +2 -0
- umf_access/build/approximate.py +34 -0
- umf_access/build/catalog.py +1626 -0
- umf_access/build/commands.py +394 -0
- umf_access/build/pg_privileges.py +1544 -0
- umf_access/build/privileges.py +134 -0
- umf_access/build/search.py +365 -0
- umf_access/build/unity_search.py +104 -0
- umf_access/bulk.py +227 -0
- umf_access/canonical.py +23 -0
- umf_access/capture.py +410 -0
- umf_access/catalog.py +325 -0
- umf_access/codec_support.py +427 -0
- umf_access/commands.py +93 -0
- umf_access/delta_read.py +758 -0
- umf_access/dsl/__init__.py +148 -0
- umf_access/dsl/_context.py +36 -0
- umf_access/dsl/_expressions.py +625 -0
- umf_access/dsl/_joined.py +4 -0
- umf_access/dsl/_nodes.py +207 -0
- umf_access/dsl/_query.py +5559 -0
- umf_access/dsl/_selection.py +143 -0
- umf_access/dsl/navigation.py +302 -0
- umf_access/dsl/render.py +516 -0
- umf_access/dsl/search.py +343 -0
- umf_access/engine_catalog.py +563 -0
- umf_access/generated_core.py +5 -0
- umf_access/lifecycle/__init__.py +46 -0
- umf_access/lifecycle/_darwin_process_observer.c +306 -0
- umf_access/lifecycle/approval.py +366 -0
- umf_access/lifecycle/backfills.py +79 -0
- umf_access/lifecycle/catalog.py +289 -0
- umf_access/lifecycle/cli.py +231 -0
- umf_access/lifecycle/concurrent.py +196 -0
- umf_access/lifecycle/conversion_policy.py +188 -0
- umf_access/lifecycle/correspondence.py +203 -0
- umf_access/lifecycle/generation_authority_bootstrap.py +1491 -0
- umf_access/lifecycle/generation_authority_session.py +125 -0
- umf_access/lifecycle/generation_enrollment_authority.py +1108 -0
- umf_access/lifecycle/generation_enrollment_ledger.py +698 -0
- umf_access/lifecycle/generation_ingress.py +185 -0
- umf_access/lifecycle/generation_root_registry.py +138 -0
- umf_access/lifecycle/head.py +250 -0
- umf_access/lifecycle/head_view.py +81 -0
- umf_access/lifecycle/host.py +1183 -0
- umf_access/lifecycle/ingress_custody.py +750 -0
- umf_access/lifecycle/ingress_local_postgres.py +7990 -0
- umf_access/lifecycle/ingress_store.py +1488 -0
- umf_access/lifecycle/ledger.py +276 -0
- umf_access/lifecycle/ledger_schema.py +116 -0
- umf_access/lifecycle/mixed.py +593 -0
- umf_access/lifecycle/outcome_schema.py +136 -0
- umf_access/lifecycle/owner_approval_core.py +1810 -0
- umf_access/lifecycle/owner_authority.py +472 -0
- umf_access/lifecycle/owner_bootstrap.py +2077 -0
- umf_access/lifecycle/owner_wire.py +161 -0
- umf_access/lifecycle/phase_fence.py +52 -0
- umf_access/lifecycle/phase_policy.py +422 -0
- umf_access/lifecycle/privileges.py +145 -0
- umf_access/lifecycle/probe.py +39 -0
- umf_access/lifecycle/progress.py +497 -0
- umf_access/lifecycle/roles.py +96 -0
- umf_access/lifecycle/row_originals.py +1712 -0
- umf_access/lifecycle/row_originals_creation.py +759 -0
- umf_access/lifecycle/row_originals_receipt.py +413 -0
- umf_access/lifecycle/seeds.py +99 -0
- umf_access/lifecycle/sql_containment.py +358 -0
- umf_access/lifecycle/sql_containment_cli.py +33 -0
- umf_access/lifecycle/sql_containment_parser.py +20 -0
- umf_access/lifecycle/supervisor_journal_codec.py +1251 -0
- umf_access/lifecycle/table_boundary.py +22 -0
- umf_access/lifecycle/target_custody_codec.py +788 -0
- umf_access/lifecycle/witness.py +72 -0
- umf_access/managed_actions.py +271 -0
- umf_access/managed_bulk.py +353 -0
- umf_access/managed_delta.py +150 -0
- umf_access/managed_support.py +166 -0
- umf_access/manifest_commands.py +278 -0
- umf_access/native_actions.py +1007 -0
- umf_access/native_program.py +570 -0
- umf_access/pages.py +125 -0
- umf_access/postgres_bulk.py +509 -0
- umf_access/postgres_read.py +1230 -0
- umf_access/postgres_search.py +639 -0
- umf_access/protocols.py +665 -0
- umf_access/py.typed +0 -0
- umf_access/related_keys.py +233 -0
- umf_access/reviewed.py +706 -0
- umf_access/search_catalog.py +122 -0
- umf_access/targets.py +312 -0
- umf_access/types.py +220 -0
- umf_access/unified/__init__.py +187 -0
- umf_access/unified/_binding.py +654 -0
- umf_access/unified/head_session.py +144 -0
- umf_access/unified/native_binding.py +143 -0
- umf_access/unified/operation_values.py +173 -0
- umf_access/unified/outcome_values.py +76 -0
- umf_access/unified/session_async.py +161 -0
- umf_access/unified/session_binding.py +108 -0
- umf_access/unified/session_policy.py +152 -0
- umf_access/unified/session_psycopg2.py +70 -0
- umf_access/unified/session_sqlalchemy.py +161 -0
- umf_access/unified/session_sync.py +148 -0
- umf_access/unified/table_reads.py +314 -0
- umf_access/unified/table_writes.py +193 -0
- umf_access/unity_search.py +312 -0
- umf_access/values.py +280 -0
- umf_access/verification.py +64 -0
- umf_access-0.1.0.dev8.dist-info/METADATA +15 -0
- umf_access-0.1.0.dev8.dist-info/RECORD +117 -0
- umf_access-0.1.0.dev8.dist-info/WHEEL +4 -0
- umf_access-0.1.0.dev8.dist-info/entry_points.txt +4 -0
- umf_access-0.1.0.dev8.dist-info/licenses/LICENSE-APACHE +201 -0
- umf_access-0.1.0.dev8.dist-info/licenses/LICENSE-MIT +21 -0
- umf_access-0.1.0.dev8.dist-info/licenses/LICENSE.md +239 -0
|
@@ -0,0 +1,1183 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT OR Apache-2.0
|
|
2
|
+
"""Host-neutral execution boundary for source-verified table migrations.
|
|
3
|
+
|
|
4
|
+
The host owns credentials, target identity, locks, approval custody and native
|
|
5
|
+
transactions. UMF owns the verified plan and ordered phase state machine. A
|
|
6
|
+
host must provide whole-phase transactions; per-statement receipts do not meet
|
|
7
|
+
this protocol.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import hashlib
|
|
12
|
+
import re
|
|
13
|
+
from contextlib import AbstractContextManager, contextmanager
|
|
14
|
+
from dataclasses import dataclass
|
|
15
|
+
from typing import Any, Protocol, runtime_checkable
|
|
16
|
+
|
|
17
|
+
from .approval import ApprovalBinding, ApprovalReceipt, TargetIdentity
|
|
18
|
+
from .catalog import CatalogError
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True, slots=True)
|
|
22
|
+
class HostCapabilities:
|
|
23
|
+
"""Verified host guarantees required by the selected migration profile."""
|
|
24
|
+
|
|
25
|
+
exclusive_custody: bool
|
|
26
|
+
authenticated_target_identity: bool
|
|
27
|
+
durable_approval_custody: bool
|
|
28
|
+
durable_effect_fence: bool
|
|
29
|
+
whole_required_phase_atomicity: bool
|
|
30
|
+
durable_phase_receipts: bool
|
|
31
|
+
exact_native_observation: bool
|
|
32
|
+
uncertain_outcome_observation: bool
|
|
33
|
+
atomic_final_publication: bool
|
|
34
|
+
concurrent_autocommit: bool
|
|
35
|
+
|
|
36
|
+
def require(self, *, uses_concurrent: bool) -> None:
|
|
37
|
+
required = (
|
|
38
|
+
self.exclusive_custody,
|
|
39
|
+
self.authenticated_target_identity,
|
|
40
|
+
self.durable_approval_custody,
|
|
41
|
+
self.durable_effect_fence,
|
|
42
|
+
self.whole_required_phase_atomicity,
|
|
43
|
+
self.durable_phase_receipts,
|
|
44
|
+
self.exact_native_observation,
|
|
45
|
+
self.uncertain_outcome_observation,
|
|
46
|
+
self.atomic_final_publication,
|
|
47
|
+
)
|
|
48
|
+
if any(value is not True for value in required):
|
|
49
|
+
raise CatalogError("host-capability-unsupported")
|
|
50
|
+
if uses_concurrent and self.concurrent_autocommit is not True:
|
|
51
|
+
raise CatalogError("host-capability-unsupported")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass(frozen=True, slots=True)
|
|
55
|
+
class MigrationRequest:
|
|
56
|
+
"""Plan and caller-bound predecessor data needed to open host custody."""
|
|
57
|
+
|
|
58
|
+
plan: dict[str, Any]
|
|
59
|
+
expected_head: str
|
|
60
|
+
file_digest: str
|
|
61
|
+
binding: ApprovalBinding | None = None
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass(frozen=True, slots=True)
|
|
65
|
+
class PhaseReceipt:
|
|
66
|
+
"""Source-bound evidence of the completed contiguous phase prefix."""
|
|
67
|
+
|
|
68
|
+
phase_index: int
|
|
69
|
+
phase_digest: str
|
|
70
|
+
completed_steps: tuple[str, ...]
|
|
71
|
+
prefix_witness: dict[str, Any]
|
|
72
|
+
state: str = "complete"
|
|
73
|
+
|
|
74
|
+
def verify(self, phases: list[dict[str, Any]], plan: dict[str, Any]) -> None:
|
|
75
|
+
from .progress import _phase_digest
|
|
76
|
+
|
|
77
|
+
if (type(self.phase_index) is not int or not 0 <= self.phase_index <= len(phases)
|
|
78
|
+
or self.state not in {"ready", "intent", "complete"}
|
|
79
|
+
or type(self.completed_steps) is not tuple
|
|
80
|
+
or type(self.prefix_witness) is not dict):
|
|
81
|
+
raise CatalogError("progress-state-mismatch")
|
|
82
|
+
expected_steps = tuple(step for phase in phases[:self.phase_index]
|
|
83
|
+
for step in phase["steps"])
|
|
84
|
+
if self.completed_steps != expected_steps:
|
|
85
|
+
raise CatalogError("progress-phase-mismatch")
|
|
86
|
+
if self.state == "intent":
|
|
87
|
+
if (self.phase_index >= len(phases)
|
|
88
|
+
or phases[self.phase_index]["transaction"] != "forbidden"
|
|
89
|
+
or self.phase_digest != _phase_digest(
|
|
90
|
+
phases[self.phase_index],
|
|
91
|
+
_steps_for_phase(plan, phases[self.phase_index]))):
|
|
92
|
+
raise CatalogError("progress-phase-mismatch")
|
|
93
|
+
else:
|
|
94
|
+
expected = (_phase_digest(phases[self.phase_index - 1],
|
|
95
|
+
_steps_for_phase(plan, phases[self.phase_index - 1]))
|
|
96
|
+
if self.phase_index else hashlib.sha256(b"").hexdigest())
|
|
97
|
+
if self.phase_digest != expected:
|
|
98
|
+
raise CatalogError("progress-phase-mismatch")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@runtime_checkable
|
|
102
|
+
class RequiredPhaseTransaction(Protocol):
|
|
103
|
+
"""Atomic effect, native witness and completion receipt for one phase."""
|
|
104
|
+
|
|
105
|
+
def execute_statement(self, statement: str) -> None:
|
|
106
|
+
"""Run only the already source-verified statement for this phase."""
|
|
107
|
+
|
|
108
|
+
def execute_data_step(self, plan: dict[str, Any], step: dict[str, Any]) -> None:
|
|
109
|
+
"""Execute a typed Weft-owned data operation, never caller SQL."""
|
|
110
|
+
|
|
111
|
+
def verify_boundary(self, plan: dict[str, Any], phase: dict[str, Any],
|
|
112
|
+
prior: PhaseReceipt) -> None:
|
|
113
|
+
"""Lock and recheck native roles, ACLs and unaffected source prefix."""
|
|
114
|
+
|
|
115
|
+
def observe_prefix(self, plan: dict[str, Any]) -> dict[str, Any]:
|
|
116
|
+
"""Return exact native witness while the phase transaction is held."""
|
|
117
|
+
|
|
118
|
+
def record_completion(self, receipt: PhaseReceipt) -> None:
|
|
119
|
+
"""Persist completion in this same transaction as all phase effects."""
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@runtime_checkable
|
|
123
|
+
class MigrationLease(Protocol):
|
|
124
|
+
"""Exclusive host custody spanning approval, phases, recovery and publish."""
|
|
125
|
+
|
|
126
|
+
@property
|
|
127
|
+
def target(self) -> TargetIdentity: ...
|
|
128
|
+
|
|
129
|
+
def read_approval(self, binding: ApprovalBinding) -> ApprovalReceipt | None: ...
|
|
130
|
+
|
|
131
|
+
def verify_predecessor(self, request: MigrationRequest) -> None: ...
|
|
132
|
+
|
|
133
|
+
def read_progress(self, binding: ApprovalBinding) -> PhaseReceipt | None: ...
|
|
134
|
+
|
|
135
|
+
def observe_prefix(self, plan: dict[str, Any]) -> dict[str, Any]: ...
|
|
136
|
+
|
|
137
|
+
def begin(self, request: MigrationRequest, initial: PhaseReceipt) -> None:
|
|
138
|
+
"""Create selected metadata, fence the head and persist initial state atomically."""
|
|
139
|
+
|
|
140
|
+
def fence_head(self, request: MigrationRequest) -> None:
|
|
141
|
+
"""Durably deny generated clients before any committed phase effect."""
|
|
142
|
+
|
|
143
|
+
def required_phase(self, phase: dict[str, Any], *,
|
|
144
|
+
prior: PhaseReceipt | None) -> AbstractContextManager[RequiredPhaseTransaction]: ...
|
|
145
|
+
|
|
146
|
+
def record_concurrent_intent(self, phase: dict[str, Any], *,
|
|
147
|
+
prior: PhaseReceipt) -> PhaseReceipt: ...
|
|
148
|
+
|
|
149
|
+
def dispatch_concurrent(self, phase: dict[str, Any]) -> None: ...
|
|
150
|
+
|
|
151
|
+
def observe_concurrent(self, plan: dict[str, Any], phase: dict[str, Any],
|
|
152
|
+
prior: PhaseReceipt) -> tuple[str, dict[str, Any] | None]: ...
|
|
153
|
+
|
|
154
|
+
def record_concurrent_completion(self, plan: dict[str, Any], phase: dict[str, Any], *,
|
|
155
|
+
prior: PhaseReceipt,
|
|
156
|
+
native_witness: dict[str, Any]) -> PhaseReceipt: ...
|
|
157
|
+
|
|
158
|
+
def finalize(self, request: MigrationRequest,
|
|
159
|
+
approval: ApprovalReceipt) -> dict[str, Any]: ...
|
|
160
|
+
|
|
161
|
+
def observe_final_publication(self, request: MigrationRequest,
|
|
162
|
+
approval: ApprovalReceipt) -> dict[str, Any] | None: ...
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
@runtime_checkable
|
|
166
|
+
class MigrationHost(Protocol):
|
|
167
|
+
"""Typed host adapter; raw database connections are not part of this API."""
|
|
168
|
+
|
|
169
|
+
@property
|
|
170
|
+
def capabilities(self) -> HostCapabilities: ...
|
|
171
|
+
|
|
172
|
+
def acquire(self, plan: dict[str, Any], *, expected_head: str,
|
|
173
|
+
file_digest: str) -> AbstractContextManager[MigrationLease]: ...
|
|
174
|
+
|
|
175
|
+
def record_approval(self, request: MigrationRequest, *, decision: str,
|
|
176
|
+
qualification_profile: str,
|
|
177
|
+
evidence_digest: str) -> ApprovalReceipt: ...
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
class PsycopgMigrationHost:
|
|
181
|
+
"""Reference PostgreSQL adapter using one authenticated idle Psycopg session.
|
|
182
|
+
|
|
183
|
+
The connection remains private to this adapter. Callers get no raw SQL,
|
|
184
|
+
cursor, or callback surface; only source-selected plan operations can cross
|
|
185
|
+
the typed lease. The caller is responsible for opening the connection as
|
|
186
|
+
the installation owner and setting ``autocommit=True``.
|
|
187
|
+
"""
|
|
188
|
+
|
|
189
|
+
def __init__(self, connection: Any, *, person_roles: tuple[str, ...],
|
|
190
|
+
target_profile: str, installation_id: str,
|
|
191
|
+
statement_timeout_ms: int = 30_000,
|
|
192
|
+
lock_timeout_ms: int = 5_000):
|
|
193
|
+
self._connection = connection
|
|
194
|
+
self._person_roles = person_roles
|
|
195
|
+
self._target_profile = target_profile
|
|
196
|
+
self._installation_id = installation_id
|
|
197
|
+
self._statement_timeout_ms = statement_timeout_ms
|
|
198
|
+
self._lock_timeout_ms = lock_timeout_ms
|
|
199
|
+
self._capabilities = HostCapabilities(*(True for _ in range(10)))
|
|
200
|
+
|
|
201
|
+
@property
|
|
202
|
+
def capabilities(self) -> HostCapabilities:
|
|
203
|
+
return self._capabilities
|
|
204
|
+
|
|
205
|
+
@contextmanager
|
|
206
|
+
def acquire(self, plan: dict[str, Any], *, expected_head: str,
|
|
207
|
+
file_digest: str):
|
|
208
|
+
from .catalog import _one
|
|
209
|
+
|
|
210
|
+
connection = self._connection
|
|
211
|
+
if (not getattr(connection, "autocommit", False)
|
|
212
|
+
or int(connection.info.transaction_status) != 0):
|
|
213
|
+
raise CatalogError("idle-autocommit-required")
|
|
214
|
+
source = plan["next"]
|
|
215
|
+
schema = source["configuration"]["schema"]
|
|
216
|
+
lock_key = int.from_bytes(hashlib.sha256(
|
|
217
|
+
("umf.table-lifecycle/0.1\0" + schema).encode()).digest()[:8],
|
|
218
|
+
"big", signed=True)
|
|
219
|
+
if _one(connection, "SELECT pg_catalog.pg_try_advisory_lock(%s)",
|
|
220
|
+
(lock_key,)) != (True,):
|
|
221
|
+
raise CatalogError("migration-busy")
|
|
222
|
+
try:
|
|
223
|
+
target = _target_identity(connection, source, self._target_profile,
|
|
224
|
+
self._installation_id)
|
|
225
|
+
yield _PsycopgLease(self, plan, target, expected_head, file_digest)
|
|
226
|
+
finally:
|
|
227
|
+
try:
|
|
228
|
+
_one(connection, "SELECT pg_catalog.pg_advisory_unlock(%s)",
|
|
229
|
+
(lock_key,))
|
|
230
|
+
except Exception: # noqa: BLE001 -- cleanup boundary sanitizes native driver failures
|
|
231
|
+
raise CatalogError("migration-session-cleanup-failed-observe-progress") from None
|
|
232
|
+
|
|
233
|
+
@contextmanager
|
|
234
|
+
def _atomic(self):
|
|
235
|
+
try:
|
|
236
|
+
with self._connection.transaction():
|
|
237
|
+
yield
|
|
238
|
+
except Exception as error:
|
|
239
|
+
if type(error).__module__.startswith("psycopg"):
|
|
240
|
+
raise HostOutcomeUnknown("native-transaction-outcome-unknown") from None
|
|
241
|
+
raise
|
|
242
|
+
|
|
243
|
+
def record_approval(self, request: MigrationRequest, *, decision: str,
|
|
244
|
+
qualification_profile: str,
|
|
245
|
+
evidence_digest: str) -> ApprovalReceipt:
|
|
246
|
+
from .ledger import _authorize_new_effects, _verified_plan
|
|
247
|
+
from .progress import _verified_phases
|
|
248
|
+
|
|
249
|
+
plan = _verified_plan(request.plan)
|
|
250
|
+
if (plan.get("version") not in ("umf.table-migration/0.3", "umf.table-migration/0.4", "umf.table-migration/0.5")
|
|
251
|
+
or request.expected_head is None
|
|
252
|
+
or type(request.file_digest) is not str
|
|
253
|
+
or re.fullmatch(r"[a-f0-9]{64}", request.file_digest) is None):
|
|
254
|
+
raise CatalogError("host-migration-profile")
|
|
255
|
+
_authorize_new_effects(plan)
|
|
256
|
+
phases = _verified_phases(plan)
|
|
257
|
+
require_host_capabilities(self, uses_concurrent=any(
|
|
258
|
+
phase["transaction"] == "forbidden" for phase in phases))
|
|
259
|
+
with self.acquire(plan, expected_head=request.expected_head,
|
|
260
|
+
file_digest=request.file_digest) as lease:
|
|
261
|
+
binding = _approval_binding(lease.target, plan, request.file_digest,
|
|
262
|
+
request.expected_head, phases,
|
|
263
|
+
qualification_profile, evidence_digest)
|
|
264
|
+
if request.binding is not None and request.binding != binding:
|
|
265
|
+
raise CatalogError("approval-binding-invalid")
|
|
266
|
+
bound_request = MigrationRequest(plan, request.expected_head,
|
|
267
|
+
request.file_digest, binding)
|
|
268
|
+
lease.verify_predecessor(bound_request)
|
|
269
|
+
with self._atomic():
|
|
270
|
+
from .approval import _record_approval, _verify_approval_relation
|
|
271
|
+
from .catalog import _installation
|
|
272
|
+
with _installation(self._connection, plan["next"],
|
|
273
|
+
statement_timeout_ms=self._statement_timeout_ms,
|
|
274
|
+
lock_timeout_ms=self._lock_timeout_ms):
|
|
275
|
+
_verify_predecessor_metadata(self._connection, plan,
|
|
276
|
+
self._person_roles,
|
|
277
|
+
expected_head=request.expected_head)
|
|
278
|
+
_verify_approval_relation(self._connection, plan["next"],
|
|
279
|
+
create=True)
|
|
280
|
+
receipt = _record_approval(
|
|
281
|
+
self._connection, plan["next"], binding,
|
|
282
|
+
decision=decision,
|
|
283
|
+
qualification_profile=qualification_profile,
|
|
284
|
+
evidence_digest=evidence_digest)
|
|
285
|
+
if decision == "approved":
|
|
286
|
+
receipt.verify(binding)
|
|
287
|
+
elif receipt.binding_digest != binding.digest:
|
|
288
|
+
raise CatalogError("approval-receipt-mismatch")
|
|
289
|
+
return receipt
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
class _PsycopgLease:
|
|
293
|
+
def __init__(self, host: PsycopgMigrationHost, plan: dict[str, Any],
|
|
294
|
+
target: TargetIdentity, expected_head: str,
|
|
295
|
+
file_digest: str):
|
|
296
|
+
self.host, self.connection, self.plan = host, host._connection, plan
|
|
297
|
+
self._target, self.expected_head, self.file_digest = target, expected_head, file_digest
|
|
298
|
+
self.bundle = plan["next"]
|
|
299
|
+
self.schema = self.bundle["configuration"]["schema"]
|
|
300
|
+
self.binding: ApprovalBinding | None = None
|
|
301
|
+
|
|
302
|
+
@property
|
|
303
|
+
def target(self) -> TargetIdentity:
|
|
304
|
+
return self._target
|
|
305
|
+
|
|
306
|
+
def read_approval(self, binding: ApprovalBinding) -> ApprovalReceipt | None:
|
|
307
|
+
from .approval import _approval_row, _verify_approval_relation
|
|
308
|
+
from .catalog import _installation
|
|
309
|
+
|
|
310
|
+
with self.connection.transaction(), _installation(
|
|
311
|
+
self.connection, self.bundle,
|
|
312
|
+
statement_timeout_ms=self.host._statement_timeout_ms,
|
|
313
|
+
lock_timeout_ms=self.host._lock_timeout_ms):
|
|
314
|
+
if not _verify_approval_relation(self.connection, self.bundle):
|
|
315
|
+
return None
|
|
316
|
+
return _approval_row(self.connection, self.schema, binding)
|
|
317
|
+
|
|
318
|
+
def verify_predecessor(self, request: MigrationRequest) -> None:
|
|
319
|
+
from .catalog import _installation
|
|
320
|
+
from .ledger import _history_rows, _verify_installed
|
|
321
|
+
from .ledger_schema import _verify_history_schema
|
|
322
|
+
|
|
323
|
+
self.binding = request.binding
|
|
324
|
+
if (self.binding is not None and self.binding.target != self.target):
|
|
325
|
+
raise CatalogError("approval-target-mismatch")
|
|
326
|
+
with self.host._atomic(), _installation(
|
|
327
|
+
self.connection, self.bundle,
|
|
328
|
+
statement_timeout_ms=self.host._statement_timeout_ms,
|
|
329
|
+
lock_timeout_ms=self.host._lock_timeout_ms):
|
|
330
|
+
if not _verify_history_schema(self.connection, self.bundle):
|
|
331
|
+
raise CatalogError("ledger-history-missing")
|
|
332
|
+
history = _history_rows(self.connection, self.schema)
|
|
333
|
+
if not history:
|
|
334
|
+
raise CatalogError("ledger-history-missing")
|
|
335
|
+
matches = [entry for entry in history
|
|
336
|
+
if entry["plan"]["id"] == request.plan["id"]]
|
|
337
|
+
if matches:
|
|
338
|
+
match = matches[0]
|
|
339
|
+
prior_head = history[match["position"] - 2]["entryDigest"] if match["position"] > 1 else None
|
|
340
|
+
if (len(matches) != 1 or match["plan"]["digest"] != request.plan["digest"]
|
|
341
|
+
or match.get("fileDigest") != request.file_digest
|
|
342
|
+
or prior_head != request.expected_head):
|
|
343
|
+
raise CatalogError("ledger-digest-conflict")
|
|
344
|
+
_verify_installed(self.connection, history[-1], self.host._person_roles,
|
|
345
|
+
self.host._statement_timeout_ms,
|
|
346
|
+
self.host._lock_timeout_ms)
|
|
347
|
+
return
|
|
348
|
+
|
|
349
|
+
latest = history[-1]
|
|
350
|
+
if (latest["entryDigest"] != request.expected_head
|
|
351
|
+
or request.plan["predecessor"] != {
|
|
352
|
+
"id": latest["plan"]["id"],
|
|
353
|
+
"digest": latest["plan"]["digest"]}
|
|
354
|
+
or latest["plan"]["toBundleDigest"] != request.plan["fromBundleDigest"]
|
|
355
|
+
or request.plan["previous"] != latest["plan"]["next"]):
|
|
356
|
+
raise CatalogError("ledger-head-mismatch")
|
|
357
|
+
if request.plan.get("version") in ("umf.table-migration/0.3", "umf.table-migration/0.4", "umf.table-migration/0.5"):
|
|
358
|
+
_verify_transition_predecessor(self, request, latest, history)
|
|
359
|
+
else:
|
|
360
|
+
_verify_installed(self.connection, latest, self.host._person_roles,
|
|
361
|
+
self.host._statement_timeout_ms,
|
|
362
|
+
self.host._lock_timeout_ms)
|
|
363
|
+
_verify_predecessor_metadata(self.connection, request.plan,
|
|
364
|
+
self.host._person_roles,
|
|
365
|
+
expected_head=request.expected_head)
|
|
366
|
+
|
|
367
|
+
def read_progress(self, binding: ApprovalBinding) -> PhaseReceipt | None:
|
|
368
|
+
from .catalog import _installation
|
|
369
|
+
from .progress import (
|
|
370
|
+
_assert_progress_binding,
|
|
371
|
+
_progress_rows,
|
|
372
|
+
_verify_progress_schema,
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
with self.host._atomic(), _installation(
|
|
376
|
+
self.connection, self.bundle,
|
|
377
|
+
statement_timeout_ms=self.host._statement_timeout_ms,
|
|
378
|
+
lock_timeout_ms=self.host._lock_timeout_ms):
|
|
379
|
+
if not _verify_progress_schema(self.connection, self.bundle):
|
|
380
|
+
return None
|
|
381
|
+
rows = _progress_rows(self.connection, self.schema)
|
|
382
|
+
if not rows:
|
|
383
|
+
return None
|
|
384
|
+
row = rows[0]
|
|
385
|
+
_assert_progress_binding(row, migration_id=self.plan["id"],
|
|
386
|
+
plan_digest=self.plan["digest"], expected_head=binding.expected_head,
|
|
387
|
+
file_digest=binding.file_digest)
|
|
388
|
+
return _phase_receipt(row)
|
|
389
|
+
|
|
390
|
+
def observe_prefix(self, plan: dict[str, Any], *, phase_index: int = 0) -> dict[str, Any]:
|
|
391
|
+
from .mixed import _source_at_phase_prefix
|
|
392
|
+
from .witness import _capture_witness
|
|
393
|
+
|
|
394
|
+
return _capture_witness(self.connection,
|
|
395
|
+
_source_at_phase_prefix(plan, phase_index))
|
|
396
|
+
|
|
397
|
+
def begin(self, request: MigrationRequest, initial: PhaseReceipt) -> None:
|
|
398
|
+
from .catalog import _installation
|
|
399
|
+
from .mixed import (
|
|
400
|
+
_lock_phase_tables,
|
|
401
|
+
_source_at_phase_prefix,
|
|
402
|
+
_verify_phase_source_boundary,
|
|
403
|
+
_verify_source_metadata_inventory,
|
|
404
|
+
)
|
|
405
|
+
from .phase_fence import _head_grants
|
|
406
|
+
from .progress import _create_progress, _verify_progress_schema, _write_progress
|
|
407
|
+
|
|
408
|
+
with self.host._atomic(), _installation(
|
|
409
|
+
self.connection, self.bundle,
|
|
410
|
+
statement_timeout_ms=self.host._statement_timeout_ms,
|
|
411
|
+
lock_timeout_ms=self.host._lock_timeout_ms):
|
|
412
|
+
self._verify_bound_approval()
|
|
413
|
+
if _verify_progress_schema(self.connection, self.bundle):
|
|
414
|
+
raise CatalogError("progress-state-mismatch")
|
|
415
|
+
_create_progress(self.connection, self.schema)
|
|
416
|
+
_lock_phase_tables(self.connection, self.plan)
|
|
417
|
+
_verify_phase_source_boundary(self.connection, self.plan,
|
|
418
|
+
self.host._person_roles,
|
|
419
|
+
_source_at_phase_prefix(self.plan, 0))
|
|
420
|
+
from .witness import _capture_witness
|
|
421
|
+
current = _capture_witness(self.connection,
|
|
422
|
+
_source_at_phase_prefix(self.plan, 0))
|
|
423
|
+
if current != initial.prefix_witness:
|
|
424
|
+
raise CatalogError("progress-prefix-mismatch")
|
|
425
|
+
_verify_source_metadata_inventory(self.connection,
|
|
426
|
+
_source_at_phase_prefix(self.plan, 0),
|
|
427
|
+
self.plan.get('approvalProfile'))
|
|
428
|
+
_head_grants(self.connection, self.bundle, False)
|
|
429
|
+
_write_progress(self.connection, self.schema,
|
|
430
|
+
_receipt_row(self.plan, self.expected_head,
|
|
431
|
+
self.file_digest, initial))
|
|
432
|
+
|
|
433
|
+
def fence_head(self, request: MigrationRequest) -> None:
|
|
434
|
+
from .catalog import _installation
|
|
435
|
+
from .phase_fence import _head_grants, _verify_phase_head
|
|
436
|
+
|
|
437
|
+
with self.host._atomic(), _installation(
|
|
438
|
+
self.connection, self.bundle,
|
|
439
|
+
statement_timeout_ms=self.host._statement_timeout_ms,
|
|
440
|
+
lock_timeout_ms=self.host._lock_timeout_ms):
|
|
441
|
+
self._verify_bound_approval()
|
|
442
|
+
_verify_phase_head(self.connection, self.bundle)
|
|
443
|
+
_head_grants(self.connection, self.bundle, False)
|
|
444
|
+
|
|
445
|
+
@contextmanager
|
|
446
|
+
def required_phase(self, phase: dict[str, Any], *,
|
|
447
|
+
prior: PhaseReceipt | None):
|
|
448
|
+
from .catalog import _installation
|
|
449
|
+
from .mixed import (
|
|
450
|
+
_lock_phase_tables,
|
|
451
|
+
_source_at_phase_prefix,
|
|
452
|
+
_verify_phase_source_boundary,
|
|
453
|
+
_verify_source_metadata_inventory,
|
|
454
|
+
)
|
|
455
|
+
|
|
456
|
+
with self.host._atomic(), _installation(
|
|
457
|
+
self.connection, self.bundle,
|
|
458
|
+
statement_timeout_ms=self.host._statement_timeout_ms,
|
|
459
|
+
lock_timeout_ms=self.host._lock_timeout_ms):
|
|
460
|
+
self._verify_bound_approval()
|
|
461
|
+
_lock_phase_tables(self.connection, self.plan)
|
|
462
|
+
phase_index = prior.phase_index if prior is not None else 0
|
|
463
|
+
_verify_phase_source_boundary(self.connection, self.plan,
|
|
464
|
+
self.host._person_roles,
|
|
465
|
+
_source_at_phase_prefix(self.plan, phase_index))
|
|
466
|
+
_verify_source_metadata_inventory(self.connection,
|
|
467
|
+
_source_at_phase_prefix(self.plan, phase_index),
|
|
468
|
+
self.plan.get('approvalProfile'))
|
|
469
|
+
transaction = _PsycopgRequiredPhase(self, phase)
|
|
470
|
+
transaction.verify_boundary(self.plan, phase, prior)
|
|
471
|
+
yield transaction
|
|
472
|
+
|
|
473
|
+
def record_concurrent_intent(self, phase: dict[str, Any], *,
|
|
474
|
+
prior: PhaseReceipt) -> PhaseReceipt:
|
|
475
|
+
from .catalog import _installation
|
|
476
|
+
from .mixed import (
|
|
477
|
+
_lock_phase_tables,
|
|
478
|
+
_verify_mixed_metadata_inventory,
|
|
479
|
+
_verify_phase_execution_boundary,
|
|
480
|
+
)
|
|
481
|
+
from .progress import _write_progress
|
|
482
|
+
|
|
483
|
+
with self.host._atomic(), _installation(
|
|
484
|
+
self.connection, self.bundle,
|
|
485
|
+
statement_timeout_ms=self.host._statement_timeout_ms,
|
|
486
|
+
lock_timeout_ms=self.host._lock_timeout_ms):
|
|
487
|
+
self._verify_bound_approval()
|
|
488
|
+
_lock_phase_tables(self.connection, self.plan)
|
|
489
|
+
_verify_phase_execution_boundary(self.connection, self.plan,
|
|
490
|
+
self.host._person_roles)
|
|
491
|
+
_verify_mixed_metadata_inventory(self.connection, self.plan)
|
|
492
|
+
if self.observe_prefix(self.plan, phase_index=prior.phase_index) != prior.prefix_witness:
|
|
493
|
+
raise CatalogError("progress-prefix-mismatch")
|
|
494
|
+
intent = PhaseReceipt(prior.phase_index,
|
|
495
|
+
_phase_hash(self.plan, phase), prior.completed_steps,
|
|
496
|
+
prior.prefix_witness, "intent")
|
|
497
|
+
_write_progress(self.connection, self.schema,
|
|
498
|
+
_receipt_row(self.plan, self.expected_head, self.file_digest, intent))
|
|
499
|
+
return intent
|
|
500
|
+
|
|
501
|
+
def dispatch_concurrent(self, phase: dict[str, Any]) -> None:
|
|
502
|
+
from .catalog import _execute, _installation
|
|
503
|
+
from .mixed import _verify_phase_execution_boundary
|
|
504
|
+
|
|
505
|
+
steps = _steps_for_phase(self.plan, phase)
|
|
506
|
+
if (phase["kind"] != "concurrent-index" or len(steps) != 1
|
|
507
|
+
or steps[0]["transaction"] != "forbidden"):
|
|
508
|
+
raise CatalogError("mixed-phase-profile")
|
|
509
|
+
try:
|
|
510
|
+
with self.connection.transaction(), _installation(
|
|
511
|
+
self.connection, self.bundle,
|
|
512
|
+
statement_timeout_ms=self.host._statement_timeout_ms,
|
|
513
|
+
lock_timeout_ms=self.host._lock_timeout_ms):
|
|
514
|
+
self._verify_bound_approval()
|
|
515
|
+
_verify_phase_execution_boundary(self.connection, self.plan,
|
|
516
|
+
self.host._person_roles)
|
|
517
|
+
_execute(self.connection, steps[0]["sql"])
|
|
518
|
+
except Exception as error:
|
|
519
|
+
if type(error).__module__.startswith("psycopg"):
|
|
520
|
+
raise HostOutcomeUnknown("concurrent-dispatch-outcome-unknown") from None
|
|
521
|
+
raise
|
|
522
|
+
|
|
523
|
+
def observe_concurrent(self, plan: dict[str, Any], phase: dict[str, Any],
|
|
524
|
+
prior: PhaseReceipt) -> tuple[str, dict[str, Any] | None]:
|
|
525
|
+
from .catalog import _installation
|
|
526
|
+
from .mixed import (
|
|
527
|
+
_lock_phase_tables,
|
|
528
|
+
_verify_index_shape,
|
|
529
|
+
_verify_mixed_metadata_inventory,
|
|
530
|
+
_verify_phase_execution_boundary,
|
|
531
|
+
_without_concurrent_effect,
|
|
532
|
+
)
|
|
533
|
+
from .witness import _capture_witness
|
|
534
|
+
|
|
535
|
+
with self.connection.transaction(), _installation(
|
|
536
|
+
self.connection, self.bundle,
|
|
537
|
+
statement_timeout_ms=self.host._statement_timeout_ms,
|
|
538
|
+
lock_timeout_ms=self.host._lock_timeout_ms):
|
|
539
|
+
self._verify_bound_approval()
|
|
540
|
+
_lock_phase_tables(self.connection, self.plan)
|
|
541
|
+
_verify_phase_execution_boundary(self.connection, self.plan,
|
|
542
|
+
self.host._person_roles)
|
|
543
|
+
_verify_mixed_metadata_inventory(self.connection, self.plan)
|
|
544
|
+
current = _capture_witness(self.connection, self.bundle)
|
|
545
|
+
if _without_concurrent_effect(current, self.plan, phase) != prior.prefix_witness:
|
|
546
|
+
raise CatalogError("progress-prefix-mismatch")
|
|
547
|
+
step = _steps_for_phase(self.plan, phase)[0]
|
|
548
|
+
if _verify_index_shape(self.connection, self.bundle, step):
|
|
549
|
+
return "applied", current
|
|
550
|
+
return "absent", None
|
|
551
|
+
|
|
552
|
+
def record_concurrent_completion(self, plan: dict[str, Any], phase: dict[str, Any], *,
|
|
553
|
+
prior: PhaseReceipt,
|
|
554
|
+
native_witness: dict[str, Any]) -> PhaseReceipt:
|
|
555
|
+
from .catalog import _installation
|
|
556
|
+
from .mixed import (
|
|
557
|
+
_lock_phase_tables,
|
|
558
|
+
_verify_index_shape,
|
|
559
|
+
_verify_mixed_metadata_inventory,
|
|
560
|
+
_verify_phase_execution_boundary,
|
|
561
|
+
_without_concurrent_effect,
|
|
562
|
+
)
|
|
563
|
+
from .progress import _write_progress
|
|
564
|
+
from .witness import _capture_witness
|
|
565
|
+
|
|
566
|
+
with self.host._atomic(), _installation(
|
|
567
|
+
self.connection, self.bundle,
|
|
568
|
+
statement_timeout_ms=self.host._statement_timeout_ms,
|
|
569
|
+
lock_timeout_ms=self.host._lock_timeout_ms):
|
|
570
|
+
self._verify_bound_approval()
|
|
571
|
+
_lock_phase_tables(self.connection, self.plan)
|
|
572
|
+
_verify_phase_execution_boundary(self.connection, self.plan,
|
|
573
|
+
self.host._person_roles)
|
|
574
|
+
_verify_mixed_metadata_inventory(self.connection, self.plan)
|
|
575
|
+
current = _capture_witness(self.connection, self.bundle)
|
|
576
|
+
if (current != native_witness
|
|
577
|
+
or _without_concurrent_effect(current, self.plan, phase)
|
|
578
|
+
!= prior.prefix_witness
|
|
579
|
+
or not _verify_index_shape(self.connection, self.bundle,
|
|
580
|
+
_steps_for_phase(self.plan, phase)[0])):
|
|
581
|
+
raise CatalogError("progress-prefix-mismatch")
|
|
582
|
+
completed = PhaseReceipt(prior.phase_index + 1,
|
|
583
|
+
_phase_hash(self.plan, phase),
|
|
584
|
+
prior.completed_steps + tuple(phase["steps"]), current, "complete")
|
|
585
|
+
_write_progress(self.connection, self.schema,
|
|
586
|
+
_receipt_row(self.plan, self.expected_head, self.file_digest, completed))
|
|
587
|
+
return completed
|
|
588
|
+
|
|
589
|
+
def finalize(self, request: MigrationRequest,
|
|
590
|
+
approval: ApprovalReceipt) -> dict[str, Any]:
|
|
591
|
+
from .catalog import _installation
|
|
592
|
+
from .ledger import _finalize_phased_migration
|
|
593
|
+
from .mixed import _verify_mixed_metadata_inventory
|
|
594
|
+
from .phase_fence import _head_grants, _verify_phase_head
|
|
595
|
+
from .progress import _delete_progress, _drop_progress, _verify_progress_schema
|
|
596
|
+
from .witness import _capture_witness
|
|
597
|
+
|
|
598
|
+
binding = request.binding
|
|
599
|
+
if binding is None:
|
|
600
|
+
raise CatalogError("approval-binding-invalid")
|
|
601
|
+
approval.verify(binding)
|
|
602
|
+
phases = self.plan["phases"]
|
|
603
|
+
rows = self.read_progress(binding) if phases else None
|
|
604
|
+
if phases and (rows is None or rows.phase_index != len(phases)):
|
|
605
|
+
raise CatalogError("progress-phase-mismatch")
|
|
606
|
+
if phases:
|
|
607
|
+
rows.verify(phases, self.plan)
|
|
608
|
+
with self.host._atomic(), _installation(
|
|
609
|
+
self.connection, self.bundle,
|
|
610
|
+
statement_timeout_ms=self.host._statement_timeout_ms,
|
|
611
|
+
lock_timeout_ms=self.host._lock_timeout_ms):
|
|
612
|
+
self._verify_bound_approval(binding)
|
|
613
|
+
if phases:
|
|
614
|
+
_verify_phase_head(self.connection, self.bundle)
|
|
615
|
+
if _capture_witness(self.connection, self.bundle) != rows.prefix_witness:
|
|
616
|
+
raise CatalogError("progress-prefix-mismatch")
|
|
617
|
+
else:
|
|
618
|
+
from .ledger import _history_rows
|
|
619
|
+
from .mixed import (
|
|
620
|
+
_lock_phase_tables,
|
|
621
|
+
_verify_mixed_metadata_inventory,
|
|
622
|
+
_verify_phase_execution_boundary,
|
|
623
|
+
)
|
|
624
|
+
from .progress import _verify_progress_schema
|
|
625
|
+
from .witness import _capture_witness
|
|
626
|
+
|
|
627
|
+
if _verify_progress_schema(self.connection, self.bundle):
|
|
628
|
+
raise CatalogError("progress-state-mismatch")
|
|
629
|
+
_lock_phase_tables(self.connection, self.plan)
|
|
630
|
+
_verify_phase_execution_boundary(self.connection, self.plan,
|
|
631
|
+
self.host._person_roles,
|
|
632
|
+
progress_required=False)
|
|
633
|
+
history = _history_rows(self.connection, self.schema)
|
|
634
|
+
if (not history or history[-1]["entryDigest"] != request.expected_head
|
|
635
|
+
or _capture_witness(self.connection, self.bundle)
|
|
636
|
+
!= history[-1]["witness"]):
|
|
637
|
+
raise CatalogError("installed-generation-mismatch")
|
|
638
|
+
_verify_mixed_metadata_inventory(self.connection, self.plan,
|
|
639
|
+
progress_required=bool(phases))
|
|
640
|
+
if phases and not _verify_progress_schema(self.connection, self.bundle):
|
|
641
|
+
raise CatalogError("progress-state-mismatch")
|
|
642
|
+
_head_grants(self.connection, self.bundle, True)
|
|
643
|
+
if phases:
|
|
644
|
+
_delete_progress(self.connection, self.schema, self.plan["id"])
|
|
645
|
+
_drop_progress(self.connection, self.schema)
|
|
646
|
+
result = _finalize_phased_migration(
|
|
647
|
+
self.connection, self.plan, approved_digest=self.plan["digest"],
|
|
648
|
+
expected_head=request.expected_head,
|
|
649
|
+
person_roles=self.host._person_roles,
|
|
650
|
+
statement_timeout_ms=self.host._statement_timeout_ms,
|
|
651
|
+
lock_timeout_ms=self.host._lock_timeout_ms,
|
|
652
|
+
file_digest=request.file_digest)
|
|
653
|
+
return {**result, "status": "committed", "commitResponsibility": "adapter"}
|
|
654
|
+
|
|
655
|
+
def observe_final_publication(self, request: MigrationRequest,
|
|
656
|
+
approval: ApprovalReceipt) -> dict[str, Any] | None:
|
|
657
|
+
from .catalog import _installation
|
|
658
|
+
from .head import _issue_reference
|
|
659
|
+
from .ledger import _history_rows, _verify_installed
|
|
660
|
+
from .ledger_schema import _verify_history_schema
|
|
661
|
+
|
|
662
|
+
if request.binding is None:
|
|
663
|
+
return None
|
|
664
|
+
approval.verify(request.binding)
|
|
665
|
+
with self.connection.transaction(), _installation(
|
|
666
|
+
self.connection, self.bundle,
|
|
667
|
+
statement_timeout_ms=self.host._statement_timeout_ms,
|
|
668
|
+
lock_timeout_ms=self.host._lock_timeout_ms):
|
|
669
|
+
if not _verify_history_schema(self.connection, self.bundle):
|
|
670
|
+
return None
|
|
671
|
+
history = _history_rows(self.connection, self.schema)
|
|
672
|
+
matches = [item for item in history
|
|
673
|
+
if item["plan"]["digest"] == self.plan["digest"]]
|
|
674
|
+
if not matches:
|
|
675
|
+
return None
|
|
676
|
+
match = matches[0]
|
|
677
|
+
prior_head = history[match["position"] - 2]["entryDigest"] if match["position"] > 1 else None
|
|
678
|
+
if (match.get("fileDigest") != request.file_digest
|
|
679
|
+
or prior_head != request.expected_head):
|
|
680
|
+
raise CatalogError("ledger-digest-conflict")
|
|
681
|
+
_verify_installed(self.connection, history[-1], self.host._person_roles,
|
|
682
|
+
self.host._statement_timeout_ms,
|
|
683
|
+
self.host._lock_timeout_ms)
|
|
684
|
+
current_bundle = history[-1]["plan"]["next"]
|
|
685
|
+
return {"status": "already-applied", "migrationId": self.plan["id"],
|
|
686
|
+
"entryDigest": match["entryDigest"],
|
|
687
|
+
"headDigest": history[-1]["entryDigest"],
|
|
688
|
+
"generation": len(history),
|
|
689
|
+
"installedBundleDigest": current_bundle["bundleDigest"],
|
|
690
|
+
"installationReference": _issue_reference(
|
|
691
|
+
self.connection, current_bundle, history[-1]),
|
|
692
|
+
"commitResponsibility": "adapter"}
|
|
693
|
+
|
|
694
|
+
def _verify_bound_approval(self, binding: ApprovalBinding | None = None) -> None:
|
|
695
|
+
from .approval import _approval_row, _verify_approval_relation
|
|
696
|
+
|
|
697
|
+
selected = binding or self.binding
|
|
698
|
+
if selected is None or selected.target != self.target:
|
|
699
|
+
raise CatalogError("approval-binding-invalid")
|
|
700
|
+
if not _verify_approval_relation(self.connection, self.bundle):
|
|
701
|
+
raise CatalogError("approval-required")
|
|
702
|
+
receipt = _approval_row(self.connection, self.schema, selected)
|
|
703
|
+
if receipt is None:
|
|
704
|
+
raise CatalogError("approval-required")
|
|
705
|
+
receipt.verify(selected)
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
class _PsycopgRequiredPhase:
|
|
709
|
+
def __init__(self, lease: _PsycopgLease, phase: dict[str, Any]):
|
|
710
|
+
self.lease, self.phase = lease, phase
|
|
711
|
+
self.steps = _steps_for_phase(lease.plan, phase)
|
|
712
|
+
self.cursor = 0
|
|
713
|
+
self.prior: PhaseReceipt | None = None
|
|
714
|
+
|
|
715
|
+
def verify_boundary(self, plan: dict[str, Any], phase: dict[str, Any],
|
|
716
|
+
prior: PhaseReceipt) -> None:
|
|
717
|
+
from .mixed import (
|
|
718
|
+
_source_at_phase_prefix,
|
|
719
|
+
_verify_backfill_add_target,
|
|
720
|
+
_verify_backfill_tighten_target,
|
|
721
|
+
)
|
|
722
|
+
from .witness import _capture_witness
|
|
723
|
+
|
|
724
|
+
if phase["kind"] == "backfill-tighten":
|
|
725
|
+
_verify_backfill_tighten_target(plan, phase)
|
|
726
|
+
if phase["kind"] == "backfill-add":
|
|
727
|
+
_verify_backfill_add_target(plan, phase)
|
|
728
|
+
source = _source_at_phase_prefix(plan, prior.phase_index)
|
|
729
|
+
if _capture_witness(self.lease.connection, source) != prior.prefix_witness:
|
|
730
|
+
raise CatalogError("progress-prefix-mismatch")
|
|
731
|
+
self.prior = prior
|
|
732
|
+
|
|
733
|
+
def execute_statement(self, statement: str) -> None:
|
|
734
|
+
from .catalog import _execute
|
|
735
|
+
|
|
736
|
+
if self.cursor >= len(self.steps):
|
|
737
|
+
raise CatalogError("mixed-phase-profile")
|
|
738
|
+
step = self.steps[self.cursor]
|
|
739
|
+
if step.get("sql") != statement or step["transaction"] != "required":
|
|
740
|
+
raise CatalogError("mixed-phase-profile")
|
|
741
|
+
_execute(self.lease.connection, statement)
|
|
742
|
+
self.cursor += 1
|
|
743
|
+
|
|
744
|
+
def execute_data_step(self, plan: dict[str, Any], step: dict[str, Any]) -> None:
|
|
745
|
+
if (self.cursor >= len(self.steps) or step != self.steps[self.cursor]
|
|
746
|
+
or step["transaction"] != "required"):
|
|
747
|
+
raise CatalogError("mixed-phase-profile")
|
|
748
|
+
if step["kind"] == "backfill":
|
|
749
|
+
from .backfills import _apply_backfill
|
|
750
|
+
|
|
751
|
+
_apply_backfill(self.lease.connection, self.lease.plan, step)
|
|
752
|
+
elif step["kind"] == "seed":
|
|
753
|
+
from .seeds import _apply_seeds
|
|
754
|
+
|
|
755
|
+
if (step.get("operation") != "apply-seeds"
|
|
756
|
+
or step.get("seedSets") != self.lease.plan["seedExecution"].get("sets")):
|
|
757
|
+
raise CatalogError("seed-execution-profile")
|
|
758
|
+
_apply_seeds(self.lease.connection, self.lease.plan,
|
|
759
|
+
statement_timeout_ms=self.lease.host._statement_timeout_ms,
|
|
760
|
+
lock_timeout_ms=self.lease.host._lock_timeout_ms)
|
|
761
|
+
else:
|
|
762
|
+
raise CatalogError("mixed-phase-profile")
|
|
763
|
+
self.cursor += 1
|
|
764
|
+
|
|
765
|
+
def observe_prefix(self, plan: dict[str, Any]) -> dict[str, Any]:
|
|
766
|
+
from .mixed import _source_at_phase_prefix
|
|
767
|
+
from .witness import _capture_witness
|
|
768
|
+
|
|
769
|
+
phase_index = self.prior.phase_index if self.prior is not None else 0
|
|
770
|
+
if (plan.get('options', {}).get('version') == 'umf.table-migration-options/0.6'
|
|
771
|
+
and self.cursor == len(self.steps)):
|
|
772
|
+
phase_index += 1
|
|
773
|
+
return _capture_witness(self.lease.connection,
|
|
774
|
+
_source_at_phase_prefix(plan, phase_index))
|
|
775
|
+
|
|
776
|
+
def record_completion(self, receipt: PhaseReceipt) -> None:
|
|
777
|
+
from .mixed import (
|
|
778
|
+
_verify_mixed_metadata_inventory,
|
|
779
|
+
_verify_phase_execution_boundary,
|
|
780
|
+
)
|
|
781
|
+
from .progress import _write_progress
|
|
782
|
+
|
|
783
|
+
if (self.prior is None or self.cursor != len(self.steps)
|
|
784
|
+
or receipt.phase_index != self.prior.phase_index + 1
|
|
785
|
+
or receipt.state != "complete"
|
|
786
|
+
or receipt.completed_steps != self.prior.completed_steps
|
|
787
|
+
+ tuple(self.phase["steps"])
|
|
788
|
+
or receipt.phase_digest != _phase_hash(self.lease.plan, self.phase)):
|
|
789
|
+
raise CatalogError("progress-phase-mismatch")
|
|
790
|
+
_verify_phase_execution_boundary(self.lease.connection,
|
|
791
|
+
self.lease.plan,
|
|
792
|
+
self.lease.host._person_roles)
|
|
793
|
+
_verify_mixed_metadata_inventory(self.lease.connection, self.lease.plan)
|
|
794
|
+
_write_progress(self.lease.connection, self.lease.schema,
|
|
795
|
+
_receipt_row(self.lease.plan, self.lease.expected_head,
|
|
796
|
+
self.lease.file_digest, receipt))
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
def require_host_capabilities(host: MigrationHost, *, uses_concurrent: bool) -> None:
|
|
800
|
+
capabilities = host.capabilities
|
|
801
|
+
if not isinstance(capabilities, HostCapabilities):
|
|
802
|
+
raise CatalogError("host-capability-unsupported")
|
|
803
|
+
capabilities.require(uses_concurrent=uses_concurrent)
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
def verify_host_approval(lease: MigrationLease,
|
|
807
|
+
binding: ApprovalBinding) -> ApprovalReceipt:
|
|
808
|
+
receipt = lease.read_approval(binding)
|
|
809
|
+
if receipt is None:
|
|
810
|
+
raise CatalogError("approval-required")
|
|
811
|
+
receipt.verify(binding)
|
|
812
|
+
return receipt
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
class HostOutcomeUnknown(RuntimeError):
|
|
816
|
+
"""A dispatched native phase must be observed before its next action."""
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
def _steps_for_phase(plan: dict[str, Any], phase: dict[str, Any]) -> list[dict[str, Any]]:
|
|
820
|
+
by_id = {step["id"]: step for step in plan["steps"]}
|
|
821
|
+
try:
|
|
822
|
+
return [by_id[step_id] for step_id in phase["steps"]]
|
|
823
|
+
except (KeyError, TypeError):
|
|
824
|
+
raise CatalogError("mixed-phase-profile") from None
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
def apply_migration_on_host(host: MigrationHost, plan: dict[str, Any], *,
|
|
828
|
+
expected_head: str, file_digest: str,
|
|
829
|
+
qualification_profile: str, evidence_digest: str,
|
|
830
|
+
approval_id: str | None = None,
|
|
831
|
+
statement_timeout_ms: int = 30_000,
|
|
832
|
+
lock_timeout_ms: int = 5_000) -> dict[str, Any]:
|
|
833
|
+
"""Apply a current source-verified 0.3 plan through host custody.
|
|
834
|
+
|
|
835
|
+
The host supplies trusted identity, database custody and native transactions;
|
|
836
|
+
this function owns ordered phases, approval binding and recovery decisions.
|
|
837
|
+
No raw driver connection or host decision callback crosses this API.
|
|
838
|
+
"""
|
|
839
|
+
from .ledger import _authorize_new_effects, _verified_plan
|
|
840
|
+
from .progress import _phase_digest, _verified_phases
|
|
841
|
+
|
|
842
|
+
source_plan = _verified_plan(plan)
|
|
843
|
+
if (source_plan.get("version") not in ("umf.table-migration/0.3", "umf.table-migration/0.4", "umf.table-migration/0.5")
|
|
844
|
+
or source_plan.get("options", {}).get("version") not in ("umf.table-migration-options/0.3", "umf.table-migration-options/0.4", "umf.table-migration-options/0.5")
|
|
845
|
+
or (source_plan.get("version") == "umf.table-migration/0.3") != (source_plan.get("options", {}).get("version") == "umf.table-migration-options/0.3")
|
|
846
|
+
or (source_plan.get("version") == "umf.table-migration/0.4") != (source_plan.get("options", {}).get("version") == "umf.table-migration-options/0.4")
|
|
847
|
+
or (source_plan.get("version") == "umf.table-migration/0.5") != (source_plan.get("options", {}).get("version") == "umf.table-migration-options/0.5")
|
|
848
|
+
or source_plan.get("approvalProfile") != {
|
|
849
|
+
"version": "umf.table-migration-approval/0.1",
|
|
850
|
+
"relation": "umf_lifecycle_approvals", "ownerOnly": True}):
|
|
851
|
+
raise CatalogError("host-migration-profile")
|
|
852
|
+
if (type(expected_head) is not str or re.fullmatch(r"[a-f0-9]{64}", expected_head) is None
|
|
853
|
+
or type(file_digest) is not str or re.fullmatch(r"[a-f0-9]{64}", file_digest) is None):
|
|
854
|
+
raise CatalogError("migration-binding-invalid")
|
|
855
|
+
_authorize_new_effects(source_plan)
|
|
856
|
+
phases = _verified_phases(source_plan)
|
|
857
|
+
uses_concurrent = any(phase["transaction"] == "forbidden" for phase in phases)
|
|
858
|
+
require_host_capabilities(host, uses_concurrent=uses_concurrent)
|
|
859
|
+
|
|
860
|
+
with host.acquire(source_plan, expected_head=expected_head,
|
|
861
|
+
file_digest=file_digest) as lease:
|
|
862
|
+
target = lease.target
|
|
863
|
+
binding = ApprovalBinding(
|
|
864
|
+
target=target,
|
|
865
|
+
migration_id=source_plan["id"],
|
|
866
|
+
plan_digest=source_plan["digest"],
|
|
867
|
+
file_digest=file_digest,
|
|
868
|
+
source_bundle_digest=source_plan["toBundleDigest"],
|
|
869
|
+
expected_head=expected_head,
|
|
870
|
+
phase_digests=tuple(_phase_digest(phase, _steps_for_phase(source_plan, phase))
|
|
871
|
+
for phase in phases),
|
|
872
|
+
qualification_profile=qualification_profile,
|
|
873
|
+
evidence_digest=evidence_digest,
|
|
874
|
+
)
|
|
875
|
+
request = MigrationRequest(source_plan, expected_head, file_digest, binding)
|
|
876
|
+
lease.verify_predecessor(request)
|
|
877
|
+
approval = verify_host_approval(lease, binding)
|
|
878
|
+
if approval_id is not None and approval.receipt_id != approval_id:
|
|
879
|
+
raise CatalogError("approval-receipt-mismatch")
|
|
880
|
+
published = lease.observe_final_publication(request, approval)
|
|
881
|
+
if published is not None:
|
|
882
|
+
return published
|
|
883
|
+
if not phases:
|
|
884
|
+
try:
|
|
885
|
+
return lease.finalize(request, approval)
|
|
886
|
+
except HostOutcomeUnknown:
|
|
887
|
+
observed = lease.observe_final_publication(request, approval)
|
|
888
|
+
if observed is None:
|
|
889
|
+
raise CatalogError("migration-outcome-unknown-observe-history") from None
|
|
890
|
+
return observed
|
|
891
|
+
receipt = lease.read_progress(binding)
|
|
892
|
+
if receipt is None:
|
|
893
|
+
initial = PhaseReceipt(0, hashlib.sha256(b"").hexdigest(), (),
|
|
894
|
+
lease.observe_prefix(source_plan), "ready")
|
|
895
|
+
try:
|
|
896
|
+
lease.begin(request, initial)
|
|
897
|
+
except HostOutcomeUnknown:
|
|
898
|
+
observed = lease.read_progress(binding)
|
|
899
|
+
if observed != initial:
|
|
900
|
+
raise CatalogError("migration-outcome-unknown-observe-progress") from None
|
|
901
|
+
receipt = initial
|
|
902
|
+
else:
|
|
903
|
+
receipt.verify(phases, source_plan)
|
|
904
|
+
if receipt.state != "intent":
|
|
905
|
+
lease.fence_head(request)
|
|
906
|
+
|
|
907
|
+
while receipt.phase_index < len(phases):
|
|
908
|
+
index = receipt.phase_index
|
|
909
|
+
phase = phases[index]
|
|
910
|
+
phase_digest = binding.phase_digests[index]
|
|
911
|
+
if receipt.state == "intent":
|
|
912
|
+
state, witness = lease.observe_concurrent(source_plan, phase, receipt)
|
|
913
|
+
if state == "applied":
|
|
914
|
+
if type(witness) is not dict:
|
|
915
|
+
raise CatalogError("migration-outcome-unknown-observe-progress")
|
|
916
|
+
try:
|
|
917
|
+
receipt = lease.record_concurrent_completion(
|
|
918
|
+
source_plan, phase, prior=receipt, native_witness=witness)
|
|
919
|
+
except HostOutcomeUnknown:
|
|
920
|
+
observed = lease.read_progress(binding)
|
|
921
|
+
expected_steps = receipt.completed_steps + tuple(phase["steps"])
|
|
922
|
+
if (observed is None or observed.phase_index != index + 1
|
|
923
|
+
or observed.phase_digest != binding.phase_digests[index]
|
|
924
|
+
or observed.completed_steps != expected_steps):
|
|
925
|
+
raise CatalogError("migration-outcome-unknown-observe-progress") from None
|
|
926
|
+
observed.verify(phases, source_plan)
|
|
927
|
+
receipt = observed
|
|
928
|
+
continue
|
|
929
|
+
if state != "absent":
|
|
930
|
+
raise CatalogError("migration-outcome-unknown-observe-progress")
|
|
931
|
+
# A fresh exact absence observation permits a new dispatch; an
|
|
932
|
+
# unobserved dispatch is never retried in the same call.
|
|
933
|
+
prior_digest = (_phase_digest(phases[index - 1],
|
|
934
|
+
_steps_for_phase(source_plan, phases[index - 1]))
|
|
935
|
+
if index else hashlib.sha256(b"").hexdigest())
|
|
936
|
+
receipt = PhaseReceipt(receipt.phase_index, prior_digest,
|
|
937
|
+
receipt.completed_steps, receipt.prefix_witness, "ready")
|
|
938
|
+
if phase["transaction"] == "forbidden":
|
|
939
|
+
try:
|
|
940
|
+
receipt = lease.record_concurrent_intent(phase, prior=receipt)
|
|
941
|
+
except HostOutcomeUnknown:
|
|
942
|
+
observed = lease.read_progress(binding)
|
|
943
|
+
if (observed is None or observed.state != "intent"
|
|
944
|
+
or observed.phase_index != index
|
|
945
|
+
or observed.phase_digest != phase_digest):
|
|
946
|
+
raise CatalogError("migration-outcome-unknown-observe-progress") from None
|
|
947
|
+
receipt = observed
|
|
948
|
+
try:
|
|
949
|
+
lease.dispatch_concurrent(phase)
|
|
950
|
+
except HostOutcomeUnknown:
|
|
951
|
+
state, witness = lease.observe_concurrent(source_plan, phase, receipt)
|
|
952
|
+
if state != "applied" or type(witness) is not dict:
|
|
953
|
+
raise CatalogError("migration-outcome-unknown-observe-progress") from None
|
|
954
|
+
else:
|
|
955
|
+
state, witness = lease.observe_concurrent(source_plan, phase, receipt)
|
|
956
|
+
if state != "applied" or type(witness) is not dict:
|
|
957
|
+
raise CatalogError("migration-outcome-unknown-observe-progress")
|
|
958
|
+
try:
|
|
959
|
+
receipt = lease.record_concurrent_completion(
|
|
960
|
+
source_plan, phase, prior=receipt, native_witness=witness)
|
|
961
|
+
except HostOutcomeUnknown:
|
|
962
|
+
observed = lease.read_progress(binding)
|
|
963
|
+
expected_steps = receipt.completed_steps + tuple(phase["steps"])
|
|
964
|
+
if (observed is None or observed.phase_index != index + 1
|
|
965
|
+
or observed.phase_digest != phase_digest
|
|
966
|
+
or observed.completed_steps != expected_steps):
|
|
967
|
+
raise CatalogError("migration-outcome-unknown-observe-progress") from None
|
|
968
|
+
observed.verify(phases, source_plan)
|
|
969
|
+
receipt = observed
|
|
970
|
+
continue
|
|
971
|
+
|
|
972
|
+
try:
|
|
973
|
+
with lease.required_phase(phase, prior=receipt) as transaction:
|
|
974
|
+
transaction.verify_boundary(source_plan, phase, receipt)
|
|
975
|
+
if transaction.observe_prefix(source_plan) != receipt.prefix_witness:
|
|
976
|
+
raise CatalogError("progress-prefix-mismatch")
|
|
977
|
+
for step in _steps_for_phase(source_plan, phase):
|
|
978
|
+
if step["kind"] in {"backfill", "seed"}:
|
|
979
|
+
transaction.execute_data_step(source_plan, step)
|
|
980
|
+
else:
|
|
981
|
+
transaction.execute_statement(step["sql"])
|
|
982
|
+
witness = transaction.observe_prefix(source_plan)
|
|
983
|
+
completed = receipt.completed_steps + tuple(phase["steps"])
|
|
984
|
+
next_receipt = PhaseReceipt(index + 1, phase_digest, completed,
|
|
985
|
+
witness, "complete")
|
|
986
|
+
transaction.record_completion(next_receipt)
|
|
987
|
+
except HostOutcomeUnknown:
|
|
988
|
+
observed = lease.read_progress(binding)
|
|
989
|
+
if (observed is None or observed.state != "complete"
|
|
990
|
+
or observed.phase_index != index + 1
|
|
991
|
+
or observed.phase_digest != phase_digest
|
|
992
|
+
or observed.completed_steps != receipt.completed_steps + tuple(phase["steps"])):
|
|
993
|
+
raise CatalogError("migration-outcome-unknown-observe-progress") from None
|
|
994
|
+
observed.verify(phases, source_plan)
|
|
995
|
+
receipt = observed
|
|
996
|
+
continue
|
|
997
|
+
receipt = next_receipt
|
|
998
|
+
|
|
999
|
+
try:
|
|
1000
|
+
return lease.finalize(request, approval)
|
|
1001
|
+
except HostOutcomeUnknown:
|
|
1002
|
+
observed = lease.observe_final_publication(request, approval)
|
|
1003
|
+
if observed is None:
|
|
1004
|
+
raise CatalogError("migration-outcome-unknown-observe-history") from None
|
|
1005
|
+
return observed
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
def _target_identity(connection: Any, source: dict[str, Any],
|
|
1009
|
+
profile: str, installation_id: str) -> TargetIdentity:
|
|
1010
|
+
from .catalog import _one
|
|
1011
|
+
|
|
1012
|
+
row = _one(connection, "SELECT d.oid,d.datname FROM pg_catalog.pg_database d "
|
|
1013
|
+
"WHERE d.datname=pg_catalog.current_database()")
|
|
1014
|
+
cfg = source["configuration"]
|
|
1015
|
+
if (row is None or type(profile) is not str or not profile
|
|
1016
|
+
or type(installation_id) is not str or not installation_id
|
|
1017
|
+
or type(cfg.get("module")) is not str or not cfg["module"]):
|
|
1018
|
+
raise CatalogError("approval-target-identity-invalid")
|
|
1019
|
+
return TargetIdentity("postgresql", profile, installation_id, row[0], row[1],
|
|
1020
|
+
cfg["schema"], cfg["module"], cfg["ownerRole"])
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
def _approval_binding(target: TargetIdentity, plan: dict[str, Any],
|
|
1024
|
+
file_digest: str, expected_head: str,
|
|
1025
|
+
phases: list[dict[str, Any]], qualification_profile: str,
|
|
1026
|
+
evidence_digest: str) -> ApprovalBinding:
|
|
1027
|
+
from .progress import _phase_digest
|
|
1028
|
+
|
|
1029
|
+
return ApprovalBinding(target, plan["id"], plan["digest"], file_digest,
|
|
1030
|
+
plan["toBundleDigest"], expected_head,
|
|
1031
|
+
tuple(_phase_digest(phase, _steps_for_phase(plan, phase)) for phase in phases),
|
|
1032
|
+
qualification_profile, evidence_digest)
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
def _phase_hash(plan: dict[str, Any], phase: dict[str, Any]) -> str:
|
|
1036
|
+
from .progress import _phase_digest
|
|
1037
|
+
|
|
1038
|
+
return _phase_digest(phase, _steps_for_phase(plan, phase))
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
def _phase_receipt(row: dict[str, Any]) -> PhaseReceipt:
|
|
1042
|
+
if (type(row.get("phaseIndex")) is not int
|
|
1043
|
+
or type(row.get("completedSteps")) is not list
|
|
1044
|
+
or any(type(step) is not str for step in row["completedSteps"])
|
|
1045
|
+
or type(row.get("prefixWitness")) is not dict):
|
|
1046
|
+
raise CatalogError("progress-state-mismatch")
|
|
1047
|
+
return PhaseReceipt(row["phaseIndex"], row["phaseDigest"],
|
|
1048
|
+
tuple(row["completedSteps"]), row["prefixWitness"],
|
|
1049
|
+
row["state"])
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
def _receipt_row(plan: dict[str, Any], expected_head: str | None,
|
|
1053
|
+
file_digest: str | None, receipt: PhaseReceipt) -> dict[str, Any]:
|
|
1054
|
+
return {"migrationId": plan["id"], "planDigest": plan["digest"],
|
|
1055
|
+
"expectedHead": expected_head, "fileDigest": file_digest,
|
|
1056
|
+
"phaseIndex": receipt.phase_index, "state": receipt.state,
|
|
1057
|
+
"phaseDigest": receipt.phase_digest,
|
|
1058
|
+
"completedSteps": list(receipt.completed_steps),
|
|
1059
|
+
"prefixWitness": receipt.prefix_witness}
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
def _verify_predecessor_metadata(connection: Any, plan: dict[str, Any],
|
|
1063
|
+
person_roles: tuple[str, ...], *,
|
|
1064
|
+
expected_head: str | None = None) -> None:
|
|
1065
|
+
from .approval import APPROVAL_PROFILE, _verify_approval_relation
|
|
1066
|
+
from .catalog import _all, verify_native_catalog
|
|
1067
|
+
from .head_view import HEAD
|
|
1068
|
+
from .ledger import _history_rows
|
|
1069
|
+
from .ledger_schema import HISTORY, _verify_history_schema
|
|
1070
|
+
from .outcome_schema import OUTCOMES, _verify_outcomes
|
|
1071
|
+
from .phase_fence import _verify_phase_head
|
|
1072
|
+
from .privileges import verify_native_privileges
|
|
1073
|
+
|
|
1074
|
+
selected_profile = None
|
|
1075
|
+
if plan.get("approvalProfile") is not None:
|
|
1076
|
+
if (plan.get("version") not in ("umf.table-migration/0.3", "umf.table-migration/0.4", "umf.table-migration/0.5")
|
|
1077
|
+
or plan.get("options", {}).get("version") != plan.get("version", "").replace("umf.table-migration/", "umf.table-migration-options/")
|
|
1078
|
+
or plan.get("approvalProfile") != APPROVAL_PROFILE
|
|
1079
|
+
or plan.get("previous") is None
|
|
1080
|
+
or type(expected_head) is not str):
|
|
1081
|
+
raise CatalogError("approval-catalog-mismatch")
|
|
1082
|
+
history = _history_rows(connection, plan["previous"]["configuration"]["schema"])
|
|
1083
|
+
if (not history or history[-1]["entryDigest"] != expected_head
|
|
1084
|
+
or plan["previous"] != history[-1]["plan"]["next"]
|
|
1085
|
+
or plan["predecessor"] != {"id":history[-1]["plan"]["id"],
|
|
1086
|
+
"digest":history[-1]["plan"]["digest"]}):
|
|
1087
|
+
raise CatalogError("ledger-head-mismatch")
|
|
1088
|
+
selected_profile = APPROVAL_PROFILE
|
|
1089
|
+
|
|
1090
|
+
source = plan["previous"] or plan["next"]
|
|
1091
|
+
schema = source["configuration"]["schema"]
|
|
1092
|
+
if (not _verify_history_schema(connection, source)
|
|
1093
|
+
or not _verify_outcomes(connection, source)):
|
|
1094
|
+
raise CatalogError("ledger-history-missing")
|
|
1095
|
+
verify_native_catalog(connection, source)
|
|
1096
|
+
approval_present = False
|
|
1097
|
+
if selected_profile is not None:
|
|
1098
|
+
approval_present = _verify_approval_relation(connection, source)
|
|
1099
|
+
verify_native_privileges(connection, source, person_roles=person_roles,
|
|
1100
|
+
_approval_profile=selected_profile)
|
|
1101
|
+
_verify_phase_head(connection, source)
|
|
1102
|
+
rows = _all(connection, "SELECT c.relname,c.relkind FROM pg_catalog.pg_class c "
|
|
1103
|
+
"JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace "
|
|
1104
|
+
"WHERE n.nspname=%s AND c.relkind IN ('r','p','v','m','S','f') ORDER BY c.relname",
|
|
1105
|
+
(schema,))
|
|
1106
|
+
allowed = {table["name"]: "r" for table in source["expectedCatalog"]["tables"]}
|
|
1107
|
+
allowed.update({HISTORY: "r", OUTCOMES: "r", HEAD: "v"})
|
|
1108
|
+
from .progress import PROGRESS, _verify_progress_schema
|
|
1109
|
+
if approval_present:
|
|
1110
|
+
allowed[APPROVAL_PROFILE["relation"]] = "r"
|
|
1111
|
+
if _verify_progress_schema(connection, plan["next"]):
|
|
1112
|
+
allowed[PROGRESS] = "r"
|
|
1113
|
+
if any((name not in allowed)
|
|
1114
|
+
or (name in allowed and kind != allowed[name]) for name, kind in rows):
|
|
1115
|
+
raise CatalogError("progress-inventory-mismatch")
|
|
1116
|
+
if (APPROVAL_PROFILE["relation"] in {name for name, _ in rows}
|
|
1117
|
+
and not approval_present):
|
|
1118
|
+
raise CatalogError("progress-inventory-mismatch")
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
def _verify_transition_predecessor(lease: _PsycopgLease,
|
|
1122
|
+
request: MigrationRequest,
|
|
1123
|
+
latest: dict[str, Any],
|
|
1124
|
+
history: list[dict[str, Any]]) -> None:
|
|
1125
|
+
"""Admit only the exact 0.3 predecessor transition or its receipt prefix."""
|
|
1126
|
+
from .approval import APPROVAL_PROFILE
|
|
1127
|
+
from .catalog import _installation
|
|
1128
|
+
from .mixed import (
|
|
1129
|
+
_lock_phase_tables,
|
|
1130
|
+
_verify_mixed_prefix,
|
|
1131
|
+
_verify_phase_execution_boundary,
|
|
1132
|
+
)
|
|
1133
|
+
from .progress import (
|
|
1134
|
+
_assert_progress_binding,
|
|
1135
|
+
_progress_rows,
|
|
1136
|
+
_verify_progress_schema,
|
|
1137
|
+
)
|
|
1138
|
+
|
|
1139
|
+
plan = request.plan
|
|
1140
|
+
if (plan.get("version") not in ("umf.table-migration/0.3", "umf.table-migration/0.4", "umf.table-migration/0.5")
|
|
1141
|
+
or plan.get("options", {}).get("version") != plan.get("version", "").replace("umf.table-migration/", "umf.table-migration-options/")
|
|
1142
|
+
or plan.get("approvalProfile") != APPROVAL_PROFILE
|
|
1143
|
+
or plan.get("previous") != latest["plan"]["next"]
|
|
1144
|
+
or latest["entryDigest"] != request.expected_head
|
|
1145
|
+
or plan["predecessor"] != {"id":latest["plan"]["id"],
|
|
1146
|
+
"digest":latest["plan"]["digest"]}):
|
|
1147
|
+
raise CatalogError("approval-catalog-mismatch")
|
|
1148
|
+
source = plan["next"]
|
|
1149
|
+
schema = source["configuration"]["schema"]
|
|
1150
|
+
with _installation(lease.connection, source,
|
|
1151
|
+
statement_timeout_ms=lease.host._statement_timeout_ms,
|
|
1152
|
+
lock_timeout_ms=lease.host._lock_timeout_ms):
|
|
1153
|
+
progress_present = _verify_progress_schema(lease.connection, source)
|
|
1154
|
+
if progress_present:
|
|
1155
|
+
rows = _progress_rows(lease.connection, schema)
|
|
1156
|
+
if len(rows) != 1 or lease.binding is None:
|
|
1157
|
+
raise CatalogError("progress-state-mismatch")
|
|
1158
|
+
row = rows[0]
|
|
1159
|
+
_assert_progress_binding(row, migration_id=plan["id"],
|
|
1160
|
+
plan_digest=plan["digest"], expected_head=request.expected_head,
|
|
1161
|
+
file_digest=request.file_digest)
|
|
1162
|
+
receipt = _phase_receipt(row)
|
|
1163
|
+
receipt.verify(plan["phases"], plan)
|
|
1164
|
+
_lock_phase_tables(lease.connection, plan)
|
|
1165
|
+
_verify_phase_execution_boundary(lease.connection, plan,
|
|
1166
|
+
lease.host._person_roles)
|
|
1167
|
+
_verify_mixed_prefix(lease.connection, plan, row)
|
|
1168
|
+
return
|
|
1169
|
+
|
|
1170
|
+
_verify_predecessor_metadata(lease.connection, plan,
|
|
1171
|
+
lease.host._person_roles, expected_head=request.expected_head)
|
|
1172
|
+
from .witness import _capture_witness
|
|
1173
|
+
|
|
1174
|
+
if _capture_witness(lease.connection, latest["plan"]["next"]) != latest["witness"]:
|
|
1175
|
+
raise CatalogError("installed-generation-mismatch")
|
|
1176
|
+
|
|
1177
|
+
|
|
1178
|
+
def _verify_approval_available(connection: Any, bundle: dict[str, Any]) -> None:
|
|
1179
|
+
from .approval import _verify_approval_relation
|
|
1180
|
+
|
|
1181
|
+
if not _verify_approval_relation(connection, bundle):
|
|
1182
|
+
raise CatalogError("approval-required")
|
|
1183
|
+
|