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,276 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT OR Apache-2.0
|
|
2
|
+
"""Approved, source-bound transactional migration application and durable replay.
|
|
3
|
+
|
|
4
|
+
The caller owns commit and trusted approval/head inputs. These administrative
|
|
5
|
+
methods never admit generated data operations or infer that commit succeeded.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import re
|
|
10
|
+
from typing import Any, cast
|
|
11
|
+
|
|
12
|
+
from .backfills import _apply_backfill
|
|
13
|
+
from .catalog import (
|
|
14
|
+
CatalogError,
|
|
15
|
+
_all,
|
|
16
|
+
_execute,
|
|
17
|
+
_installation,
|
|
18
|
+
_one,
|
|
19
|
+
_q,
|
|
20
|
+
_q_bound,
|
|
21
|
+
verify_native_catalog,
|
|
22
|
+
)
|
|
23
|
+
from .head import _issue_reference
|
|
24
|
+
from .head_view import _create_head_view
|
|
25
|
+
from .ledger_schema import HISTORY, _create_history, _verify_history_schema
|
|
26
|
+
from .outcome_schema import _create_outcomes
|
|
27
|
+
from .privileges import verify_native_privileges
|
|
28
|
+
from .seeds import _apply_seeds
|
|
29
|
+
from .witness import _capture_witness, _entry_digest
|
|
30
|
+
|
|
31
|
+
_MAX_ENTRIES = 128
|
|
32
|
+
_MAX_HISTORY_BYTES = 32*1024*1024
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _verified_plan(plan: dict[str, object]) -> dict[str, Any]:
|
|
36
|
+
from umf_codegen import verify_table_migration
|
|
37
|
+
|
|
38
|
+
try:
|
|
39
|
+
return cast(dict[str, Any],verify_table_migration(plan))
|
|
40
|
+
except (ValueError,TypeError):
|
|
41
|
+
raise CatalogError('source-mismatch') from None
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _authorize_new_effects(plan: dict[str, Any]) -> None:
|
|
45
|
+
"""Require current public compilation admission before first-time effects.
|
|
46
|
+
|
|
47
|
+
`_verified_plan` deliberately accepts exact historical v0.1 plans so
|
|
48
|
+
installed history remains verifiable. That compatibility path is not
|
|
49
|
+
authorization to execute a previously unreviewed v0.1 change today.
|
|
50
|
+
Recompile through the public entry point and require its exact result.
|
|
51
|
+
"""
|
|
52
|
+
from umf_codegen import compile_table_migration
|
|
53
|
+
|
|
54
|
+
request = {
|
|
55
|
+
'migration_id': plan['id'],
|
|
56
|
+
'previous_bundle': plan['previous'],
|
|
57
|
+
'next_bundle': plan['next'],
|
|
58
|
+
'predecessor': plan['predecessor'],
|
|
59
|
+
}
|
|
60
|
+
if 'options' in plan:
|
|
61
|
+
request['options'] = plan['options']
|
|
62
|
+
try:
|
|
63
|
+
admitted = compile_table_migration(**request)
|
|
64
|
+
except (ValueError, TypeError):
|
|
65
|
+
raise CatalogError('new-effect-admission-required') from None
|
|
66
|
+
if admitted.get('status') != 'compiled' or admitted.get('plan') != plan:
|
|
67
|
+
raise CatalogError('new-effect-admission-required')
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _history_rows(connection: Any, schema: str) -> list[dict[str, Any]]:
|
|
71
|
+
relation = _q(schema)+'.'+_q(HISTORY)
|
|
72
|
+
count, size = _one(connection,'SELECT count(*),COALESCE(sum(pg_catalog.octet_length(payload::pg_catalog.text)),0) FROM '+relation)
|
|
73
|
+
if count > _MAX_ENTRIES or size > _MAX_HISTORY_BYTES:
|
|
74
|
+
raise CatalogError('ledger-capacity')
|
|
75
|
+
try:
|
|
76
|
+
rows = _all(connection,'SELECT position,migration_id,predecessor_entry_digest,plan_digest,entry_digest,payload FROM '+relation+' ORDER BY position',())
|
|
77
|
+
except RecursionError:
|
|
78
|
+
raise CatalogError('ledger-value-capacity') from None
|
|
79
|
+
history = []
|
|
80
|
+
previous: dict[str, Any] | None = None
|
|
81
|
+
for position, row in enumerate(rows,1):
|
|
82
|
+
number, migration_id, predecessor, plan_digest, entry_digest, payload = row
|
|
83
|
+
if type(payload) is not dict or set(payload) not in ({'plan','witness'},{'plan','witness','fileDigest'}) or type(payload['plan']) is not dict or type(payload['witness']) is not dict:
|
|
84
|
+
raise CatalogError('ledger-digest-conflict')
|
|
85
|
+
if 'fileDigest' in payload and (type(payload['fileDigest']) is not str or not re.fullmatch('[a-f0-9]{64}',payload['fileDigest'])):
|
|
86
|
+
raise CatalogError('ledger-digest-conflict')
|
|
87
|
+
try:
|
|
88
|
+
plan = _verified_plan(payload['plan'])
|
|
89
|
+
except CatalogError:
|
|
90
|
+
raise CatalogError('ledger-digest-conflict') from None
|
|
91
|
+
if (number != position or migration_id != plan['id'] or plan_digest != plan['digest']
|
|
92
|
+
or predecessor != (previous['entryDigest'] if previous else None)
|
|
93
|
+
or entry_digest != _entry_digest(position,predecessor,plan_digest,payload['witness'],payload.get('fileDigest'))
|
|
94
|
+
or plan['predecessor'] != ({'id':previous['plan']['id'],'digest':previous['plan']['digest']} if previous else None)
|
|
95
|
+
or plan['fromBundleDigest'] != (previous['plan']['toBundleDigest'] if previous else None)):
|
|
96
|
+
raise CatalogError('ledger-digest-conflict')
|
|
97
|
+
previous = {'position':position,'entryDigest':entry_digest,'plan':plan,'witness':payload['witness'],'fileDigest':payload.get('fileDigest')}
|
|
98
|
+
history.append(previous)
|
|
99
|
+
return history
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _verify_installed(connection: Any, entry: dict[str, Any], person_roles: tuple[str, ...],
|
|
103
|
+
statement_timeout_ms: int, lock_timeout_ms: int) -> None:
|
|
104
|
+
source = entry['plan']['next']
|
|
105
|
+
approval_profile = entry['plan'].get('approvalProfile')
|
|
106
|
+
selected_profile = None
|
|
107
|
+
if entry['plan'].get('version') in ('umf.table-migration/0.3','umf.table-migration/0.4','umf.table-migration/0.5'):
|
|
108
|
+
from .approval import APPROVAL_PROFILE, _verify_approval_relation
|
|
109
|
+
|
|
110
|
+
if approval_profile != APPROVAL_PROFILE or not _verify_approval_relation(connection, source):
|
|
111
|
+
raise CatalogError('approval-catalog-mismatch')
|
|
112
|
+
selected_profile = APPROVAL_PROFILE
|
|
113
|
+
verify_native_catalog(connection,source,statement_timeout_ms=statement_timeout_ms,lock_timeout_ms=lock_timeout_ms)
|
|
114
|
+
verify_native_privileges(connection,source,person_roles=person_roles,
|
|
115
|
+
statement_timeout_ms=statement_timeout_ms,lock_timeout_ms=lock_timeout_ms,
|
|
116
|
+
_approval_profile=selected_profile)
|
|
117
|
+
if _capture_witness(connection,source) != entry['witness']:
|
|
118
|
+
raise CatalogError('installed-generation-mismatch')
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def apply_table_migration(connection: Any, plan: dict[str, object], *, approved_digest: str,
|
|
122
|
+
expected_head: str | None, person_roles: tuple[str, ...],
|
|
123
|
+
statement_timeout_ms: int = 30_000,
|
|
124
|
+
lock_timeout_ms: int = 5_000, file_digest: str | None = None) -> dict[str, object]:
|
|
125
|
+
"""Apply/replay an approved initial/append plan in a caller-owned transaction.
|
|
126
|
+
|
|
127
|
+
expected_head is the prior receipt's entryDigest (initial None), not a
|
|
128
|
+
value inferred from the database. History and effects roll back together.
|
|
129
|
+
A successful result is pending the caller's commit; no writes are retried.
|
|
130
|
+
"""
|
|
131
|
+
return _apply_table_migration(connection,plan,approved_digest=approved_digest,
|
|
132
|
+
expected_head=expected_head,person_roles=person_roles,
|
|
133
|
+
statement_timeout_ms=statement_timeout_ms,lock_timeout_ms=lock_timeout_ms,file_digest=file_digest)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _apply_table_migration(connection,plan,*,approved_digest,expected_head,person_roles,
|
|
137
|
+
statement_timeout_ms=30_000,lock_timeout_ms=5_000,prebuilt_indexes=False,file_digest=None,
|
|
138
|
+
phases_applied=False):
|
|
139
|
+
from psycopg.types.json import Jsonb
|
|
140
|
+
|
|
141
|
+
if file_digest is not None and (type(file_digest) is not str or not re.fullmatch('[a-f0-9]{64}',file_digest)):
|
|
142
|
+
raise CatalogError('migration-file-digest-conflict')
|
|
143
|
+
source_plan = _verified_plan(plan)
|
|
144
|
+
if source_plan.get('phases') and not phases_applied:
|
|
145
|
+
raise CatalogError('mixed-phase-runner-required')
|
|
146
|
+
concurrent = any(step['transaction'] == 'forbidden' for step in source_plan['steps'])
|
|
147
|
+
if concurrent and not prebuilt_indexes and not phases_applied:
|
|
148
|
+
raise CatalogError('nontransactional-runner-required')
|
|
149
|
+
if (phases_applied and not source_plan.get('phases')
|
|
150
|
+
and source_plan.get('steps')):
|
|
151
|
+
raise CatalogError('mixed-phase-profile')
|
|
152
|
+
if prebuilt_indexes and (not concurrent or any(step['kind'] != 'index' for step in source_plan['steps'])):
|
|
153
|
+
raise CatalogError('index-phase-profile')
|
|
154
|
+
if type(approved_digest) is not str or approved_digest != source_plan['digest']:
|
|
155
|
+
raise CatalogError('approval-required')
|
|
156
|
+
if expected_head is not None and (type(expected_head) is not str or not re.fullmatch('[a-f0-9]{64}',expected_head)):
|
|
157
|
+
raise CatalogError('ledger-head-mismatch')
|
|
158
|
+
if source_plan['previous'] is not None and expected_head is None:
|
|
159
|
+
raise CatalogError('ledger-head-required')
|
|
160
|
+
with _installation(connection,source_plan['next'],statement_timeout_ms=statement_timeout_ms,
|
|
161
|
+
lock_timeout_ms=lock_timeout_ms) as source:
|
|
162
|
+
schema = source['configuration']['schema']
|
|
163
|
+
exists = _verify_history_schema(connection,source)
|
|
164
|
+
history = _history_rows(connection,schema) if exists else []
|
|
165
|
+
if exists and not history:
|
|
166
|
+
raise CatalogError('ledger-history-missing')
|
|
167
|
+
if history and not prebuilt_indexes and not phases_applied:
|
|
168
|
+
_verify_installed(connection,history[-1],person_roles,statement_timeout_ms,lock_timeout_ms)
|
|
169
|
+
matches = [entry for entry in history if entry['plan']['id'] == source_plan['id']]
|
|
170
|
+
if matches:
|
|
171
|
+
entry = matches[0]
|
|
172
|
+
if entry.get('fileDigest') != file_digest:
|
|
173
|
+
raise CatalogError('migration-file-digest-conflict')
|
|
174
|
+
previous_head = history[entry['position']-2]['entryDigest'] if entry['position']>1 else None
|
|
175
|
+
if entry['plan']['digest'] != source_plan['digest'] or previous_head != expected_head:
|
|
176
|
+
raise CatalogError('ledger-digest-conflict')
|
|
177
|
+
return {'status':'already-applied','migrationId':source_plan['id'],'entryDigest':entry['entryDigest'],
|
|
178
|
+
'headDigest':history[-1]['entryDigest'],'generation':len(history),
|
|
179
|
+
'installedBundleDigest':history[-1]['plan']['toBundleDigest'],
|
|
180
|
+
'installationReference':_issue_reference(connection,history[-1]['plan']['next'],history[-1]),
|
|
181
|
+
'commitResponsibility':'caller','lifecycleAdmission':False}
|
|
182
|
+
if (expected_head != (history[-1]['entryDigest'] if history else None)
|
|
183
|
+
or source_plan['predecessor'] != ({'id':history[-1]['plan']['id'],'digest':history[-1]['plan']['digest']} if history else None)
|
|
184
|
+
or source_plan['fromBundleDigest'] != (history[-1]['plan']['toBundleDigest'] if history else None)):
|
|
185
|
+
raise CatalogError('ledger-head-mismatch')
|
|
186
|
+
_authorize_new_effects(source_plan)
|
|
187
|
+
if len(history) >= _MAX_ENTRIES:
|
|
188
|
+
raise CatalogError('ledger-capacity')
|
|
189
|
+
if not exists:
|
|
190
|
+
occupied = _one(connection,"""SELECT EXISTS(SELECT FROM pg_catalog.pg_class WHERE relnamespace=n.oid)
|
|
191
|
+
OR EXISTS(SELECT FROM pg_catalog.pg_proc WHERE pronamespace=n.oid)
|
|
192
|
+
OR EXISTS(SELECT FROM pg_catalog.pg_type WHERE typnamespace=n.oid)
|
|
193
|
+
FROM pg_catalog.pg_namespace n WHERE n.nspname=%s""",(schema,))
|
|
194
|
+
if occupied != (False,):
|
|
195
|
+
raise CatalogError('initial-schema-not-empty')
|
|
196
|
+
if not phases_applied:
|
|
197
|
+
for step in source_plan['steps']:
|
|
198
|
+
if step['transaction'] == 'forbidden':
|
|
199
|
+
continue
|
|
200
|
+
if step['kind'] == 'backfill':
|
|
201
|
+
_apply_backfill(connection,source_plan,step)
|
|
202
|
+
else:
|
|
203
|
+
_execute(connection,step['sql'])
|
|
204
|
+
if not exists:
|
|
205
|
+
_create_history(connection,schema)
|
|
206
|
+
_create_head_view(connection,source)
|
|
207
|
+
_create_outcomes(connection,source)
|
|
208
|
+
verify_native_catalog(connection,source,statement_timeout_ms=statement_timeout_ms,lock_timeout_ms=lock_timeout_ms)
|
|
209
|
+
approval_profile = source_plan.get('approvalProfile')
|
|
210
|
+
if approval_profile is not None:
|
|
211
|
+
from .approval import APPROVAL_PROFILE
|
|
212
|
+
|
|
213
|
+
if (source_plan.get('version') not in ('umf.table-migration/0.3','umf.table-migration/0.4','umf.table-migration/0.5')
|
|
214
|
+
or approval_profile != APPROVAL_PROFILE):
|
|
215
|
+
raise CatalogError('approval-catalog-mismatch')
|
|
216
|
+
verify_native_privileges(connection,source,person_roles=person_roles,
|
|
217
|
+
statement_timeout_ms=statement_timeout_ms,lock_timeout_ms=lock_timeout_ms,
|
|
218
|
+
_approval_profile=approval_profile)
|
|
219
|
+
# Phased plans include their typed seed execution as an ordered phase
|
|
220
|
+
# with a durable prefix receipt. Reapplying seeds during publication
|
|
221
|
+
# would repeat committed data effects after a recovery boundary.
|
|
222
|
+
if not phases_applied:
|
|
223
|
+
_apply_seeds(connection,source_plan,statement_timeout_ms=statement_timeout_ms,lock_timeout_ms=lock_timeout_ms)
|
|
224
|
+
witness = _capture_witness(connection,source)
|
|
225
|
+
position = len(history)+1
|
|
226
|
+
entry_digest = _entry_digest(position,expected_head,source_plan['digest'],witness,file_digest)
|
|
227
|
+
payload = {'plan':source_plan,'witness':witness,**({'fileDigest':file_digest} if file_digest is not None else {})}
|
|
228
|
+
_execute(connection,'INSERT INTO '+_q_bound(schema)+'.'+_q_bound(HISTORY)+' (position,migration_id,predecessor_entry_digest,plan_digest,entry_digest,payload) VALUES (%s,%s,%s,%s,%s,%s)',
|
|
229
|
+
(position,source_plan['id'],expected_head,source_plan['digest'],entry_digest,Jsonb(payload)))
|
|
230
|
+
# Also verify the newly persisted JSONB round trip, full chain and caps
|
|
231
|
+
# before exposing a result. No partially durable history is accepted.
|
|
232
|
+
stored = _history_rows(connection,schema)
|
|
233
|
+
if len(stored) != position or stored[-1]['entryDigest'] != entry_digest:
|
|
234
|
+
raise CatalogError('ledger-digest-conflict')
|
|
235
|
+
return {'status':'applied-in-transaction','migrationId':source_plan['id'],'entryDigest':entry_digest,
|
|
236
|
+
'headDigest':entry_digest,'generation':position,'installedBundleDigest':source['bundleDigest'],
|
|
237
|
+
'installationReference':_issue_reference(connection,source,stored[-1]),
|
|
238
|
+
'commitResponsibility':'caller','lifecycleAdmission':False}
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _finalize_phased_migration(connection,plan,*,approved_digest,expected_head,person_roles,
|
|
242
|
+
statement_timeout_ms=30_000,lock_timeout_ms=5_000,file_digest=None):
|
|
243
|
+
"""Publish history after the mixed runner has durably completed every phase."""
|
|
244
|
+
source_plan = _verified_plan(plan)
|
|
245
|
+
if (not source_plan.get('phases') and source_plan.get('steps')):
|
|
246
|
+
raise CatalogError('mixed-phase-profile')
|
|
247
|
+
return _apply_table_migration(connection,source_plan,approved_digest=approved_digest,
|
|
248
|
+
expected_head=expected_head,person_roles=person_roles,
|
|
249
|
+
statement_timeout_ms=statement_timeout_ms,lock_timeout_ms=lock_timeout_ms,
|
|
250
|
+
file_digest=file_digest,phases_applied=True)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def inspect_table_history(connection: Any, bundle: dict[str, object], *, person_roles: tuple[str, ...],
|
|
254
|
+
statement_timeout_ms: int = 30_000,
|
|
255
|
+
lock_timeout_ms: int = 5_000) -> dict[str, object]:
|
|
256
|
+
"""Observe durable history on a fresh transaction after an uncertain commit.
|
|
257
|
+
|
|
258
|
+
Caller supplies its expected current source bundle; no stale cohort is
|
|
259
|
+
silently substituted. The reference includes native metadata, without
|
|
260
|
+
returning original plans or seed payloads.
|
|
261
|
+
"""
|
|
262
|
+
with _installation(connection,bundle,statement_timeout_ms=statement_timeout_ms,
|
|
263
|
+
lock_timeout_ms=lock_timeout_ms) as source:
|
|
264
|
+
if not _verify_history_schema(connection,source):
|
|
265
|
+
raise CatalogError('ledger-history-missing')
|
|
266
|
+
history = _history_rows(connection,source['configuration']['schema'])
|
|
267
|
+
if not history:
|
|
268
|
+
raise CatalogError('ledger-history-missing')
|
|
269
|
+
if history[-1]['plan']['toBundleDigest'] != source['bundleDigest']:
|
|
270
|
+
raise CatalogError('installed-generation-mismatch')
|
|
271
|
+
_verify_installed(connection,history[-1],person_roles,statement_timeout_ms,lock_timeout_ms)
|
|
272
|
+
return {'generation':len(history),'headDigest':history[-1]['entryDigest'],
|
|
273
|
+
'installedBundleDigest':source['bundleDigest'],
|
|
274
|
+
'installationReference':_issue_reference(connection,source,history[-1]),
|
|
275
|
+
'entries':[{'migrationId':e['plan']['id'],'planDigest':e['plan']['digest'],'entryDigest':e['entryDigest']} for e in history],
|
|
276
|
+
'lifecycleAdmission':False}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT OR Apache-2.0
|
|
2
|
+
"""Fixed private lifecycle metadata; a name alone never establishes authority."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
from uuid import uuid4
|
|
7
|
+
|
|
8
|
+
from .catalog import (
|
|
9
|
+
_COLUMNS as _COLUMN_QUERY,
|
|
10
|
+
)
|
|
11
|
+
from .catalog import (
|
|
12
|
+
_CONSTRAINTS as _NATIVE_CONSTRAINTS,
|
|
13
|
+
)
|
|
14
|
+
from .catalog import (
|
|
15
|
+
_INDEXES as _NATIVE_INDEXES,
|
|
16
|
+
)
|
|
17
|
+
from .catalog import (
|
|
18
|
+
CatalogError,
|
|
19
|
+
_all,
|
|
20
|
+
_columns,
|
|
21
|
+
_constraint_shapes,
|
|
22
|
+
_execute,
|
|
23
|
+
_index_shapes,
|
|
24
|
+
_one,
|
|
25
|
+
_q,
|
|
26
|
+
)
|
|
27
|
+
from .probe import _Query, _run
|
|
28
|
+
|
|
29
|
+
HISTORY = 'umf_lifecycle_history'
|
|
30
|
+
_COLUMNS = [
|
|
31
|
+
'"position" pg_catalog.int8 NOT NULL',
|
|
32
|
+
'"migration_id" pg_catalog.text COLLATE pg_catalog."C" NOT NULL',
|
|
33
|
+
'"predecessor_entry_digest" pg_catalog.text COLLATE pg_catalog."C"',
|
|
34
|
+
'"plan_digest" pg_catalog.text COLLATE pg_catalog."C" NOT NULL',
|
|
35
|
+
'"entry_digest" pg_catalog.text COLLATE pg_catalog."C" NOT NULL',
|
|
36
|
+
'"payload" pg_catalog.jsonb NOT NULL',
|
|
37
|
+
]
|
|
38
|
+
_CONSTRAINTS = [
|
|
39
|
+
('umf_lifecycle_history_pkey','PRIMARY KEY ("position")'),
|
|
40
|
+
('umf_lifecycle_history_id_key','UNIQUE ("migration_id")'),
|
|
41
|
+
('positive_position','CHECK ("position">0)'),
|
|
42
|
+
('bounded_id','CHECK (pg_catalog.octet_length("migration_id") BETWEEN 1 AND 256)'),
|
|
43
|
+
('plan_digest_shape',"CHECK (\"plan_digest\" ~ '^[a-f0-9]{64}$')"),
|
|
44
|
+
('entry_digest_shape',"CHECK (\"entry_digest\" ~ '^[a-f0-9]{64}$')"),
|
|
45
|
+
('predecessor_shape',"CHECK (\"predecessor_entry_digest\" IS NULL OR \"predecessor_entry_digest\" ~ '^[a-f0-9]{64}$')"),
|
|
46
|
+
('object_payload',"CHECK (pg_catalog.jsonb_typeof(\"payload\")='object')"),
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
_STATE = """SELECT c.relkind,c.relpersistence,r.rolname,c.relrowsecurity,c.relforcerowsecurity,
|
|
50
|
+
c.relispartition,c.relhassubclass,c.relhasrules,c.relreplident,
|
|
51
|
+
EXISTS(SELECT FROM pg_catalog.pg_trigger t WHERE t.tgrelid=c.oid AND NOT t.tgisinternal),
|
|
52
|
+
EXISTS(SELECT FROM pg_catalog.pg_inherits i WHERE i.inhrelid=c.oid),
|
|
53
|
+
EXISTS(SELECT FROM pg_catalog.pg_policy p WHERE p.polrelid=c.oid),r.oid,c.oid,c.relam
|
|
54
|
+
FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace
|
|
55
|
+
JOIN pg_catalog.pg_roles r ON r.oid=c.relowner WHERE n.nspname=%s AND c.relname=%s"""
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _history_shape(connection: Any, schema: str) -> dict[str, Any] | None:
|
|
59
|
+
return _run(connection, _history_shape_steps(schema))
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _history_shape_steps(schema: str):
|
|
63
|
+
return (yield from _metadata_shape_steps(schema,HISTORY))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _metadata_shape_steps(schema: str, name: str):
|
|
67
|
+
"""Read-only snapshot; issuers must first reparse the fixed original schema."""
|
|
68
|
+
state = (yield _Query('one', _STATE, (schema, name)))
|
|
69
|
+
if state is None:
|
|
70
|
+
return None
|
|
71
|
+
oid = state[13]
|
|
72
|
+
return {'state': state, 'columns': (yield _Query('all', _COLUMN_QUERY, (schema, name))), 'constraints': (yield _Query('all', _NATIVE_CONSTRAINTS, (schema, name))), 'indexes': (yield _Query('all', _NATIVE_INDEXES, (schema, name))), 'acl': (yield _Query('all', "SELECT grantor,grantee,privilege_type,is_grantable\nFROM pg_catalog.pg_class c,LATERAL pg_catalog.aclexplode(COALESCE(c.relacl,pg_catalog.acldefault('r',c.relowner)))\nWHERE c.oid=%s ORDER BY 1,2,3,4", (oid,))), 'columnAcl': (yield _Query('all', 'SELECT a.attname,x.grantor,x.grantee,x.privilege_type,x.is_grantable\nFROM pg_catalog.pg_attribute a,LATERAL pg_catalog.aclexplode(a.attacl) x\nWHERE a.attrelid=%s ORDER BY 1,2,3,4,5', (oid,)))}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _create_history(connection: Any, schema: str, name: str = HISTORY) -> dict[str, str]:
|
|
76
|
+
aliases: dict[str, str] = {}
|
|
77
|
+
definitions = list(_COLUMNS)
|
|
78
|
+
for original, body in _CONSTRAINTS:
|
|
79
|
+
constraint = original if name == HISTORY else 'umf_verify_'+uuid4().hex
|
|
80
|
+
aliases[constraint] = original
|
|
81
|
+
definitions.append('CONSTRAINT '+_q(constraint)+' '+body)
|
|
82
|
+
_execute(connection,'CREATE TABLE '+_q(schema)+'.'+_q(name)+' ('+', '.join(definitions)+') USING heap')
|
|
83
|
+
return aliases
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _verify_history_schema(connection: Any, source: dict[str, Any]) -> bool:
|
|
87
|
+
"""Caller holds the administrative scope; verify even before reading payload."""
|
|
88
|
+
schema, owner = source['configuration']['schema'], source['configuration']['ownerRole']
|
|
89
|
+
query = _STATE
|
|
90
|
+
state = _one(connection,query,(schema,HISTORY))
|
|
91
|
+
if state is None:
|
|
92
|
+
return False
|
|
93
|
+
if tuple(state[:12]) != ('r','p',owner,False,False,False,False,False,'d',False,False,False):
|
|
94
|
+
raise CatalogError('ledger-catalog-mismatch')
|
|
95
|
+
owner_id, relation_id = state[12:14]
|
|
96
|
+
if state[14] != _one(connection,"SELECT oid FROM pg_catalog.pg_am WHERE amname='heap'")[0]:
|
|
97
|
+
raise CatalogError('ledger-catalog-mismatch')
|
|
98
|
+
_execute(connection,'LOCK TABLE '+_q(schema)+'.'+_q(HISTORY)+' IN ACCESS EXCLUSIVE MODE')
|
|
99
|
+
if _one(connection,query,(schema,HISTORY)) != state:
|
|
100
|
+
raise CatalogError('ledger-catalog-mismatch')
|
|
101
|
+
actual = _all(connection,"""SELECT x.grantor,x.grantee,x.privilege_type,x.is_grantable
|
|
102
|
+
FROM pg_catalog.pg_class c,LATERAL pg_catalog.aclexplode(COALESCE(c.relacl,pg_catalog.acldefault('r',c.relowner))) x
|
|
103
|
+
WHERE c.oid=%s ORDER BY 1,2,3,4""",(relation_id,))
|
|
104
|
+
expected = _all(connection,"""SELECT grantor,grantee,privilege_type,is_grantable
|
|
105
|
+
FROM pg_catalog.aclexplode(pg_catalog.acldefault('r',%s::pg_catalog.oid)) ORDER BY 1,2,3,4""",(owner_id,))
|
|
106
|
+
if actual != expected or _one(connection,"""SELECT count(*) FROM pg_catalog.pg_attribute a,
|
|
107
|
+
LATERAL pg_catalog.aclexplode(a.attacl) x WHERE a.attrelid=%s""",(relation_id,)) != (0,):
|
|
108
|
+
raise CatalogError('ledger-privilege-mismatch')
|
|
109
|
+
temporary = 'umf_verify_'+uuid4().hex
|
|
110
|
+
aliases = _create_history(connection,schema,temporary)
|
|
111
|
+
if (_columns(connection,schema,HISTORY) != _columns(connection,schema,temporary)
|
|
112
|
+
or _constraint_shapes(connection,schema,HISTORY,{}) != _constraint_shapes(connection,schema,temporary,aliases)
|
|
113
|
+
or _index_shapes(connection,schema,HISTORY,{}) != _index_shapes(connection,schema,temporary,aliases)):
|
|
114
|
+
raise CatalogError('ledger-catalog-mismatch')
|
|
115
|
+
_execute(connection,'DROP TABLE '+_q(schema)+'.'+_q(temporary))
|
|
116
|
+
return True
|