Skip to content

Saved reports stop storing and returning a copy of the reform's changes - #4402

Merged
anth-volk merged 6 commits into
masterfrom
v2plan/d1-5a-saved-reports-drop-reform-copy
Dec 3, 2026
Merged

anth-volk merged 6 commits into
masterfrom
v2plan/d1-5a-saved-reports-drop-reform-copy

Conversation

@anth-volk

Copy link
Copy Markdown
Collaborator

Fixes #4401

Summary

Saved reports (user_report_associations rows, served by /v2/user-reports) kept a copy of their reform's changes in the JSONB column provisions. With app v3 change D1 · 5, report pages, "Edit in Build", re-runs and shared links read the report's policy and dynamic records instead, so API v2 stops storing and returning the copy. This is the first of two steps (tracker task D1 · 5a):

  • UserReportAssociation keeps provisions as a SQLModel field, with its declaration unchanged, and exclude_properties leaves it out of the mapper. API v1 neither loads nor writes the column: no update or insert through the model can name it. The table keeps the column, so there is no Alembic revision and alembic -c alembic-v2.ini check finds no difference. Rows saved before keep their copies, which nothing reads. This is the form used for columns awaiting removal since 2026-11-24 (93e3be3b, C1 · 51a), which rule api.data.orm-only accepts without an exception. DIRECT_SQLALCHEMY_EXCEPTIONS lists columns_left_out_of_the_mapper again.
  • POST /v2/user-reports and PATCH /v2/user-reports/{id} still accept provisions, checked as before (the strict provision models, the 64 KiB limit, finite numbers), and discard it. App v3 tabs running builds from before D1 · 5 still send it. OpenAPI marks the request field deprecated. A PATCH that sends only provisions succeeds and changes no stored field except updated_at.
  • No response returns provisions: the owner's read and list (/v2/user-reports), GET /v2/shared-reports/{id}, GET /v2/me/export, GET /v2/admin/users/{user_id}/saved-items and published reports' saved-report fields. ReportProvisionItem and ReportProvisionIntervalItem leave the OpenAPI schema.
  • The migration contract registry and its generated files drop the response field. docs/migration/stage-12a-user-login.md, the Alembic guide's "Columns a mapper leaves out" and a removed changelog fragment describe the change.

The second step is tracker task D1 · 10, in a later release: an autogenerated revision drops user_report_associations.provisions, the request models stop accepting the field, and columns_left_out_of_the_mapper leaves DIRECT_SQLALCHEMY_EXCEPTIONS again, as C1 · 53 did. The two steps are separate because a release upgrades the v2 schema before the new Cloud Run revision takes traffic, so the previously deployed code, which maps the column, must keep working on the upgraded schema. The same split was used for the report columns (C1 · 40a, then C1 · 41) and parameter_values.dynamic_id (C1 · 51a, then C1 · 53).

Migration scope

  • Route groups covered: API v2's saved reports (/v2/user-reports), shared reports (/v2/shared-reports/{id}), published reports (/v2/published-reports/{user_report_id}), the account export (/v2/me/export) and the admin saved-items read.
  • Nothing on the Flask API v1 path changes.
  • No schema change: the column stays until D1 · 10.
  • Intended contract change: the responses above no longer have provisions; the request field is deprecated, checked, then discarded.

Testing

