Skip to content

Commit a0e8730

Browse files
AlexeyShalaevAlexey Shalaev
andauthored
fix: plan keeps unattached_rows and rows_in_default when the policy needs facts (#109)
measure() rebuilt the tree from its root and orphans, dropping the unattached tables and the earliest DEFAULT row that inspection had put on it. With a policy that reads facts (Unreferenced, SizeAbove, RowsAbove, a SQL predicate) plan never reported either finding. It now returns a copy of the tree with the facts added. Closes #108 Co-authored-by: Alexey Shalaev <shalaevad.alexey@gmail.com>
1 parent 2890538 commit a0e8730

5 files changed

Lines changed: 76 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919

2020
* a backfilled window always attaches with writers held at the parent ([#89](https://github.com/bedrock-python/pg-partsmith/issues/89)) ([b1260c1](https://github.com/bedrock-python/pg-partsmith/commit/b1260c1a8bcdd6388c3836d57eb548f8fe5959bb))
2121

22+
## 1.8.1 — the two findings survive a policy that reads facts
23+
24+
`unattached_rows` (1.7.0) and `rows_in_default` (1.8.0) went missing whenever the lifecycle
25+
policy needed facts — `Unreferenced()`, `SizeAbove`, `RowsAbove`, a SQL predicate, including
26+
the `ExpireIf(when=AllOf(members=(KeepNewest(...), Unreferenced())))` the foreign-key guide
27+
recommends. `measure()` rebuilt the tree from its root and orphans and dropped everything else
28+
inspection had put on it, so `plan`, `plan --check` and `apply` were silent about exactly the
29+
states those findings exist for. It now returns a copy with the facts added
30+
([#108](https://github.com/bedrock-python/pg-partsmith/issues/108)).
31+
2232
## 1.8.0 — rows in DEFAULT that no run would reach are reported
2333

2434
An application that writes its own key values can put a row into DEFAULT after the table

‎pg_partsmith/aio/metadata.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ async def measure(
226226
orphans = tuple(
227227
o.model_copy(update={"facts": measured[o.name]}) if o.name in measured else o for o in tree.orphans
228228
)
229-
return ActualTree(root=root, orphans=orphans)
229+
# A copy, not a new tree: whatever else inspection put on it -- unattached
230+
# tables, the earliest row in DEFAULT -- belongs to the plan as much as the facts.
231+
return tree.model_copy(update={"root": root, "orphans": orphans})
230232

231233
async def get_partition_tree(self, table_name: str) -> PartitionNode | None:
232234
"""Return the tree rooted at ``table_name``, without orphans.

‎pg_partsmith/sync/metadata.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ def measure(
226226
orphans = tuple(
227227
o.model_copy(update={"facts": measured[o.name]}) if o.name in measured else o for o in tree.orphans
228228
)
229-
return ActualTree(root=root, orphans=orphans)
229+
# A copy, not a new tree: whatever else inspection put on it -- unattached
230+
# tables, the earliest row in DEFAULT -- belongs to the plan as much as the facts.
231+
return tree.model_copy(update={"root": root, "orphans": orphans})
230232

231233
def get_partition_tree(self, table_name: str) -> PartitionNode | None:
232234
"""Return the tree rooted at ``table_name``, without orphans.

‎tests/integration/aio/test_planning.py‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from pg_partsmith.aio.metadata import PostgresMetadataProvider
1818
from pg_partsmith.entities import MaintenanceIssueStep
1919
from pg_partsmith.exceptions import PlanStaleError
20-
from pg_partsmith.lifecycle import CreateAhead, DetachMode, KeepNewest, LifecyclePolicy
20+
from pg_partsmith.lifecycle import AllOf, CreateAhead, DetachMode, ExpireIf, KeepNewest, LifecyclePolicy, Unreferenced
2121
from pg_partsmith.plan import FindingReason, MaintenancePlan, OperationKind, Reason, Severity
2222
from pg_partsmith.planner import PlanMode
2323
from pg_partsmith.topology import RangeBounds, RelationKind
@@ -141,6 +141,35 @@ async def test__plan__a_row_dated_before_every_partition__is_reported_until_back
141141
assert await is_attached(db_engine, f"{table}__2025_06")
142142

143143

144+
async def test__plan__a_policy_that_needs_facts__still_reports_rows_no_run_would_reach(
145+
db_engine: AsyncEngine, table: str
146+
) -> None:
147+
# Arrange -- a past row in DEFAULT and a filled window table nobody attached, under the
148+
# retention rule the foreign-key guide recommends, which reads a fact
149+
await exec_sql(db_engine, f'CREATE TABLE "{table}_default" PARTITION OF "{table}" DEFAULT')
150+
lifecycle = LifecyclePolicy(
151+
creation=CreateAhead(count=1),
152+
retention=ExpireIf(when=AllOf(members=(KeepNewest(count=12), Unreferenced()))),
153+
)
154+
config = monthly_config(table, lifecycle=lifecycle)
155+
service = make_service(db_engine)
156+
now = datetime.fromisoformat(NOW).replace(tzinfo=UTC)
157+
await service.apply(config, await service.plan(config, now=now))
158+
await exec_sql(db_engine, f"INSERT INTO \"{table}\" (created_at, payload) VALUES ('2025-06-15', 'late')") # noqa: S608
159+
await exec_sql(db_engine, f'CREATE TABLE "{table}__2025_01" (LIKE "{table}" INCLUDING DEFAULTS)')
160+
await exec_sql(db_engine, f"INSERT INTO \"{table}__2025_01\" (created_at, payload) VALUES ('2025-01-15', 'x')") # noqa: S608
161+
162+
# Act
163+
plan = await service.plan(config, now=now)
164+
165+
# Assert
166+
assert config.lifecycle.needs_facts
167+
assert {(f.partition_name, f.reason) for f in plan.findings} == {
168+
(f"public.{table}_default", FindingReason.ROWS_IN_DEFAULT),
169+
(f"public.{table}__2025_01", FindingReason.UNATTACHED_ROWS),
170+
}
171+
172+
144173
# ── plan() / apply() ────────────────────────────────────────────────────────────
145174

146175

‎tests/integration/sync/test_planning.py‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from pg_partsmith.entities import MaintenanceIssueStep
1717
from pg_partsmith.exceptions import PlanStaleError
18-
from pg_partsmith.lifecycle import CreateAhead, DetachMode, KeepNewest, LifecyclePolicy
18+
from pg_partsmith.lifecycle import AllOf, CreateAhead, DetachMode, ExpireIf, KeepNewest, LifecyclePolicy, Unreferenced
1919
from pg_partsmith.plan import FindingReason, MaintenancePlan, OperationKind, Reason, Severity
2020
from pg_partsmith.planner import PlanMode
2121
from pg_partsmith.sync.metadata import PostgresMetadataProvider
@@ -138,6 +138,35 @@ def test__plan__a_row_dated_before_every_partition__is_reported_until_backfill_m
138138
assert is_attached(sync_db_engine, f"{table}__2025_06")
139139

140140

141+
def test__plan__a_policy_that_needs_facts__still_reports_rows_no_run_would_reach(
142+
sync_db_engine: Engine, table: str
143+
) -> None:
144+
# Arrange -- a past row in DEFAULT and a filled window table nobody attached, under the
145+
# retention rule the foreign-key guide recommends, which reads a fact
146+
exec_sql(sync_db_engine, f'CREATE TABLE "{table}_default" PARTITION OF "{table}" DEFAULT')
147+
lifecycle = LifecyclePolicy(
148+
creation=CreateAhead(count=1),
149+
retention=ExpireIf(when=AllOf(members=(KeepNewest(count=12), Unreferenced()))),
150+
)
151+
config = monthly_config(table, lifecycle=lifecycle)
152+
service = make_service(sync_db_engine)
153+
now = datetime.fromisoformat(NOW).replace(tzinfo=UTC)
154+
service.apply(config, service.plan(config, now=now))
155+
exec_sql(sync_db_engine, f"INSERT INTO \"{table}\" (created_at, payload) VALUES ('2025-06-15', 'late')") # noqa: S608
156+
exec_sql(sync_db_engine, f'CREATE TABLE "{table}__2025_01" (LIKE "{table}" INCLUDING DEFAULTS)')
157+
exec_sql(sync_db_engine, f"INSERT INTO \"{table}__2025_01\" (created_at, payload) VALUES ('2025-01-15', 'x')") # noqa: S608
158+
159+
# Act
160+
plan = service.plan(config, now=now)
161+
162+
# Assert
163+
assert config.lifecycle.needs_facts
164+
assert {(f.partition_name, f.reason) for f in plan.findings} == {
165+
(f"public.{table}_default", FindingReason.ROWS_IN_DEFAULT),
166+
(f"public.{table}__2025_01", FindingReason.UNATTACHED_ROWS),
167+
}
168+
169+
141170
# ── plan() / apply() ────────────────────────────────────────────────────────────
142171

143172

0 commit comments

Comments
 (0)