|
17 | 17 | from pg_partsmith.aio.metadata import PostgresMetadataProvider |
18 | 18 | from pg_partsmith.entities import MaintenanceIssueStep |
19 | 19 | 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 |
21 | 21 | from pg_partsmith.plan import FindingReason, MaintenancePlan, OperationKind, Reason, Severity |
22 | 22 | from pg_partsmith.planner import PlanMode |
23 | 23 | 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 |
141 | 141 | assert await is_attached(db_engine, f"{table}__2025_06") |
142 | 142 |
|
143 | 143 |
|
| 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 | + |
144 | 173 | # ── plan() / apply() ──────────────────────────────────────────────────────────── |
145 | 174 |
|
146 | 175 |
|
|
0 commit comments