Each test was run individually by node ID.

  • New: tests/unit/v2/test_saved_report_reform_copy.py:
    • a save stores no copy;
    • three kinds of update leave a stored copy as it was;
    • the owner's read and list, the shared read and the account export return no copy;
    • no update (with synchronize_session=False) or insert through the model can name provisions.
  • New: tests/unit/v2/test_models.py::test_saved_reports_keep_the_reform_copy_column_unmapped: the mapper leaves the column out; the table keeps it, nullable; the field declares JSONB on PostgreSQL. Tests read and write the unmapped column through a column-only class on a registry of its own (tests/unit/v2/saved_report_reform_copies.py).
  • Changed:
    • the user-report, shared-report, published-report, account-export, account-service and admin saved-items tests expect no provisions in responses;
    • the OpenAPI test checks the request field is deprecated and the response schemas are gone;
    • test_model_persistence.py::test_direct_sqlalchemy_categories_are_complete_and_documented expects columns_left_out_of_the_mapper;
    • tests/integration/test_v2_user_identity_persistence.py::test_non_finite_numbers_never_reach_jsonb checks nothing is stored.
  • Commits, in order:
    • 2467d9c8 (tests): 14 of the 16 changed unit tests failed on assertions; the 2 that passed are rewrites of existing tests whose behaviour doesn't change. The integration test failed on its assertion (disposable PostgreSQL 17).
    • b9de8730 (change): the 17 changed unit tests (the 16 plus the admin saved-items test, whose fixture changed) and the integration test passed.
    • 73836ce3 and 040d4302: review rounds 1 and 2's fixes.
    • 5882f4b5 (tests, review round 3): the statement test's two cases, the model test and the category test failed on assertions.
    • 400102be (change): 23 test runs passed. They are the changed tests above and the ORM-only rule tests test_database_code_uses_only_the_orm, test_orm_only_exceptions_match_a_finding_and_name_a_category and test_every_table_is_mapped_by_a_class. The integration test passed too.
  • alembic -c alembic-v2.ini check on PostgreSQL 17 at head: no new upgrade operations.
  • On the changed files: ruff check, ruff format --check and mypy. mypy reports no new errors: associations.py has the six Field(sa_type=...) errors master has.
  • Also passed: lint-imports, python -m scripts.rules validate and guard, and python scripts/run_quality_guards.py.
  • scripts/export_migration_contracts.py regenerated the contract files.

No # noqa suppressions are added.

Review

Three rounds of adversarial review, each by a fresh reviewer in its own clone:

  • Round 1 (head b9de8730): one high-priority bug. The enforced rule api.data.orm-only's test test_database_code_uses_only_the_orm failed on a Core sa.Column("provisions", ...) in UserReportAssociation's __table_args__. 73836ce3 first declared it as a rule exception, and 400102be later replaced that with the field form (round 3). Wording notes on the update docstring, the request field's OpenAPI description and the changelog fragment were fixed in 73836ce3.
  • Round 2 (head 73836ce3): no bugs. Three low-priority wording notes were fixed in 040d4302.
  • Round 3 (73836ce3..040d4302): no bugs. It raised one medium-priority point outside its diff: the decision of 2026-11-24 keeps a column awaiting removal as a SQLModel field left out of the mapper, not as a Core column. That form also refuses every update and insert through the model that names the column. 5882f4b5 and 400102be follow that decision, which also settles round 3's low-priority note on the guide's wording. They came after the third and last round, so no review round covered them.

Bugs left: none.

Merge order

Earlier tasks in the tracker and what each must reach before this merges:

  • D1 · 0, D1 · 0a, D1 · 0b, D1 · 1, D1 · 2, D1 · 3 and D1 · 4: merged (they are done). This PR uses none of their code; they come first in tracker order.
  • D1 · 5 (app v3 reads the report's policy and dynamic records): deployed. Until its app v3 build is live, report pages read provisions from these responses. Deploying this first would show no reform changes on saved reports and make re-runs refuse.

This PR branches from master and builds on no other task's branch.

🤖 Generated with Claude Code

anth-volk and others added 6 commits December 4, 2026 01:15
…ailing)

Saved reports no longer keep a copy of their reform's changes
(`provisions`): pages read the report's policy and dynamic records. The
tests expect:

- `UserReportAssociation` to leave the `provisions` column unmapped,
  while the table keeps it, JSONB and nullable, until D1 · 10's revision
  drops it.
- A save or an update to take the copy, which app v3 builds from before
  D1 · 5 still send, store none and leave a copy stored before untouched.
- No read to return a copy: the owner's read and list, the shared read,
  the account export and the published report's saved-report fields.
