datadouble 0.0.3__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.
- datadouble/__init__.py +10 -0
- datadouble/announce.py +380 -0
- datadouble/anonymize.py +165 -0
- datadouble/api.py +1159 -0
- datadouble/boundary.py +47 -0
- datadouble/catalog.py +677 -0
- datadouble/channels.py +62 -0
- datadouble/ci.py +1357 -0
- datadouble/cli.py +940 -0
- datadouble/closure.py +712 -0
- datadouble/engine.py +435 -0
- datadouble/files.py +78 -0
- datadouble/images.py +265 -0
- datadouble/jailer.py +403 -0
- datadouble/names.py +114 -0
- datadouble/notify.py +262 -0
- datadouble/objects.py +195 -0
- datadouble/outcome.py +27 -0
- datadouble/pii.py +109 -0
- datadouble/planner.py +1492 -0
- datadouble/programs.py +68 -0
- datadouble/promote.py +190 -0
- datadouble/prune.py +258 -0
- datadouble/py.typed +0 -0
- datadouble/redaction.py +131 -0
- datadouble/registry.py +128 -0
- datadouble/replicate.py +562 -0
- datadouble/resolve.py +201 -0
- datadouble/run.py +1075 -0
- datadouble/runtime.py +154 -0
- datadouble/secrets.py +57 -0
- datadouble/settings.py +81 -0
- datadouble/signing.py +98 -0
- datadouble/spec.py +1590 -0
- datadouble/staging.py +346 -0
- datadouble/storage.py +222 -0
- datadouble/strict.py +498 -0
- datadouble/subset.py +406 -0
- datadouble/verify.py +171 -0
- datadouble/versions.py +25 -0
- datadouble-0.0.3.dist-info/METADATA +36 -0
- datadouble-0.0.3.dist-info/RECORD +45 -0
- datadouble-0.0.3.dist-info/WHEEL +4 -0
- datadouble-0.0.3.dist-info/entry_points.txt +12 -0
- datadouble-0.0.3.dist-info/licenses/LICENSE +21 -0
datadouble/subset.py
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
# Copyright (c) 2026- Tight Line LLC
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""The subset model, its two execution strategies, and the conditions adapters render.
|
|
5
|
+
|
|
6
|
+
Each table's selection is the union of its sources (a seed, every row, rows
|
|
7
|
+
whose parents were chosen through a followed child edge, rows referenced by
|
|
8
|
+
chosen children), restricted by its ``drop`` references. The selection is the
|
|
9
|
+
least fixed point of those rules, so cycles are fine.
|
|
10
|
+
|
|
11
|
+
Strategy A (``nested``) turns each table's rule into a :class:`Condition`
|
|
12
|
+
that refers to other tables' selections, for the engine to evaluate in one
|
|
13
|
+
query; it needs the dependencies between tables to be acyclic. Strategy B
|
|
14
|
+
(``keys``) evaluates the rules here, reading only the key columns it needs
|
|
15
|
+
through a :class:`KeyReader`, until nothing changes; its conditions hold the
|
|
16
|
+
chosen values themselves. Both give the same rows.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from collections.abc import Hashable, Iterable, Mapping
|
|
20
|
+
from dataclasses import dataclass, field
|
|
21
|
+
from enum import StrEnum
|
|
22
|
+
from graphlib import CycleError, TopologicalSorter
|
|
23
|
+
from typing import Protocol
|
|
24
|
+
|
|
25
|
+
from pydantic import BaseModel, ConfigDict
|
|
26
|
+
|
|
27
|
+
from datadouble.names import ColumnName, TableName
|
|
28
|
+
|
|
29
|
+
type KeyTuple = tuple[Hashable, ...]
|
|
30
|
+
"""Values of some columns of one row, as the engine's driver returns them."""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class _Model(BaseModel):
|
|
34
|
+
model_config = ConfigDict(strict=True, frozen=True, extra="forbid")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class EdgeMode(StrEnum):
|
|
38
|
+
"""How a reference from a child row to its parent is honoured."""
|
|
39
|
+
|
|
40
|
+
FOLLOW = "follow"
|
|
41
|
+
"""Pull in the parent rows of every chosen child row (the default)."""
|
|
42
|
+
ALL = "all"
|
|
43
|
+
"""Take the whole parent table (``follow: all``)."""
|
|
44
|
+
NULL = "null"
|
|
45
|
+
"""Don't pull parents; null references whose parent is missing."""
|
|
46
|
+
STUB = "stub"
|
|
47
|
+
"""Don't pull parents; insert placeholder parents for missing ones."""
|
|
48
|
+
DROP = "drop"
|
|
49
|
+
"""Don't pull parents; leave out child rows whose parent is missing."""
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass(frozen=True, slots=True)
|
|
53
|
+
class Edge:
|
|
54
|
+
"""A reference from ``child.columns`` to ``parent.parent_columns``."""
|
|
55
|
+
|
|
56
|
+
__pydantic_config__ = ConfigDict(strict=True, extra="forbid")
|
|
57
|
+
|
|
58
|
+
name: str
|
|
59
|
+
child: TableName
|
|
60
|
+
columns: tuple[ColumnName, ...]
|
|
61
|
+
parent: TableName
|
|
62
|
+
parent_columns: tuple[ColumnName, ...]
|
|
63
|
+
mode: EdgeMode = EdgeMode.FOLLOW
|
|
64
|
+
down: bool = False
|
|
65
|
+
"""Whether chosen parents also pull in their children through this edge."""
|
|
66
|
+
when: str | None = None
|
|
67
|
+
"""For virtual references: only child rows matching this native predicate refer."""
|
|
68
|
+
virtual: bool = False
|
|
69
|
+
parent_unique: bool = True
|
|
70
|
+
"""Whether ``parent_columns`` identify one parent row (a key), as declared ones do."""
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class TableRule(_Model):
|
|
74
|
+
"""How one table's rows are chosen."""
|
|
75
|
+
|
|
76
|
+
table: TableName
|
|
77
|
+
seed: str | None = None
|
|
78
|
+
every_row: bool = False
|
|
79
|
+
"""Every row is a source of rows, like a seed; ``drop`` references still restrict it."""
|
|
80
|
+
rows_kept: bool = True
|
|
81
|
+
"""False for structure-only tables, which keep their definition and no rows."""
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
# Conditions: what adapters turn into native queries.
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@dataclass(frozen=True, slots=True)
|
|
88
|
+
class AllRows:
|
|
89
|
+
"""Every row."""
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@dataclass(frozen=True, slots=True)
|
|
93
|
+
class NoRows:
|
|
94
|
+
"""No row."""
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@dataclass(frozen=True, slots=True)
|
|
98
|
+
class Predicate:
|
|
99
|
+
"""A native predicate from the spec; ``:as_of`` is bound by the adapter."""
|
|
100
|
+
|
|
101
|
+
text: str
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclass(frozen=True, slots=True)
|
|
105
|
+
class ValuesIn:
|
|
106
|
+
"""Rows whose ``columns`` hold one of ``values`` (strategy B)."""
|
|
107
|
+
|
|
108
|
+
columns: tuple[ColumnName, ...]
|
|
109
|
+
values: frozenset[KeyTuple]
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@dataclass(frozen=True, slots=True)
|
|
113
|
+
class SelectedBy:
|
|
114
|
+
"""Rows whose ``columns`` match ``source_columns`` of rows chosen in ``source`` (A).
|
|
115
|
+
|
|
116
|
+
``where`` narrows the source rows (a virtual reference's ``when``). When
|
|
117
|
+
``source_condition`` is set, the source rows are those matching it rather
|
|
118
|
+
than ``source``'s whole selection.
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
columns: tuple[ColumnName, ...]
|
|
122
|
+
source: TableName
|
|
123
|
+
source_columns: tuple[ColumnName, ...]
|
|
124
|
+
where: str | None = None
|
|
125
|
+
source_condition: "Condition | None" = None
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
@dataclass(frozen=True, slots=True)
|
|
129
|
+
class AnyNull:
|
|
130
|
+
"""Rows where any of ``columns`` is null."""
|
|
131
|
+
|
|
132
|
+
columns: tuple[ColumnName, ...]
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@dataclass(frozen=True, slots=True)
|
|
136
|
+
class AnyOf:
|
|
137
|
+
"""Rows matching any part."""
|
|
138
|
+
|
|
139
|
+
parts: tuple["Condition", ...]
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
@dataclass(frozen=True, slots=True)
|
|
143
|
+
class AllOf:
|
|
144
|
+
"""Rows matching every part."""
|
|
145
|
+
|
|
146
|
+
parts: tuple["Condition", ...]
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
type Condition = AllRows | NoRows | Predicate | ValuesIn | SelectedBy | AnyNull | AnyOf | AllOf
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def any_of(parts: Iterable[Condition]) -> Condition:
|
|
153
|
+
"""A union, simplified."""
|
|
154
|
+
kept: list[Condition] = []
|
|
155
|
+
for part in parts:
|
|
156
|
+
if isinstance(part, AllRows):
|
|
157
|
+
return part
|
|
158
|
+
if not isinstance(part, NoRows) and not (isinstance(part, ValuesIn) and not part.values):
|
|
159
|
+
kept.append(part)
|
|
160
|
+
if not kept:
|
|
161
|
+
return NoRows()
|
|
162
|
+
return kept[0] if len(kept) == 1 else AnyOf(tuple(kept))
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def all_of(parts: Iterable[Condition]) -> Condition:
|
|
166
|
+
"""An intersection, simplified."""
|
|
167
|
+
kept: list[Condition] = []
|
|
168
|
+
for part in parts:
|
|
169
|
+
if isinstance(part, NoRows):
|
|
170
|
+
return part
|
|
171
|
+
if not isinstance(part, AllRows):
|
|
172
|
+
kept.append(part)
|
|
173
|
+
if not kept:
|
|
174
|
+
return AllRows()
|
|
175
|
+
return kept[0] if len(kept) == 1 else AllOf(tuple(kept))
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
class SubsetError(Exception):
|
|
179
|
+
"""A subset that cannot be computed as asked."""
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
@dataclass(frozen=True, slots=True)
|
|
183
|
+
class Graph:
|
|
184
|
+
"""Table rules and edges, with the lookups both strategies need."""
|
|
185
|
+
|
|
186
|
+
rules: Mapping[TableName, TableRule]
|
|
187
|
+
edges: tuple[Edge, ...]
|
|
188
|
+
by_child: Mapping[TableName, tuple[Edge, ...]] = field(init=False)
|
|
189
|
+
by_parent: Mapping[TableName, tuple[Edge, ...]] = field(init=False)
|
|
190
|
+
|
|
191
|
+
def __post_init__(self) -> None:
|
|
192
|
+
"""Index the edges by table."""
|
|
193
|
+
by_child: dict[TableName, list[Edge]] = {}
|
|
194
|
+
by_parent: dict[TableName, list[Edge]] = {}
|
|
195
|
+
for edge in self.edges:
|
|
196
|
+
by_child.setdefault(edge.child, []).append(edge)
|
|
197
|
+
by_parent.setdefault(edge.parent, []).append(edge)
|
|
198
|
+
object.__setattr__(self, "by_child", {k: tuple(v) for k, v in by_child.items()})
|
|
199
|
+
object.__setattr__(self, "by_parent", {k: tuple(v) for k, v in by_parent.items()})
|
|
200
|
+
|
|
201
|
+
def incoming(self, table: TableName) -> tuple[list[Edge], list[Edge], list[Edge]]:
|
|
202
|
+
"""Edges that add rows to ``table`` (down, up) and those that restrict it (drop)."""
|
|
203
|
+
down = [e for e in self.by_child.get(table, ()) if e.down]
|
|
204
|
+
up = [e for e in self.by_parent.get(table, ()) if e.mode is EdgeMode.FOLLOW]
|
|
205
|
+
drop = [e for e in self.by_child.get(table, ()) if e.mode is EdgeMode.DROP]
|
|
206
|
+
return down, up, drop
|
|
207
|
+
|
|
208
|
+
def takes_all(self, table: TableName) -> bool:
|
|
209
|
+
"""Whether ``follow: all`` on a reference to ``table`` takes its whole table."""
|
|
210
|
+
rule = self.rules[table]
|
|
211
|
+
return rule.rows_kept and any(e.mode is EdgeMode.ALL for e in self.by_parent.get(table, ()))
|
|
212
|
+
|
|
213
|
+
def is_acyclic(self) -> bool:
|
|
214
|
+
"""Whether strategy A can run."""
|
|
215
|
+
try:
|
|
216
|
+
nested_conditions(self)
|
|
217
|
+
except SubsetError:
|
|
218
|
+
return False
|
|
219
|
+
return True
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def _when(edge: Edge, condition: Condition) -> Condition:
|
|
223
|
+
"""Restrict a child-side condition to the rows a conditional reference applies to."""
|
|
224
|
+
return all_of([Predicate(edge.when), condition]) if edge.when else condition
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
# Strategy A
|
|
228
|
+
#
|
|
229
|
+
# A followed child edge parent -> child also lets the child pull in parents
|
|
230
|
+
# through the same reference, which would make each table depend on the other.
|
|
231
|
+
# Child rows reached through that edge already have their parent chosen, so the
|
|
232
|
+
# parent only needs the child rows chosen some other way: that partial
|
|
233
|
+
# condition is inlined instead of referring to the child's whole selection.
|
|
234
|
+
# This holds when the referenced columns identify one parent row, as declared
|
|
235
|
+
# foreign keys and virtual references to a primary key do.
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _own_sources(rule: TableRule) -> list[Condition]:
|
|
239
|
+
"""The rows a table's own rule chooses: its seed, or every row."""
|
|
240
|
+
sources: list[Condition] = [Predicate(rule.seed)] if rule.seed else []
|
|
241
|
+
if rule.every_row:
|
|
242
|
+
sources.append(AllRows())
|
|
243
|
+
return sources
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def _nested(graph: Graph, table: TableName, skip: Edge | None, stack: frozenset[Edge]) -> Condition:
|
|
247
|
+
rule = graph.rules[table]
|
|
248
|
+
if not rule.rows_kept:
|
|
249
|
+
return NoRows()
|
|
250
|
+
if graph.takes_all(table):
|
|
251
|
+
return AllRows()
|
|
252
|
+
down, up, drop = graph.incoming(table)
|
|
253
|
+
sources = _own_sources(rule)
|
|
254
|
+
sources.extend(
|
|
255
|
+
_when(e, SelectedBy(e.columns, e.parent, e.parent_columns)) for e in down if e != skip
|
|
256
|
+
)
|
|
257
|
+
for e in up:
|
|
258
|
+
inline = e.down and e.parent_unique and e not in stack
|
|
259
|
+
partial = _nested(graph, e.child, e, stack | {e}) if inline else None
|
|
260
|
+
sources.append(SelectedBy(e.parent_columns, e.child, e.columns, e.when, partial))
|
|
261
|
+
return all_of(
|
|
262
|
+
[
|
|
263
|
+
any_of(sources),
|
|
264
|
+
*(
|
|
265
|
+
any_of([AnyNull(e.columns), SelectedBy(e.columns, e.parent, e.parent_columns)])
|
|
266
|
+
for e in drop
|
|
267
|
+
),
|
|
268
|
+
]
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def _references(condition: Condition) -> set[TableName]:
|
|
273
|
+
"""Tables whose whole selections ``condition`` refers to."""
|
|
274
|
+
match condition:
|
|
275
|
+
case SelectedBy(source=source, source_condition=None):
|
|
276
|
+
return {source}
|
|
277
|
+
case SelectedBy(source_condition=partial) if partial is not None:
|
|
278
|
+
return _references(partial)
|
|
279
|
+
case AnyOf(parts) | AllOf(parts):
|
|
280
|
+
return {name for part in parts for name in _references(part)}
|
|
281
|
+
case _:
|
|
282
|
+
return set()
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def nested_conditions(graph: Graph) -> dict[TableName, Condition]:
|
|
286
|
+
"""Each table's condition in terms of other tables' selections (strategy A).
|
|
287
|
+
|
|
288
|
+
Raises :class:`SubsetError` if the conditions refer to each other in a cycle.
|
|
289
|
+
"""
|
|
290
|
+
conditions = {table: _nested(graph, table, None, frozenset()) for table in graph.rules}
|
|
291
|
+
sorter = TopologicalSorter({table: _references(c) for table, c in conditions.items()})
|
|
292
|
+
try:
|
|
293
|
+
sorter.prepare()
|
|
294
|
+
except CycleError as error:
|
|
295
|
+
cycle = " -> ".join(str(name) for name in error.args[1])
|
|
296
|
+
message = f"the selection rules form a cycle: {cycle}"
|
|
297
|
+
raise SubsetError(message) from None
|
|
298
|
+
return conditions
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
# Strategy B
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
class KeyReader(Protocol):
|
|
305
|
+
"""Reads column values of chosen rows from the source snapshot."""
|
|
306
|
+
|
|
307
|
+
def project(
|
|
308
|
+
self, table: TableName, condition: Condition, columns: tuple[ColumnName, ...]
|
|
309
|
+
) -> Iterable[KeyTuple]:
|
|
310
|
+
"""Values of ``columns`` for rows of ``table`` matching ``condition``.
|
|
311
|
+
|
|
312
|
+
``condition`` only uses :class:`AllRows`, :class:`Predicate`,
|
|
313
|
+
:class:`ValuesIn`, :class:`AnyNull`, :class:`AnyOf` and :class:`AllOf`.
|
|
314
|
+
"""
|
|
315
|
+
...
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def _non_null(rows: Iterable[KeyTuple]) -> set[KeyTuple]:
|
|
319
|
+
return {row for row in rows if all(value is not None for value in row)}
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
@dataclass
|
|
323
|
+
class _KeyState:
|
|
324
|
+
graph: Graph
|
|
325
|
+
reader: KeyReader
|
|
326
|
+
# Values pulled into each edge's target: for a down edge, the parent
|
|
327
|
+
# values its child columns must match; for an up edge, the child values
|
|
328
|
+
# the parent columns must match; for a drop edge, the chosen parent values.
|
|
329
|
+
values: dict[Edge, set[KeyTuple]] = field(default_factory=dict[Edge, set[KeyTuple]])
|
|
330
|
+
|
|
331
|
+
def condition(self, table: TableName) -> Condition:
|
|
332
|
+
graph = self.graph
|
|
333
|
+
rule = graph.rules[table]
|
|
334
|
+
if not rule.rows_kept:
|
|
335
|
+
return NoRows()
|
|
336
|
+
if graph.takes_all(table):
|
|
337
|
+
return AllRows()
|
|
338
|
+
down, up, drop = graph.incoming(table)
|
|
339
|
+
sources = _own_sources(rule)
|
|
340
|
+
sources.extend(
|
|
341
|
+
_when(e, ValuesIn(e.columns, frozenset(self.values.get(e, ())))) for e in down
|
|
342
|
+
)
|
|
343
|
+
sources.extend(ValuesIn(e.parent_columns, frozenset(self.values.get(e, ()))) for e in up)
|
|
344
|
+
return all_of(
|
|
345
|
+
[
|
|
346
|
+
any_of(sources),
|
|
347
|
+
*(
|
|
348
|
+
any_of(
|
|
349
|
+
[AnyNull(e.columns), ValuesIn(e.columns, frozenset(self.values.get(e, ())))]
|
|
350
|
+
)
|
|
351
|
+
for e in drop
|
|
352
|
+
),
|
|
353
|
+
]
|
|
354
|
+
)
|
|
355
|
+
|
|
356
|
+
def outgoing(self, table: TableName) -> list[tuple[Edge, tuple[ColumnName, ...], str | None]]:
|
|
357
|
+
"""Edges whose values come from ``table``'s chosen rows."""
|
|
358
|
+
graph = self.graph
|
|
359
|
+
found: list[tuple[Edge, tuple[ColumnName, ...], str | None]] = [
|
|
360
|
+
(edge, edge.parent_columns, None)
|
|
361
|
+
for edge in graph.by_parent.get(table, ())
|
|
362
|
+
if edge.down or edge.mode is EdgeMode.DROP
|
|
363
|
+
]
|
|
364
|
+
found.extend(
|
|
365
|
+
(edge, edge.columns, edge.when)
|
|
366
|
+
for edge in graph.by_child.get(table, ())
|
|
367
|
+
if edge.mode is EdgeMode.FOLLOW
|
|
368
|
+
)
|
|
369
|
+
return found
|
|
370
|
+
|
|
371
|
+
def target(self, edge: Edge, source: TableName) -> TableName:
|
|
372
|
+
return edge.child if source == edge.parent else edge.parent
|
|
373
|
+
|
|
374
|
+
def step(self, table: TableName) -> set[TableName]:
|
|
375
|
+
"""Re-read ``table``'s outgoing values; return the tables that gained any."""
|
|
376
|
+
condition = self.condition(table)
|
|
377
|
+
grown: set[TableName] = set()
|
|
378
|
+
if isinstance(condition, NoRows):
|
|
379
|
+
return grown
|
|
380
|
+
for edge, columns, when in self.outgoing(table):
|
|
381
|
+
narrowed = all_of([condition, Predicate(when)]) if when else condition
|
|
382
|
+
fresh = _non_null(self.reader.project(table, narrowed, columns))
|
|
383
|
+
known = self.values.setdefault(edge, set())
|
|
384
|
+
if not fresh <= known:
|
|
385
|
+
known |= fresh
|
|
386
|
+
grown.add(self.target(edge, table))
|
|
387
|
+
return grown
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
def key_conditions(graph: Graph, reader: KeyReader) -> dict[TableName, Condition]:
|
|
391
|
+
"""Compute the selection by reading keys until nothing changes (strategy B).
|
|
392
|
+
|
|
393
|
+
Selections only grow, so the loop ends; each pass re-reads only tables
|
|
394
|
+
whose inputs changed.
|
|
395
|
+
"""
|
|
396
|
+
state = _KeyState(graph, reader)
|
|
397
|
+
pending = [table for table in graph.rules if graph.rules[table].rows_kept]
|
|
398
|
+
queued = set(pending)
|
|
399
|
+
while pending:
|
|
400
|
+
table = pending.pop(0)
|
|
401
|
+
queued.discard(table)
|
|
402
|
+
for grown in sorted(state.step(table), key=str):
|
|
403
|
+
if grown not in queued:
|
|
404
|
+
queued.add(grown)
|
|
405
|
+
pending.append(grown)
|
|
406
|
+
return {table: state.condition(table) for table in graph.rules}
|
datadouble/verify.py
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# Copyright (c) 2026- Tight Line LLC
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""Verification of a packaged golden image, restored into scratch space.
|
|
5
|
+
|
|
6
|
+
Every format of every image is restored and checked: row counts against the
|
|
7
|
+
working database, references between the image's tables, a PII scan of text
|
|
8
|
+
columns, and the spec's assertions. Reports carry counts, never values, so a
|
|
9
|
+
report never repeats the personal data it found.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from collections import Counter
|
|
13
|
+
from enum import StrEnum
|
|
14
|
+
|
|
15
|
+
from pydantic import BaseModel, ConfigDict
|
|
16
|
+
|
|
17
|
+
from datadouble.catalog import ColumnKind
|
|
18
|
+
from datadouble.engine import RestoredImage, TableCount
|
|
19
|
+
from datadouble.names import ColumnRef
|
|
20
|
+
from datadouble.pii import allowed, detect, like_pattern
|
|
21
|
+
from datadouble.planner import Plan, PlannedGroup
|
|
22
|
+
from datadouble.spec import ImageFormat
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class CheckKind(StrEnum):
|
|
26
|
+
"""The kinds of verification check."""
|
|
27
|
+
|
|
28
|
+
RESTORE = "restore"
|
|
29
|
+
ROW_COUNTS = "row-counts"
|
|
30
|
+
REFERENCES = "references"
|
|
31
|
+
PII_SCAN = "pii-scan"
|
|
32
|
+
ASSERTION = "assertion"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class _Model(BaseModel):
|
|
36
|
+
model_config = ConfigDict(strict=True, frozen=True, extra="forbid")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class Check(_Model):
|
|
40
|
+
"""One check's outcome."""
|
|
41
|
+
|
|
42
|
+
kind: CheckKind
|
|
43
|
+
name: str
|
|
44
|
+
passed: bool
|
|
45
|
+
detail: str = ""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class FormatReport(_Model):
|
|
49
|
+
"""The checks run against one restored format."""
|
|
50
|
+
|
|
51
|
+
format: ImageFormat
|
|
52
|
+
checks: tuple[Check, ...]
|
|
53
|
+
|
|
54
|
+
def passed(self) -> bool:
|
|
55
|
+
"""Whether every check passed."""
|
|
56
|
+
return all(check.passed for check in self.checks)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class VerificationReport(_Model):
|
|
60
|
+
"""Verification of one image, all formats."""
|
|
61
|
+
|
|
62
|
+
group: str
|
|
63
|
+
formats: tuple[FormatReport, ...]
|
|
64
|
+
|
|
65
|
+
def passed(self) -> bool:
|
|
66
|
+
"""Whether every format passed every check; an empty report never passes."""
|
|
67
|
+
return bool(self.formats) and all(report.passed() for report in self.formats)
|
|
68
|
+
|
|
69
|
+
def checks(self) -> tuple[Check, ...]:
|
|
70
|
+
"""Every check, across formats."""
|
|
71
|
+
return tuple(check for report in self.formats for check in report.checks)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def restore_failed(image_format: ImageFormat, reason: str) -> FormatReport:
|
|
75
|
+
"""The report for a format that could not be restored at all."""
|
|
76
|
+
return FormatReport(
|
|
77
|
+
format=image_format,
|
|
78
|
+
checks=(Check(kind=CheckKind.RESTORE, name="restore", passed=False, detail=reason),),
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def check_image(
|
|
83
|
+
plan: Plan,
|
|
84
|
+
group: PlannedGroup,
|
|
85
|
+
image_format: ImageFormat,
|
|
86
|
+
restored: RestoredImage,
|
|
87
|
+
expected: tuple[TableCount, ...],
|
|
88
|
+
) -> FormatReport:
|
|
89
|
+
"""Run every check against a restored format."""
|
|
90
|
+
checks = [Check(kind=CheckKind.RESTORE, name="restore", passed=True)]
|
|
91
|
+
checks.append(_row_counts(restored, expected))
|
|
92
|
+
checks.extend(_references(plan, group, restored))
|
|
93
|
+
checks.extend(_pii_scan(plan, group, restored))
|
|
94
|
+
checks.extend(
|
|
95
|
+
Check(
|
|
96
|
+
kind=CheckKind.ASSERTION,
|
|
97
|
+
name=assertion.name,
|
|
98
|
+
passed=(failing := restored.failing_rows(assertion.sql)) == 0,
|
|
99
|
+
detail="" if failing == 0 else f"{failing} row(s) returned",
|
|
100
|
+
)
|
|
101
|
+
for assertion in plan.assertions
|
|
102
|
+
)
|
|
103
|
+
return FormatReport(format=image_format, checks=tuple(checks))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _row_counts(restored: RestoredImage, expected: tuple[TableCount, ...]) -> Check:
|
|
107
|
+
actual = restored.counts(count.table for count in expected)
|
|
108
|
+
wrong = [
|
|
109
|
+
f"{count.table}: {actual.get(count.table, 0)} rows, expected {count.rows}"
|
|
110
|
+
for count in expected
|
|
111
|
+
if actual.get(count.table, 0) != count.rows
|
|
112
|
+
]
|
|
113
|
+
return Check(
|
|
114
|
+
kind=CheckKind.ROW_COUNTS,
|
|
115
|
+
name="row counts",
|
|
116
|
+
passed=not wrong,
|
|
117
|
+
detail="; ".join(wrong) or f"{len(expected)} tables match",
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _references(plan: Plan, group: PlannedGroup, restored: RestoredImage) -> list[Check]:
|
|
122
|
+
members = set(group.packaged())
|
|
123
|
+
rowless = group.rowless()
|
|
124
|
+
checks: list[Check] = []
|
|
125
|
+
for edge in plan.edges:
|
|
126
|
+
if edge.child not in members or edge.parent not in members:
|
|
127
|
+
continue
|
|
128
|
+
if edge.parent in rowless and edge.child not in rowless:
|
|
129
|
+
continue # The image holds the parent's structure only, on purpose.
|
|
130
|
+
orphans = restored.orphans(edge)
|
|
131
|
+
checks.append(
|
|
132
|
+
Check(
|
|
133
|
+
kind=CheckKind.REFERENCES,
|
|
134
|
+
name=edge.name,
|
|
135
|
+
passed=orphans == 0,
|
|
136
|
+
detail="" if orphans == 0 else f"{orphans} row(s) of {edge.child} have no parent",
|
|
137
|
+
)
|
|
138
|
+
)
|
|
139
|
+
return checks
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _pii_scan(plan: Plan, group: PlannedGroup, restored: RestoredImage) -> list[Check]:
|
|
143
|
+
allow = {rule.column: [like_pattern(p) for p in rule.patterns] for rule in plan.pii_allow}
|
|
144
|
+
checks: list[Check] = []
|
|
145
|
+
members = set(group.packaged())
|
|
146
|
+
for table in plan.catalog.tables:
|
|
147
|
+
if table.name not in members:
|
|
148
|
+
continue
|
|
149
|
+
for column in table.columns:
|
|
150
|
+
if column.kind is not ColumnKind.TEXT:
|
|
151
|
+
continue
|
|
152
|
+
ref = ColumnRef(table=table.name, column=column.name)
|
|
153
|
+
patterns = allow.get(ref, [])
|
|
154
|
+
found: Counter[str] = Counter()
|
|
155
|
+
for value in restored.text_values(ref):
|
|
156
|
+
if not allowed(value, patterns):
|
|
157
|
+
found.update(detect(value))
|
|
158
|
+
if found:
|
|
159
|
+
summary = ", ".join(f"{n} value(s) look like {d}" for d, n in sorted(found.items()))
|
|
160
|
+
checks.append(
|
|
161
|
+
Check(kind=CheckKind.PII_SCAN, name=str(ref), passed=False, detail=summary)
|
|
162
|
+
)
|
|
163
|
+
checks.append(
|
|
164
|
+
Check(
|
|
165
|
+
kind=CheckKind.PII_SCAN,
|
|
166
|
+
name="pii scan",
|
|
167
|
+
passed=not checks,
|
|
168
|
+
detail=f"{len(checks)} column(s) with possible personal data" if checks else "clean",
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
return checks
|
datadouble/versions.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Copyright (c) 2026- Tight Line LLC
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""Engine server versions: the newest of several.
|
|
5
|
+
|
|
6
|
+
Sources on one compatibility line (``images.version_line``, as long as the
|
|
7
|
+
engine's ``Capabilities.version_line`` says) read each other's dumps, so one
|
|
8
|
+
working server of the newest version on the line can hold every source on it.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import re
|
|
12
|
+
|
|
13
|
+
_NUMBER = re.compile(r"\d+")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _key(version: str) -> tuple[int, ...]:
|
|
17
|
+
return tuple(int(number) for number in _NUMBER.findall(version))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def newest(versions: tuple[str, ...]) -> str:
|
|
21
|
+
"""The newest of ``versions``, comparing their numbers in turn; the first of equals."""
|
|
22
|
+
if not versions:
|
|
23
|
+
message = "no versions to choose from"
|
|
24
|
+
raise ValueError(message)
|
|
25
|
+
return max(versions, key=_key)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: datadouble
|
|
3
|
+
Version: 0.0.3
|
|
4
|
+
Summary: Realistic, safe copies of production data for development and testing
|
|
5
|
+
Project-URL: Homepage, https://datadouble.tightlinesoftware.com/
|
|
6
|
+
Project-URL: Source, https://github.com/datadouble/datadouble
|
|
7
|
+
Project-URL: Changelog, https://github.com/datadouble/datadouble/blob/main/CHANGELOG.md
|
|
8
|
+
Author: Tight Line LLC
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
19
|
+
Classifier: Topic :: Database
|
|
20
|
+
Classifier: Topic :: Software Development :: Testing
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.13
|
|
23
|
+
Requires-Dist: faker>=40.0
|
|
24
|
+
Requires-Dist: pydantic>=2.11
|
|
25
|
+
Requires-Dist: pyyaml>=6.0.3
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# datadouble
|
|
29
|
+
|
|
30
|
+
Realistic, safe copies of production data for development and testing.
|
|
31
|
+
|
|
32
|
+
This is the core package: the planner, manifests, the JSON interface and the
|
|
33
|
+
command-line interface. Engine, storage and other adapters are published as
|
|
34
|
+
separate packages.
|
|
35
|
+
|
|
36
|
+
Documentation: <https://datadouble.tightlinesoftware.com/>
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
datadouble/__init__.py,sha256=kek9Te1mM2o4AWqeDgVpl-qA01EKA7umNUlMAto360g,255
|
|
2
|
+
datadouble/announce.py,sha256=kZGH_c-PMJ22o7NjPGRxQrlPp_qXrzqqAYVhlXOZoaU,14632
|
|
3
|
+
datadouble/anonymize.py,sha256=A7-VTaTBX9LTmxU1keQ5YUzZxMcLzxTf0BYo-0Zorxg,7011
|
|
4
|
+
datadouble/api.py,sha256=5Iy7E0Y9mjEN_B8CbKjSEjRJNd7a_nG2rA05qd6COnE,39602
|
|
5
|
+
datadouble/boundary.py,sha256=NfRb_LxZxi8Ww58GbxYXEwQb_ebFuxfrshqa6npTW5g,1838
|
|
6
|
+
datadouble/catalog.py,sha256=PZvuJbX-qcrr_v9Pzdq3te7M9aeDl384n1J7AwDkpAg,24219
|
|
7
|
+
datadouble/channels.py,sha256=kpG54GXwZPXy5cuqzfIAJDwod9nCsqIxNUkhpNjaPZk,2185
|
|
8
|
+
datadouble/ci.py,sha256=lRbmXZOYjOUfe7_BhzlJ-Y_hoEGcx41Q0jAAukVE9ko,51874
|
|
9
|
+
datadouble/cli.py,sha256=QCeLxqi3ZD-qnabj6MKpkAi110AHEkmKaBzJLxlcIrI,34947
|
|
10
|
+
datadouble/closure.py,sha256=AAgBZMh8VNr-rzJQChoI0prKUXJkX3D8i31sh4wAH8k,30727
|
|
11
|
+
datadouble/engine.py,sha256=-4RyuP2h5T2kT_xZVl89X9P0ERJyRqzK1YemnQxeDwU,16375
|
|
12
|
+
datadouble/files.py,sha256=9Se30mZPRczHhbOQs9kNkEn2z1ruH0yAYvylWSc0Mh4,2533
|
|
13
|
+
datadouble/images.py,sha256=N6YjJSVRICQ4M40zlCwoyA_HAI5Edn5I6ysv_ZfqRmo,9300
|
|
14
|
+
datadouble/jailer.py,sha256=qhrD0TaiMk6IWa70JBO14kNdzZr6JONnXiuai2yW9Og,15629
|
|
15
|
+
datadouble/names.py,sha256=VRa0XGKkf0FPrhDfP_bm89DODFZtdiEzvo4dh8vCRZ0,3773
|
|
16
|
+
datadouble/notify.py,sha256=_6HYve7xdzhLRuIIDMbPl0GSmywRxrkJABQguDwt0Ns,9968
|
|
17
|
+
datadouble/objects.py,sha256=2RJ-2lDXH7rPAT9mfCg0s928emmyMLMgdwVgtkj3lvs,8079
|
|
18
|
+
datadouble/outcome.py,sha256=V33rDhDsoDhrLir_ruBrI9YDjLoNKZIo7tB8aIBDf9E,575
|
|
19
|
+
datadouble/pii.py,sha256=qCwmUbM6NvYCzHjt3VPLb88orl7HT9NgmHjdlRTGCg0,3484
|
|
20
|
+
datadouble/planner.py,sha256=X9s0ODLB_Kfa6vHjD-LAKOfTPPvqRfuMUlPhP8AgYMU,62450
|
|
21
|
+
datadouble/programs.py,sha256=2vzkUBR6y-UoXyN7_YlTLh4h7Cggn5uOItCv6Z8gvac,2423
|
|
22
|
+
datadouble/promote.py,sha256=gXHwyLPaFsM2kkTqf6UismNo2viOmtlKhXnchmbHvuo,7597
|
|
23
|
+
datadouble/prune.py,sha256=4RV6GMU5z9bxX4vYzFHfj0iTVTGHUfEFxzfzYm9WVUo,9195
|
|
24
|
+
datadouble/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
25
|
+
datadouble/redaction.py,sha256=qBTSsi1BorFpQnVhTftpbo1dTRmi5ncDpRfWMA5DMTc,5364
|
|
26
|
+
datadouble/registry.py,sha256=EdTrzXrmwB6Cu-ZqGrJRBIi2Pzd2Z14hxsWhfqfWhoc,5375
|
|
27
|
+
datadouble/replicate.py,sha256=wqpnc_0hkiGQai3qgNU9xz0MnZZhMeJ13hUjV5IN6Cw,21596
|
|
28
|
+
datadouble/resolve.py,sha256=RnCu3Kk1fo-DBWQUlhR-xPSMKiNfoecgy1Ce6ixzoaY,8381
|
|
29
|
+
datadouble/run.py,sha256=R6Xb2Zej6lkZM-Ls43XSf80SevxNNuPnE6uamRG3Dfo,43374
|
|
30
|
+
datadouble/runtime.py,sha256=AzJ-Z1y7gXTrCsalSU9dUUabtxYthfUYWhRN2U4MD48,5117
|
|
31
|
+
datadouble/secrets.py,sha256=cbM6Cdi_Ei8UtFxWWr0fWjMC85E_M6XX43oA9HmDEHc,1897
|
|
32
|
+
datadouble/settings.py,sha256=gN1OkcPDH7IUQbkeimfTNzksVZFqK0ndwLgPN0wf4mg,3025
|
|
33
|
+
datadouble/signing.py,sha256=I5pLf5OcTITBLjP6R54B3M5wi05g_2H5apJtta2JIwk,3774
|
|
34
|
+
datadouble/spec.py,sha256=eH7BEqjhUQxYEjq40yQlITt52NHAHpXgMY9umDn-HDY,60860
|
|
35
|
+
datadouble/staging.py,sha256=igKiefDxDbN28CxiElNgVtW3k0Fc66lW6k9YRP2vy7o,13676
|
|
36
|
+
datadouble/storage.py,sha256=QDWllkHsk0iewJTZimJpXF3_XrwSVja2I1V1lg58G3E,7901
|
|
37
|
+
datadouble/strict.py,sha256=4ZIZznYBYtr8JlMEFvzBRH67F1uO8tMPfPNMi4t_jcQ,18664
|
|
38
|
+
datadouble/subset.py,sha256=_0rQFFhY-5A3qp_NUIk8nIKS5JLDiCzm0uCaq_nAkwI,14479
|
|
39
|
+
datadouble/verify.py,sha256=JuBHTKlZ3Cdal94jmgBWYoJDJpKo5lJjwhPBO2Hkugs,5762
|
|
40
|
+
datadouble/versions.py,sha256=iIynscKHIhtSjXNRkFrdMErhNdRJhHUVB7Xir6-NfLY,786
|
|
41
|
+
datadouble-0.0.3.dist-info/METADATA,sha256=YW8_vSoVR5SSSRupqKh0NHZh7iT9JgY2ZFOu1gXpF1o,1380
|
|
42
|
+
datadouble-0.0.3.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
43
|
+
datadouble-0.0.3.dist-info/entry_points.txt,sha256=WojJ041N6KAmddaiVDcT0AgbW_VAAGyhVsfqSmGc9Cs,328
|
|
44
|
+
datadouble-0.0.3.dist-info/licenses/LICENSE,sha256=2LcT-SSMiSa7vrHsdC41sUWNmkcwZqIYdYBaSVmqqT8,1072
|
|
45
|
+
datadouble-0.0.3.dist-info/RECORD,,
|