- OpenAPI to keep the request field, marked deprecated, and drop the
  response field.

Rows saved before keep their copies; the tests store and read them
through a class of their own that maps only the saved report's ID and
the column.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Report pages read the report's policy and dynamic records, so a saved
report keeps no copy of its reform's changes (`provisions`):

- `UserReportAssociation` leaves the `provisions` column out of its
  mapper (`exclude_properties`), declared in `__table_args__` so the
  table keeps it. There is no revision: a release of this code keeps
  serving while D1 · 10's revision drops the column, and rows saved
  before keep their copies until then.
- `/v2/user-reports` saves and updates still take `provisions`, checked
  as before, because app v3 builds from before D1 · 5 send it, and
  discard it; OpenAPI marks the field deprecated. An update that sends
  only that changes nothing.
- The owner's read and list, `/v2/shared-reports/{id}`, `/v2/me/export`
  and published reports no longer return it.

The test fixtures stop building read records with the field, the
contract registry drops it from the response fields, and the generated
contract files are regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UserReportAssociation declares user_report_associations.provisions as a
Core column in __table_args__, which the api.data.orm-only rule reports.
The ORM has no form for a column a table keeps but its class doesn't map,
so the rule registry names it as a no-orm-form exception until D1 · 10's
revision drops the column. The Alembic guide now says such a column needs
the exception, and the update docstring, the request field's OpenAPI
description and the changelog fragment describe what the code does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Alembic guide said an update through the model naming such a column
raises AttributeError. A Session refuses it under its default
synchronization but writes the column with synchronize_session=False, so
the guide now says that what keeps the column unwritten is that no code
names it. The simulation-role privilege tests' notes that no model leaves
a column out now speak only of the simulation tables, and the request
field's OpenAPI description says the value is checked, then discarded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ailing)

Review round 3: the decision of 2026-11-24 keeps a column awaiting removal
as a SQLModel field that exclude_properties leaves out of the mapper, not
as a Core column in __table_args__. In that form no update or insert
through UserReportAssociation can name provisions, whatever its
synchronization; the model test reads the column's PostgreSQL type from
the field; and DIRECT_SQLALCHEMY_EXCEPTIONS lists
columns_left_out_of_the_mapper again, as it did for C1 · 51a. All four
runs fail on assertions against the Core-column form.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 3: the decision of 2026-11-24 (93e3be3) and C1 · 51a keep a
column awaiting removal as a SQLModel field that exclude_properties leaves
out of the mapper. UserReportAssociation now does so for provisions, with
the declaration it had before, instead of a Core column in __table_args__.
So no update or insert through the model can name the column, the rule
api.data.orm-only needs no exception (removed from the registry, and
rules.md regenerated), and DIRECT_SQLALCHEMY_EXCEPTIONS lists
columns_left_out_of_the_mapper again.

The Alembic guide's "Columns a mapper leaves out" is as before, naming
this column and that inserts are refused too; the schema-reading helper
loses declared_column; and the simulation-role privilege tests' docstring
says again that such a column has no statement through the model.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.94%. Comparing base (a64dbcd) to head (400102b).
⚠️ Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
policyengine_api/services/v2/user_reports/types.py 83.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4402      +/-   ##
==========================================
- Coverage   93.95%   93.94%   -0.01%     
==========================================
  Files         459      459              
  Lines       26287    26261      -26     
  Branches     4096     4088       -8     
==========================================
- Hits        24697    24670      -27     
- Misses        903      907       +4     
+ Partials      687      684       -3     
Flag Coverage Δ
integration 60.85% <88.88%> (-0.01%) ⬇️
unit 90.41% <88.88%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@anth-volk
anth-volk marked this pull request as ready for review December 3, 2026 23:30
@anth-volk
anth-volk merged commit aeca8e3 into master Dec 3, 2026
23 checks passed

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Saved reports stop storing and returning a copy of the reform's changes

1 participant